map won't print output

Hi my iterator for my map won't actually print the output for some reason,any idea how to fix this and why it won't print?

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{



   map<string,string> name;
   map<string,string>::iterator mitr = name.begin();

   name["adam"];
   name["mike"];

   for(mitr;mitr!=name.end();mitr++){

       cout << mitr->first << endl;
       cout << mitr->second << endl;
   }

}
closed account (48bpfSEw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()
{
   map<string,string> mapName;
   
   mapName["adam"] = "surename1";
   mapName["mike"] = "surename2";

   for(map<string,string>::iterator it = mapName.begin(); it != mapName.end(); ++it) {
       cout << it->first << endl;
       cout << it->second << endl;
   }

  return 0;
}
What output are you getting and what do you expect to get?
I don't get any output is the problem,just stays blank

I expected it to print name then adam

but I think Necip solved the problem I think I left them empty
I just changed the code to pretty much what Necips code did but it still prints blank it does not output anything :s

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



#include <iostream>
#include <vector>
#include <map>
#include <string>

using namespace std;

int main()
{



   map<string,string> name;
   map<string,string>::iterator mitr = name.begin();

   name["adam"] = "adam";
   name["mike"] = "mike";



   for(mitr;mitr!=name.end();mitr++){

       cout << mitr->first << endl;
       cout << mitr->second << endl;
   }

}


Last edited on
Line 4: You're missing the <string> header.

line 13: At the time you're setting mitr here, the map is empty. i.e. mitr = name.end()

Line 18: Change your for loop:
 
  for (mitr=name.begin(); mitr!=name.end(); mitr++)  // now begin() is valid  



thanks that solved it

line 13: At the time you're setting mitr here, the map is empty. i.e. mitr = name.end()


It's crazy how much difference that makes,but yeah that makes a lot of sense,I didn't think it would have mattered.
Topic archived. No new replies allowed.