Translating python dictionary to C++

前端 未结 7 1059
-上瘾入骨i
-上瘾入骨i 2020-11-30 00:57

I have python code that contains the following code.

d = {}

d[(0,0)] = 0
d[(1,2)] = 1
d[(2,1)] = 2
d[(2,3)] = 3
d[(3,2)] = 4

for (i,j) in d:
    print d[(         


        
7条回答
  •  难免孤独
    2020-11-30 01:10

    The type is

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

    The code to add entries to map is like here:

    typedef  std::map< std::pair, int> container;
    
    container m;
    
    m[ make_pair(1,2) ] = 3; //...
    
    for(container::iterator i = m.begin();  i != m.end(); ++i){
       std::cout << i.second << ' '; 
       // not really sure how to translate [i,j] [j,i] idiom here easily
    }
    

提交回复
热议问题