2011-07-25 35 views
3

这是最后的手段..基于密钥合并两个地图的值

所以我有两个地图。

typedef std::map <string, vector<float> > Dict; 
typedef std::map <string, string> Dict1; 

第一地图看起来像这样的内容: 字典= {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]); 
          } 
         } 

        } 
        }  

      } 
     } 


    } 
} 

我在想什么?

+0

你能解释一下你需要用哪种方式执行查找吗?也许使用两个'boost.bimap's而不是两个'std :: map'就可以解决你的问题。 –

回答

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++没有针对浮点向量的比较器。

+0

修复了C++ 0x模板右大括号和缺少的冒号;-) – rubenvb

+0

很满意我的C++ 0x模板关闭,谢谢。 – Puppy

+0

只是试图保持针对当前纯C++的答案非法语法(见标签),但嘿,我是谁:)) – rubenvb