R
rogerhillster
Hi,
I have the below code for printing out the contents of a map.
The output prints the strings contained in the value for each key in
the map.
In the first for loop, I am clearing the list contents and I am
storing the reference to the list in each of the map's entries.
Could somebody let me know as to why even after clearing the contents
of the list, I can see different strings contained in the map for each
key?
Thanks,
Roger.
#include <iostream>
#include <map>
#include <list>
#include <string>
using namespace std;
int main() {
typedef list<string> tslist;
tslist slist;
map<int, tslist> tmap;
int i = 0, j =0;
for (; i < 10; i++) {
slist.clear();
char str[10];
itoa(i, str, 10);
slist.push_front(string(str));
tmap = slist;
}
i = 0;
for (; i < 10; i++) {
tslist temp = tmap;
tslist::iterator iter = temp.begin();
while(iter!=temp.end()) {
string& temp = *iter;
cout << temp.c_str() << endl;
iter++;
}
}
return 0;
}
I have the below code for printing out the contents of a map.
The output prints the strings contained in the value for each key in
the map.
In the first for loop, I am clearing the list contents and I am
storing the reference to the list in each of the map's entries.
Could somebody let me know as to why even after clearing the contents
of the list, I can see different strings contained in the map for each
key?
Thanks,
Roger.
#include <iostream>
#include <map>
#include <list>
#include <string>
using namespace std;
int main() {
typedef list<string> tslist;
tslist slist;
map<int, tslist> tmap;
int i = 0, j =0;
for (; i < 10; i++) {
slist.clear();
char str[10];
itoa(i, str, 10);
slist.push_front(string(str));
tmap = slist;
}
i = 0;
for (; i < 10; i++) {
tslist temp = tmap;
tslist::iterator iter = temp.begin();
while(iter!=temp.end()) {
string& temp = *iter;
cout << temp.c_str() << endl;
iter++;
}
}
return 0;
}