我已经有一段时间在研究这个问题了。我有3个.txt文件,其中包含一些可以读入并在多张地图中排列的内容,
std::multimap<std::string, std::multimap<int, int>>
但是,如果键已经存在,则将值增加;如果键是唯一的,则将值放置在地图中。
3个.txt文件(标记为“ x”,“ y”和“ z”)包含以下内容:
“ x.txt”包含主键:
a a a b b b c c c d d d e e e
“ y.txt”包含辅助键:
1 2 2 3 4 4 5 4 4 2 6 6 6 6 6
计算辅助键的“ z.txt”包含:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
所需的输出是这样的:
1 : 1
2 : 2
3 : 1
4 : 2
5 : 1
4 : 2
2 : 1
6 : 2
6 : 3
指出a中有一个1,a中有两个2,b中有一个3,b中有两个4 ...依此类推。
我的代码(如下)错误地产生了以下结果:
1 : 1
2 : 1
2 : 1
2 : 1
3 : 1
4 : 1
4 : 1
5 : 1
6 : 1
6 : 1
6 : 1
这是代码:有什么建议吗?
//needed to output your result (print map)
template<typename a, typename b>
void print1(const std::multimap<a, b>& thing){
for (std::multimap<a,b>::const_iterator it = begin(thing); it != thing.end(); it++){
std::cout << it->first << " : " << it->second << std::endl;
}
}
std::ifstream X("x.txt");
std::ifstream Y("y.txt");
std::ifstream Z("z.txt");
typedef std::multimap<int, int> Y_Z;
typedef std::multimap<std::string, Y_Z> X_Y_Z;
typedef std::pair<int, int> pair_y_z;
typedef std::pair<std::string, Y_Z> pair_x_y_z;
Y_Z y_z;
X_Y_Z x_y_z;
std::string x_;
int y_;
int z_;
if (X){
while (X >> x_, Y >> y_, Z >> z_){
if (x_y_z.empty()) {
y_z.insert(pair_y_z(y_, z_));
x_y_z.insert(pair_x_y_z(x_, y_z));
}
else {
X_Y_Z::iterator first_iter = x_y_z.find(x_);
X_Y_Z::iterator last_iter = x_y_z.upper_bound(x_);
if (x_y_z.find(x_) == x_y_z.end()) {
y_z.insert(pair_y_z(y_, z_));
x_y_z.insert(pair_x_y_z(x_, y_z));
}
else{
for (; first_iter != last_iter; first_iter++){
if (x_y_z.find(x_)->second.find(y_) == x_y_z.find(x_)->second.end()){
//no match
y_z.insert(pair_y_z(y_, z_));
x_y_z.insert(pair_x_y_z(x_, y_z));
break;
}
else{
//found match
x_y_z.find(x_)->second.find(y_)->second += z_;
break;
}
}
}
}
}
}
std::cin.get();
print1(y_z);
std::cin.get();
最佳答案
所以。完全不清楚为什么要使用这种复杂的数据结构来伤害自己。您有一个包含两个部分的密钥。因此,让我们将std::map
的密钥设为std::pair<T1, T2>
。
如果这样做,代码将变得非常简单。
源代码:
#include <iostream>
#include <fstream>
#include <utility>
#include <map>
int main() {
std::ifstream X("x.txt");
std::ifstream Y("y.txt");
std::ifstream Z("z.txt");
std::map<std::pair<std::string, int>, int> data;
std::string x_;
int y_;
int z_;
while (X >> x_, Y >> y_, Z >> z_)
data[std::make_pair(x_, y_)] += z_;
for (auto const & element : data)
std::cout << std::get<1>(element.first) << " : " << element.second << "\n";
}
输出:
1 : 1
2 : 2
3 : 1
4 : 2
4 : 2
5 : 1
2 : 1
6 : 2
6 : 3
进一步的问题:
我的输出与期望的输出之间存在差异,但是我认为这是您期望的错误。
您当前的输出预期:
{a, 1} : 1
{a, 2} : 2
{b, 3} : 1
{b, 4} : 2
{c, 5} : 1 <--- note that this key is out of order with the next one. 5 > 4.
{c, 4} : 2
{d, 2} : 1
{d, 6} : 2
{e, 6} : 3
但是我假设所有的键都应该正确排序,这就是我的输出所做的。