copy contents of one map to another

I created two maps one initialized to be map<string,int>msi and the other to be map<int,string>mis, im having difficulties copying the contents of the msi map to the mis map, and also how would i find a better way of adding the intergers of map msi while using the map.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <map>
#include <string>
#include <cstdio>
#include "std_lib_facilities.h"


//--------------------------problem 5-------------------------// 
void output(map<string,int>&m,string s,int a)

{
m[s]=a;
}


int main()
{
//--------------------------problem 1-------------------------// 
map<string,int> msi;
//--------------------------problem 2-------------------------// 
msi["lecture"]=21;
msi["tom"]=31;
msi["tim"]=51;
msi["sue"]=11;
msi["jade"]=91;
msi["tammy"]=8;
msi["lorie"]=6;
msi["fransis"]=4;
msi["leanne"]=22;
msi["larry"]=10;

while (!msi.empty())
  {
  
//--------------------------problem 3-------------------------// 

     cout << msi.begin()->first << ", ";   // outputs elements
     cout << msi.begin()->second << endl;
     

//--------------------------problem 4-------------------------// 
     msi.erase(msi.begin());  //deletes elements
  }
  
cout<<"\nContents where sucessfully erased\n";

       
string s; 
int a;
int count=0;
int sum=0;

//--------------------------problem 6-------------------------//       
    
    cout<<"Enter 10 strings followed by an integer(space in between)\n";
    while (count<10&&cin>>s&&cin>>a) {
    sum=sum+a;
    output(msi,s,a);
    count++;
    
    
   } 
//--------------------------problem 7-------------------------//     
    
cout<<"Elements of map MSI are\n";  
    typedef map<string,int>::const_iterator Iter;
    for (Iter  p = msi.begin(); p!=msi.end(); ++p) 
    
    cout<<p->first << ": " << p->second << '\n';
    
        
//--------------------------problem 8-------------------------//
cout<<"\nThe sum of your entered integers are: "<<sum;

//--------------------------problem 9-------------------------// 
map<int,string> mis;
//--------------------------problem 10-------------------------//           
   

    
//--------------------------problem 11-------------------------//        
 typedef map<int,string>::const_iterator It;
    for (It p = mis.begin(); p!=mis.end(); ++p) 
    
    cout<<p->first << ": " << p->second << '\n';        
//----------------------------FIN-----------------------------//

return 0;
}
> I created two maps one initialized to be map<string,int>msi and the other to be map<int,string>mis,
> im having difficulties copying the contents of the msi map to the mis map

What do you mean by 'copying the contents of the msi map to the mis map'?
In a map<string,int>, the keys (string) are unique, the mapped data values (int) are not.
In the other map the int values (keys) are unique, the string values (mapped data) are not.


> how would i find a better way of adding the intergers of map msi while using the map.

Iterate though the map, accumulating the values as you go along. Something like:
1
2
3
4
std::map<std::string,int> msi ;
// populate the map
int sum = 0 ;
for( const auto& pair : msi )  sum += pair.second ;



Topic archived. No new replies allowed.