在集合 c 中找到一对

新手上路,请多包涵

如果我有一个包含成对 int 的集合,

 set<pair<int,int> > cells;

如何使用“查找”找到一对是否存在于集合中。我可以使用“查找”来设置一个值,但不能为一对设置。

我正在尝试,

  cells.insert(make_pair(1,1));
 set<int,int>::iterator it;
 it=cells.find(pair<int,int>(1,1));

error: no match for 'operator=' in 'it = cells.std::set<_Key, _Compare, _Alloc>::find<std::pair<int, int>, std::less<std::pair<int, int> >, std::allocator<std::pair<int, int> > >((*(const key_type*)(& std::pair<int, int>((* &1), (* &1)))))'|

有没有人有任何想法?谢谢!

原文由 Safwan Ull Karim 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 410
2 个回答

问题是您的集合是一组整数 std::pair<int,int> ,而不仅仅是 <int,int> 。更改以修复您的代码。如果您使用的是 c++11 或更高版本,则可以使用 auto 关键字。

 // Example program
#include <iostream>
#include <string>
#include <utility>
#include <set>

int main()
{
    std::pair<int,int> p1(1,0);
    std::pair<int,int> p2(2,1);
    std::set<std::pair<int,int>> s;
    s.insert(p1);
    s.insert(p2);
    auto it = s.find(p1);
    std::cout << it->first << "," << it->second <<std::endl;
}

原文由 Zafi 发布,翻译遵循 CC BY-SA 3.0 许可协议

关于用于 it 的类型似乎存在拼写错误/误解。你需要使用:

 std::set<std::pair<int,int>>::iterator it;

原文由 R Sahu 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题