基于键合并两个映射的值

3
这有点是救命稻草。
我有两张地图。
typedef std::map <string, vector<float> > Dict;
typedef std::map <string, string> Dict1;

第一个地图的内容如下: Dict = {A: -3.1, 2.1, 1.1}; {B: -4.5, 5.6, 7.2}...

第二个地图中的字符串与第一个地图中的键相同。 Dict1 = {A: B};...

我需要创建类似以下内容的东西:

Dict2 = {-3.1, 2.1, 1.1:  -4.5, 5.6, 7.2}... 

或者将它们放在两个向量中,但要有重构Dict1结构的可能。从技术上讲,这些是一些点的坐标。
我实际上选择了第二种方法,尝试创建两个向量,然后进行匹配,但显然我犯了一些错误。以下是我的尝试结果:
typedef std::map <string, vector<float> > Dict;
typedef std::map <string, string> Dict1;

typedef std::vector<float> V1;

V1 v1;
V1 v2;

Dict d;
Dict d1;


//Here is the code, I know, oh well...



for( map<string, vector<float> >::iterator iter0 = d.begin(); iter0 != d.end(); ++iter0 ) {

    for( map<string, string >::iterator iter1 = d1.begin(); iter1 != d1.end(); ++iter1 ) {

        vector <float> tempVal0 = (*iter0).second;
        string tempKey0 = (*iter0).first;

        string tempVal1 = (*iter1).second; 
        string tempKey1 = (*iter1).first;

        size_t comp1 = tempKey0.compare(tempKey1);
        if(comp1 == 0 ){
            for (unsigned i = 2; i < tempVal0.size(); i++) {
            v1.push_back(tempVal0[i-2]);
            v1.push_back(tempVal0[i-1]);
            v1.push_back(tempVal0[i]);

                for( map<string, vector<float> >::iterator iter00 = d.begin(); iter00 != d.end(); ++iter00 ) {

                    for( map<string, string >::iterator iter11 = d1.begin(); iter11 != d1.end(); ++iter11 ) {
                        vector <float> tempVal00 = (*iter00).second;
                        string tempKey00 = (*iter00).first;

                        string tempVal11 = (*iter11).second; 
                        string tempKey11 = (*iter11).first;

                        size_t comp2 = tempVal1.compare(tempKey00);
                        if (comp2 == 0){
                            for (unsigned i = 2; i < tempVal00.size(); i++) {
                                v2.push_back(tempVal00[i-2]);
                                v2.push_back(tempVal00[i-1]);
                                v2.push_back(tempVal00[i]);
                            }
                        }

                    }   
                    }     

            }
        }


    }
}

我错过了什么?

你能解释一下你需要执行查找的方式吗?也许使用两个 boost.bimap 而不是两个 std::map 已经可以解决你的问题了。 - Kerrek SB
1个回答

3
std::map<string, vector<float>> d;
std::map<string, string> d1;
std::map<vector<float>, vector<float>> d2;

// Fill the maps here

for(std::map<string, string>::iterator i = d1.begin(); i != d1.end(); i++) {
    d2[d[i->first]] = d[i->second];
}

如果您具备基本的C++标准库工作知识,这是一个相当简单的操作。但我不确定您打算如何比较一个浮点数向量。默认情况下,C++没有针对浮点数向量的比较器。


为您修复了C++0x模板的闭合括号和一个缺失的冒号;-) - rubenvb
只是试图避免在针对当前普通的“c ++”(请参见标签)的答案中出现非法语法,但是嘿,我是谁呢 :) ... - rubenvb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接