2011-07-25 9 views
3

これは最後の一種です。キーに基づく2つのマップの値を組み合わせる

私は2つのマップを持っています。 dictの= {A:-3.1、2.1、1.1}最初のマップの

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

内容は次のように見えます。 {B:-4.5,5.6,7.2} ...

2番目のマップの文字列は、最初のマップの文字列と同じです。 Dict1 = {A:B}; ...

私のようなものを作成する必要があります。技術的に..しかしDict1の構造を再構築する可能性を持つ、

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

または2つのベクトルでの場所にそれらをこれらはいくつかの点の座標です。私は実際にここで.. 2番目のルートを行って、二つのベクトルを作成しようとしましたし、それらを一致させる、しかし、どうやら私はいくつかのミスを犯した

は私が持っているものです。

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

ルックアップをどのように実行する必要があるか説明できますか?おそらく2つの 'std :: map'の代わりに2つの' boost.bimap'を使うと、あなたの問題はすでに解決されています。 –

答えて

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

関連する問題