双映射插入问题
Double map insertion problem
我有一个stl::map,其中键定义为我定义的对象,以及int。地图的用途如下:我有一个特定物体的列表我想数一下有多少相同的物体。所以我把对象插入到映射中。如果对象已经存在于地图中,我就增加它的值(因此计数器)。对象定义了所有基本操作符。对象由5个字符串组成。==运算符定义为对所有5个字符串进行比较,在逻辑上在上下文中是有意义的。问题是运算符
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;
{
}
如果您有一个相当现代的编译器,那么可以用两个std::tie()
'd元组之间的单个比较来替换这个比较阶梯:
#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';
}
Test on ideone: http://ideone.com/uA7ao
要与多个元素进行比较,您比较的每个元素将具有三个结果:小于、大于或相等。你必须考虑到所有这些情况。
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;
}
您需要为您的类型提供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));
}
编辑:在对这个问题进行了更多的思考之后,我决定删除我的旧答案,因为它似乎与当前遇到的问题无关。您的operator<
方法似乎确实满足了严格弱排序的要求,所以我认为问题在于其他地方,所以我在下面留下以下替代解决方案…
似乎你在为你的地图创建总顺序时遇到了问题,所以你可能想看看std::unordered_map
作为一种替代方案,它将直接应用你的operator==
来检测相等性,而不是使用你的operator<
来进行严格的弱排序……您必须为您的类提供一个散列函数,但除此之外,基于散列表的std::unordered_map
容器的使用非常直接。
相关文章:
- 屏幕插入运算符<<的运算符过载问题
- 我想直接在结构中插入,但没有一种方法可以正确避免填充问题
- 插入操作的二叉搜索树代码问题
- 当索引位于末尾和空数组时,向数组插入元素时出现问题
- 在链表中的某个点插入时出现问题,C++中的继承函数
- 通过C ODB插入汉字时的问题
- 在我的Quadtree中插入点问题
- 自己的C++列表类实现(插入函数)出现问题
- 单个链表,节点插入最低到最高节点值的问题
- BST插入的这段代码有什么问题?
- 在二叉搜索树中插入时出现问题
- 通过 ODBC C++将带有日期时间的记录插入 SQL Server 2014 的问题
- 二叉搜索树插入数据问题
- 我的二叉搜索树插入逻辑有什么问题?
- 测试外壳,插入和快速的程序问题
- 二叉搜索树在插入功能方面有问题
- 尝试在链表程序的末尾进行更简单的插入.小问题需要帮助
- 在二叉树中插入/查找问题
- 我的插入排序代码有问题
- 在将结构插入设置C++时遇到问题