class Example
{
private:
string one;
string two;
string three;
string four;
string five;
public:
inline Example (string a_one,string a_two, string a_four, string a_five) :
one(a_one),two(a_two),three(a_three),four(a_four),five(a_five)
{}
inline bool operator == (const Example& other) const
{
if (one == other.one)
{
if (two == other.two)
{
if (three == other.three)
{
if (four == other.four)
{
if (five == other.five)
{
return true;
}
}
}
}
}
return false;
}
inline bool operator < (const Example& other) const
{
if (one < other.one)
{
return true;
}
else if (two < other.two)
{
return true;
}
else if (three < other.three)
{
return true ;
}
else if (four < other.four)
{
return true;
}
else if (five < other.five)
{
return true;
}
else
{
return false;
}
}
}
void CountExample(Example& example,std::map<Example,int>& counters);
void main()
{
std::map<Example,int> counters;
std::list<Example> examples = GetExamples();
//GetExamples defined elsewhere, and initializes examples with a long list of instances of Example
std::list<Example>::const_iterator Iter;
for (Iter = examples.begin();Iter != examples.end();Iter++)
{
CountExample(*Iter);
}
PrintCounters(counters);//PrintCounters defined elsewhere and prints the map to a file
}
void CountExample(Example& example,std::map<Example,int>& counters)
{
std::map<Example,int>::const_iterator Iter;
Iter = counters.find(example);
if (Iter ==counters.end()) //means the specific Example is not in the map
{
counters.insert(std::pair<Example,int>(example,1));
}
else
{
counters[example] += 1;
{
}
答案 0 :(得分:2)
如果你有一个相当现代的编译器,那么这个比较阶梯可以用两个std::tie()
'元组之间的单一比较代替:
#include <tuple>
...
bool operator== (const Example& other) const
{
return std::tie(one, two, three, four, five)
== std::tie(other.one, other.two, other.three, other.four, other.five);
}
bool operator < (const Example& other) const
{
return std::tie(one, two, three, four, five)
< std::tie(other.one, other.two, other.three, other.four, other.five);
}
顺便说一句,使用std::multiset
计算特定元素存储在关联容器中的次数可能更简单,这可以将CountExample
简化为单行
void CountExample(const Example& example, std::multiset<Example>& counters)
{
counters.insert(example);
}
虽然打印变得有点棘手:
void PrintCounters(const std::multiset<Example>& counters)
{
for(auto i=counters.begin(); i!=counters.end(); i = counters.upper_bound(*i))
std::cout << *i << ":" << counters.count(*i) << '\n';
}
对ideone进行测试:http://ideone.com/uA7ao
答案 1 :(得分:1)
要与多个元素进行比较,您比较的每个元素都将具有三个结果:小于,大于或等效。您必须考虑所有这些情况。
bool LessThan(const MyClass & left, const MyClass right)
{
if (left.one < right.one)
return true;
else if (right.one < left.one)
return false;
// equivalent in one
if (left.two < right.two)
return true;
else if (right.two < left.two)
return false;
// equivalent in one and two
...
return false;
}
答案 2 :(得分:0)
编辑:在考虑了这个问题之后,我决定删除我的旧答案,因为它似乎与当前遇到的问题无关。您的operator<
方法似乎确实满足严格弱排序的要求,因此我认为问题出在其他地方,所以我将在下面留下以下备用解决方案......
您似乎在为地图创建总订单时遇到问题,因此您可能希望将std::unordered_map
作为替代方案,直接应用operator==
来检测相等性,而不是使用你的operator<
用于严格的弱排序......你必须为你的类提供一个哈希函数,否则使用基于哈希表的std::unordered_map
容器非常简单。
答案 3 :(得分:0)
您需要为您输入operator<
。编写这可能非常繁琐,但您只需使用Boost.Tuple
即可 - 这样,元组处理比较,使代码更易于阅读,编写和理解。
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <string>
struct Object
{
std::string a;
std::string b;
std::string c;
};
bool operator<(const Object& obj1, const Object& obj2)
{
return (boost::tie(obj1.a, obj1.b, obj1.c) <
boost::tie(obj2.a, obj2.b, obj2.c));
}