在 std::set 中插入对不一致(无法识别 <pair>.second)
Inserting pair in std::set is inconsistent (doesn't recognize <pair>.second)
如果我添加一个条件,这段代码的性能会有所不同:
第一种情况:
#include<bits/stdc++.h>
using namespace std;
struct comp
{
bool operator()(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b)
{
return a.first>b.first;
}
};
int main()
{
set<pair<int,pair<int,int>>,comp> s;
auto d = s.insert({4,{6,10}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
d = s.insert({4,{0,4}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
}
输出:
4 6 10
4 6 10
第二种情况:(条件为 .second)
#include<bits/stdc++.h>
using namespace std;
struct comp
{
bool operator()(pair<int,pair<int,int> > a, pair<int,pair<int,int> > b)
{
if(a.first==b.first)
return a.second.first<b.second.first;
return a.first>b.first;
}
};
int main()
{
set<pair<int,pair<int,int>>,comp> s;
auto d = s.insert({4,{6,10}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
d = s.insert({4,{0,4}});
cout<<(d.first)->first<<" "<<(d.first)->second.first<<" "<<(d.first)->second.second<<endl;
}
输出:
4 6 10
4 0 4
为什么集合在第一种情况下不添加不同的对?我认为额外的条件只决定顺序,不区分元素。
您的第一个比较器仅考虑货币对的第一项。当您尝试插入第二对时,它被视为等于已插入的一对,因此没有插入。
相反,你会取回已插入到集合中的对象,这是预期的行为。
请记住,根据定义,一个集合只有特定对象的一个实例,您的比较器可以帮助它说明 2 个对象的相互比较情况。
相关文章:
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 模板类无法识别友元运算符
- 如何防止clang格式在流运算符调用之间添加换行符<<
- std::visit无法识别类型
- <<操作员在下面的行中工作
- 无法识别模板功能
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 如何在LLVM中dyn_cast以识别StoreInst?
- 从不同的附加依赖项中识别等同命名的函数
- C++出现控制台错误.我无法识别源代码的问题
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- Qt:"Q3DScatter"即使包含在内也无法识别
- 使用指针的类识别
- 如何静态识别动态堆分配?
- 为什么C++无法识别我的对象实例化?
- C++ 无法识别创建进程'telnet'
- 识别 2D 矢量C++中的位置