C++之set find结构体对象

这篇博客详细介绍了如何运用深度优先搜索(DFS)策略解决LeetCode中的365号问题——水壶问题。作者通过创建状态结构体和自定义排序函数,实现了状态的存储和比较,并在搜索过程中避免了重复状态。最后,通过递归地尝试所有可能的操作,如倒水、清空或填满水壶,来判断是否能测量出目标容量的水。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下面的例子作说明,leetcode中的365水壶问题:

class Solution {
public:
    struct status{
        int left;
        int right;
    };
class statusSort {
public:
	//重点
    bool operator() (const status &a, const status &b) const {
        if(a.left!=b.left)return a.left<b.left;
        else return a.right<b.right;
    }
};
    bool canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        stack<status>s;
        set<status,statusSort>st;
        s.push(status{0,0});
        while(!s.empty()){
            status temp=s.top();
            s.pop();
            int l=temp.left,r=temp.right;
            if(l==targetCapacity||r==targetCapacity||(l+r)==targetCapacity)return true;
            set<status,statusSort>::iterator it=st.find(temp);
            if(it!=st.end())continue;
            st.insert(temp);
            s.push(status{jug1Capacity,r});
            s.push(status{l,jug2Capacity});
            s.push(status{l,0});
            s.push(status{0,r});
            s.push(status{l-min(l,jug2Capacity-r),r+min(l,jug2Capacity-r)});
            s.push(status{l+min(r,jug1Capacity-l),r-min(r,jug1Capacity-l)});

        }
        return false;
    }
};
### C++ `std::set` 中 `find` 函数的使用 在 C++ 的标准库中,`std::set` 是一种关联容器,用于存储唯一的键并按升序排列。为了高效地查找某个元素是否存在,`std::set` 提供了一个成员函数 `find()`。该函数的时间复杂度为 O(log n),因为它基于平衡二叉树实现。 以下是 `std::set` 中 `find` 函数的具体用法: #### 基本语法 ```cpp iterator set_name.find(const key_type& key); const_iterator set_name.find(const key_type& key) const; ``` - 如果找到指定的关键字,则返回指向关键字的迭代器。 - 如果未找到指定的关键字,则返回 `end()` 迭代器。 #### 示例代码 下面是一个完整的例子展示如何使用 `std::set` 的 `find` 方法: ```cpp #include <iostream> #include <set> int main() { std::set<int> mySet = {10, 20, 30, 40, 50}; int searchValue = 30; // 使用 find 查找元素 auto it = mySet.find(searchValue); if (it != mySet.end()) { std::cout << "Element found: " << *it << std::endl; // 输出 Element found: 30 } else { std::cout << "Element not found." << std::endl; } return 0; } ``` 在这个例子中,如果集合中存在值为 `searchValue` 的元素,则会打印出对应的值;否则,提示找不到该元素[^1]。 #### 自定义类型的查找 当 `std::set` 存储自定义类型时,需要提供一个比较函数对象以便正确排序和查找。例如: ```cpp #include <iostream> #include <set> #include <string> struct Person { std::string name; int age; bool operator<(const Person& other) const { return age < other.age; // 按年龄排序 } }; int main() { std::set<Person> people = {{ "Alice", 30 }, { "Bob", 25 }, { "Charlie", 35 }}; Person target = {"", 30}; // 只关心年龄字段 auto it = people.find(target); // 找到第一个age等于30的人 if (it != people.end()) { std::cout << "Found person with age " << it->age << ": " << it->name << std::endl; } else { std::cout << "Person not found." << std::endl; } return 0; } ``` 在此示例中,通过重载 `<` 运算符实现了对结构体 `Person` 的排序逻辑,并成功调用了 `find` 来定位目标元素[^2]。 #### 注意事项 - 对于 `unordered_set` 或其他无序容器,由于它们不支持有序操作,因此无法直接应用类似的 `find` 方法来执行高效的查找[^3]。 - 当涉及 Qt 库中的某些组件(如 QTcpServer 和 QListWidget),虽然也可以完成数据管理任务,但这属于 GUI 编程范畴而非 STL 容器讨论范围[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值