按键进行多图智能排序

时间:2014-07-06 23:01:45

标签: c++ sorting multimap accumulate

我一直在研究这个问题。我有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中有2个......依此类推。

我的代码(下面)错误地产生了这个结果:

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();

2 个答案:

答案 0 :(得分:2)

因此。我们完全不清楚你为什么要用这种复杂的数据结构来伤害自己。你有两个部分的钥匙。因此,让我们为您的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

但是我假设键应该全部正确排序,这就是我的输出。

答案 1 :(得分:0)

我没有在您的问题描述中看到任何需要多图的内容。只需使用std::map代替。

另外,摆脱y_z。每次使用它时,您似乎都应该使用x_y_z[x]

相关问题