iterators for use in maps

Hi,


I RTFM about iterators, could you please tell me the purpose behind:

pair<map<Person, string>::iterator, bool> pr = book.insert(entry);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Add a new entry to a phone book
void addEntry(map < Person, string > & book)
{
	pair <Person, string> entry;		//Stores a phone book entry
	string number;
	Person person = getPerson();

	cout << "Enter the phone number for "
		<< person.getName() << ": ";
	getline(cin, number);
	entry = make_pair(person, number);
	pair<map<Person, string>::iterator, bool> pr = book.insert(entry);

	if(pr.second)
		cout << "Entry successful." << endl;
	else
	{
		cout << "Entry exists for " << person.getName()
			<< ". The number is " << pr.first->second << endl;
	}
}


thanks in advance for your help
Take a look at the documentation of the return value for the std::map::insert member function.
http://cplusplus.com/reference/stl/map/insert/
thanks, but my purpose is to understand the meaning of iterator in
pair<map<Person, string>::iterator, bool> pr = book.insert(entry);
I think it has something to do with insertion point, but what is bool used for?
Last edited on
The whole pair thing is a trick for the insert function to return 2 values:

- one value is an iterator which points to the value you just inserted

- the other value is a bool which is set to false if you tried to insert something with a key that already existed.


So pretty much if you try to insert something that already exists, the function will return false for the bool, and will return the iterator of the element that already exists so you can see how it exists in the map.

Here's some comments for the code you posted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	pair<map<Person, string>::iterator, bool> pr = book.insert(entry);
	/* now, 'pr.first' is an iterator which points to the element in the map
	   and 'pr.second' is true/false depending on whether or not the element
	   already existed */

	if(pr.second)   // see if the desired key (Person) already existed in our map
		cout << "Entry successful." << endl;  // if not, we added them OK
	else  // if they already existed
	{
		//  this code basically is like "this person already exists in the map..
		//    here is their information"
		cout << "Entry exists for " << person.getName()
			<< ". The number is " << pr.first->second << endl;
	}
Disch,
Learning the STL is difficult because of the manual I'm using. I think I understand it a little better now. I looked at what iterator does differently than what it's for.

thanks for your help.
An iterator is just a way to "step through" a container. vectors, maps, lists, etc all have iterators.

You can think of an iterator as a way to access an individual element in the container. For example if you have a vector<int>, then a vector<int>::iterator would refer to a single int within that vector.
Disch,
Oh, I see. Hence the word iterator. So the step-through process searches for the first occurance of int in the example, vector<int>::iterator?

thanks.
Last edited on
So the step-through process searches for the first occurance of int in the example, vector<int>::iterator?


Well it doesn't really "search" for anything.

you use your vector's (or list's or map's or whatever) begin() and end() functions as starting points and ending points for the elements in the container. You then step through the entire container by incrementing the iterator:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// make a list
//  with elements: 10, 20, 15
list<int> mylist;
mylist.push_back(10);
mylist.push_back(20);
mylist.push_back(15);

// now let's say we want to print the elements of this list.  We can step through the
//  list and print them one by one.  We do this by using iterators:

list<int>::iterator i;
for(i = mylist.begin(); i != mylist.end(); ++i)
{
  cout << *i << "\n";
}


Iterators are similar in concept to pointers. An iterator "points" to one element in the container, and when you increment it, it points to the next element in the container.
Disch,
that example you used for make a list really helped. It mentioned iterators are similiar to pointers in the book, but offered no examples. Thanks for taking the time to generate those examples.

thank you :)
Last edited on
Conceptually, a pointer is an iterator but an iterator is not necessarily a pointer. Just like the old square is a rectangle thing. I always find that kind of relationship interesting.
On the topic of containers, is using a container better at sorting than using an array. Because to sort an array you need to index each value. Is this correct? I know that shifting arrays would not be logical, you would have to use some type of swap function to do this.
Last edited on
It depends on the container.

sets and maps are already sorted

vectors basically are just arrays (but resizable, etc)
Dear sirs,
In the book I'm using, I started to lose my understanding in the large amount of information crammed into one chapter. This book has a horrible way of explaining the STL or I'm lost in the complexity, I'm using Ivor Horton's beginning visual C++ 2008. Is there a better set of books I should be reading. I don't mind stepping through material I know pretty well.

thank you much.
Last edited on
I ordered

"Thinking in C++: Introduction to Standard C++, Volume One (2nd Edition) (Vol 1)"
Bruce Eckel;

Topic archived. No new replies allowed.