注意:答案是按照特定的顺序给出的,但由于许多用户根据投票而不是给出的时间对答案进行排序,因此这里是答案的索引,它们是最有意义的顺序:
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
(注意:tuple并且tie可以从Boost或C++ 11中获取.)
当编写只包含两个元素的小结构时,我有时倾向于选择a std::pair,因为所有重要的东西都已经为该数据类型完成,例如operator<严格弱序.
但缺点是几乎没用的变量名.即使我自己创造了这个typedef,我也不会记得2天后究竟是什么first,second究竟是什么,特别是如果它们都属于同一类型.这对两个以上的成员来说更糟糕,因为嵌套pair非常糟糕.
另一种选择是tuple,无论是来自Boost还是C++ 11,但它看起来并没有更好,更清晰.所以我回去自己编写结构,包括任何需要的比较运算符.
特别是因为这operator<可能非常繁琐,我想通过依赖为tuple以下定义的操作来绕过这整个混乱:
示例operator<,例如严格弱排序:
bool operator<(MyStruct const& lhs, MyStruct const& rhs){
return std::tie(lhs.one_member, lhs.another, lhs.yet_more) <
std::tie(rhs.one_member, rhs.another, rhs.yet_more);
}
Run Code Online (Sandbox Code Playgroud)
(从传递的参数中引用tie一个引用.) tupleT&
编辑:从@DeadMG到私有继承的建议tuple并不是很糟糕,但它有一些缺点:
operator=特别是)可以轻松绕过tie解决方案,如果他们对订购无关紧要,我可以省略某些成员我需要考虑这个实现有什么缺点吗?
比较以下结构的两个实例,我收到一个错误:
struct MyStruct1 {
Position(const MyStruct2 &_my_struct_2, const int _an_int = -1) :
my_struct_2(_my_struct_2),
an_int(_an_int)
{}
std::string toString() const;
MyStruct2 my_struct_2;
int an_int;
};
Run Code Online (Sandbox Code Playgroud)
错误是:
错误C2678:二进制'==':找不到哪个运算符带有'myproj :: MyStruct1'类型的左手操作数(或者没有可接受的转换)
为什么?
以下程序是使用VC++ 2012编译的.
#include <algorithm>
struct A
{
A()
: a()
{}
bool operator <(const A& other) const
{
return a <= other.a;
}
int a;
};
int main()
{
A coll[8];
std::sort(&coll[0], &coll[8]); // Crash!!!
}
Run Code Online (Sandbox Code Playgroud)
如果我return a <= other.a;改为return a < other.a;那么程序按预期运行,没有例外.
为什么?
我有一组3个整数的元组,我不想要任何重复.也就是说,我不希望2个条目具有相同的3个值.
这是我的代码.
struct Key{
unsigned a;
unsigned b;
unsigned c;
public:
Key(unsigned _a, unsigned _b, unsigned _c) :
a(_a),
b(_b),
c(_c) {}
bool operator<(const Key& rhs) const
{
if (a < rhs.a) {
return true;
}
if (b < rhs.b) {
return true;
}
if (c < rhs.c) {
return true;
}
return false;
};
};
std::set<Key> myset;
Run Code Online (Sandbox Code Playgroud)
但我myset有时会看到重复.我无法准确捕捉到什么序列导致添加重复条目.它并不总是发生.我的问题是,我的operator<功能有什么本质上的错误吗?
我如何使用以下结构:
struct point
{
int x;
int y;
int z;
};
Run Code Online (Sandbox Code Playgroud)
作为关键std::map<point, bool>?我该如何定义operator<两点?
在尝试提高我的算法技能时,我发现自己陷入了以下问题,简而言之,要求您找到时间段的持续时间,其中房间中有最多人数:
https://jutge.org/problems/P27158_en
我提出的解决方案正确解决了网站建议的所有公共测试用例的问题,但是对于一个或多个隐藏的私有测试用例失败了.
我的解决方案为std :: vector中的每个事件保存了两个条目:一个用于到达,一个用于离开,每个条目由[eventtype,eventtime]组成.然后按事件时间对向量进行排序,最后循环遍历向量以确定有最大访客数的时间跨度的持续时间.我的代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
enum class EventType {ARRIVE, LEAVE};
struct Event{
int time;
EventType type;
Event(){}
Event(int tm, EventType t) : time(tm), type (t){}
// inline bool operator<(const Event& e) const {return time < e.time || (time == e.time && type==EventType::LEAVE);}
};
bool eventCompare(const Event& e1, const Event& e2) {
if(e1.time < e2.time){
return true;
} else if(e1.time == e2.time) {
if(e1.type == EventType::LEAVE && e2.type == EventType::ARRIVE) { …Run Code Online (Sandbox Code Playgroud) 在C++中默认情况下都std::set与std::multiset有std::less<T>作为他们的比较.任何人都可以解释如何std::multiset允许重复,std::set不是吗?
使用函数时<algorithm>,通常会有一个额外的参数来自定义比较.但我不太了解有关参数的描述(set_intersection的文档).
二进制函数,它接受输入迭代器指向的两个类型的参数,并返回一个可转换为bool的值.返回的值指示第一个参数是否在其定义的特定严格弱顺序中被认为是在第二个参数之前.该函数不得修改其任何参数.这可以是函数指针或函数对象.
它描述了该函数应该返回两个参数的顺序.但是在匹配函数中呢,例如:
#include <algorithm>
#include <iostream>
using namespace std;
void print (const char* name, int* start, int* end) {
cout << name << ": ";
while (start < end)
cout << *start++ << ", ";
cout << endl;
}
bool func1 (int a, int b) { return a==b; }
bool func2 (int a, int b) { return a+b == 8; }
int main() {
int set1[6] = {0, 1, 2, 4, 2, 4};
int set2[6] …Run Code Online (Sandbox Code Playgroud) 我已经宣布了这样的结构 - >
struct data{
int x,y;
bool operator < (const data& other) {
return x<other.x or y<other.y;
}
};
Run Code Online (Sandbox Code Playgroud)
现在我希望map它作为一个关键并具有bool价值.
int main()
{
data a;
map<data,bool>mp;
a.x=12, a.y=24;
mp[a]=true;
}
Run Code Online (Sandbox Code Playgroud)
最后一行给了我这个错误 - >
error: passing 'const' as 'this' argument of 'bool data::operator<(const data&)' discards qualifiers
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题 ??