Explanation for behavior

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;
}
 
R

red floyd

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?

Because you're not storing a reference to the list in the map. You're
storing a copy of the list in the map. You can't store references in
Standard Library containers.

[code retained for reference purposes]
#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;
}
 
V

Victor Bazarov

I have the below code for printing out the contents of a map.

Beyond the use of non-standard 'itoa', the code is OK (not perfect,
but OK).
The output prints the strings contained in the value for each key in
the map.
Yes.

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.

No, you're not storing the reference. The 'map' stores a copy of the
list.
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?

Because the map contains copies of the list. Every time you assign
your list to 'tmap', the list is _copied_ into the one contained
within the map.
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;
}
 
R

rogerhillster

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?

Because you're not storing a reference to the list in the map. You're
storing a copy of the list in the map. You can't store references in
Standard Library containers.

[code retained for reference purposes]




#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;
}- Hide quoted text -


- Show quoted text -


Floyd,

I thought that Vector containers store objects by reference.
Am I wrong on that?

-Roger.
 
K

Kai-Uwe Bux

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.

Nope. You store a copy of the list (with the value at that time).

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?
[code snipped]


Best

Kai-Uwe Bux
 
V

Victor Bazarov

[..]
I thought that Vector containers store objects by reference.
Am I wrong on that?

What are "Vector containers"? Standard containers (map, vector,
list, deque, set) store *copies*. What book are you reading that
caused you to think that standard containers stored references
(if that's what you thought)?

V
 
K

Kai-Uwe Bux

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?

Because you're not storing a reference to the list in the map. You're
storing a copy of the list in the map. You can't store references in
Standard Library containers.

[code retained for reference purposes]




#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;
}- Hide quoted text -


- Show quoted text -


Floyd,

I thought that Vector containers store objects by reference.
Am I wrong on that?


Yes, you are wrong.

Indeed, all standard containers store objects by value and make copies of
whatever you push into them. If you need reference semantics for type T
objects in containers, you need to use container<T*> or (as some would
prefer) container< some_smart_pointer<T> > to introduce the necessary level
of indirection.


Best

Kai-Uwe Bux
 
R

rogerhillster

[..]
I thought that Vector containers store objects by reference.
Am I wrong on that?

What are "Vector containers"? Standard containers (map, vector,
list, deque, set) store *copies*. What book are you reading that
caused you to think that standard containers stored references
(if that's what you thought)?

V

My bad!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top