C++ How to create a map from two other maps -


how create map mymap3 as:

std::map< vector<std::pair<int, int>>, int > mymap3 

example:

 mymap3[0] = ((1,3) , (1,5), 7 ) // 5 = 4 + 3  mymap3[1] = ( (2,1) , (2,4), 6 ) // 6 = 1 + 5 

where key = (vector of pairs) extract 'mymap1',

and value = sum of pairs' values in 'mymap2'.

i don't care pair order, (2,4) same (4,2)

std::map <int, vector<pair<int, int>> > mymap1; std::map< std::pair< int, int>, int>  mymap2; 

here example:

mymap1[0] = (0, (1,3) , (1,5) ) mymap1[1] = (1, (2,1) , (2,4) )  mymap2[0] = ( (1,3) , 4 ) mymap2[1] = ( (1,5) , 3 ) mymap2[2] = ( (2,1) , 1 ) mymap2[3] = ( (4,2) , 5 ) 

i don't know how that, , try:

    std::map <int, vector<pair<int, int>> > ::iterator it1 = mymap1.begin();     std::map<std::pair< int, int>, int> ::iterator it2 = mymap2.begin(); //  std::map< vector<pair<int, int> >, int> mymap3;     std::map< pair<int, int> , int> mymap3;      int = 0;      while (it1 != mymap1.end())     {         vector< pair<int, int> > temp = mymap1[i];         vector< pair<int, int> > ::iterator it3 = temp.begin();          while (it3 != temp.end())         {             int = it2->first.first;             int b = it2->first.second;               while (it2 != mymap2.end())             {                 if (it3 == mymap2.find(std::make_pair(a, b))                     mymap3[std::make_pair(a, b)] += it2->second;                  else                     ++it2;             }             ++it3;             ++i;         }         ++it1;     } 

i have syntax error in:

if (it3 == mymap2.find(make_pair(a, b)))     mymap3[std::make_pair(a, b)] += it2->second; 

your question confuse, decipher want each key in map1 sum value of each pair in vector found in map2 single value, , store result map1 key. not code do…

example result : http://coliru.stacked-crooked.com/a/a4d77c77fa6896c1

#include <iostream> #include <string> #include <vector>  #include <map> #include <utility>  using map1 = std::map< int, std::vector<std::pair<int,int>>>; using map2 = std::map< std::pair<int,int>, int >; using map3 = std::map< int, int >;  map3 foo( map1 const & m1, map2 const & m2 ) {     auto m2end = m2.end(); // constant call move outside loop     map3 result;     for( auto & v1 : m1 ) { // each entry in m1 made of key first , value second         for( auto & p1 : v1.second ) { // iterate on vector of pair             auto v2it = m2.find( p1 ); // search pair             if ( v2it != m2end ) {                 result[v1.first] += v2it->second; // if pair found, add value result, using map1 key key             }         }     }     return result; }  template<typename t,typename s> std::ostream& operator<<(std::ostream& os, const std::pair<t,s>& pair ) {     os << "{ " << pair.first << ", " << pair.second << " }";     return os; } template<typename t> std::ostream& operator<<(std::ostream& os, const std::vector<t>& vec) {     os << "( ";     (auto& el : vec)     {         os << el << ", ";     }     os << " )";     return os; }  template<typename t,typename s> std::ostream& operator<<(std::ostream& os, const std::map<t,s>& map) {     (auto& el : map)     {         os << el.first << " : " << el.second << "\n";     }     return os; }  int main() {     map1 m1 { { 0, { {1,3}, {1,5}} }, { 1, { {2,1}, {2,4}} }, };     map2 m2 { { {1,3}, 4 }, { {1,5}, 3 }, { {2,1}, 1 }, { {2,4}, 5 }, };      auto m3 = foo( m1, m2);      std::cout << "m1 :\n" << m1 << std::endl;     std::cout << "m2 :\n" << m2 << std::endl;     std::cout << "m3 :\n" << m3 << std::endl; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -