2011-07-25 36 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