Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the return value of map::begin() for an empty map?

Tags:

c++

map

stl

I use something like this:

map<string, Data>::iterator it  = mymap->begin();
map<string, Data>::iterator end = mymap->end();

while (it != end) {
    // do stuff
    ++it;
}

I was just wondering if this would work even if the map is empty. I couldn't find any information about the return of map::begin() if the map ist empty.

like image 949
HWende Avatar asked May 21 '12 08:05

HWende


People also ask

What is the use of return value in map?

Return value: It returns a collection view of all the values present in the map. [student 4, student 3, student 6, student 1, student 2, student 5] As you can see, the output of the above example is returning a collection view of values. So any change in the map will be reflected in the collection view automatically.

What is the use of map begin () function?

begin () function is used to return an iterator pointing to the first element of the map container. begin () function returns a bidirectional iterator to the first element of the container. mapname.begin () Parameters : No parameters are passed.

What is the use of map begin in C++?

map::begin () function is an inbuilt function in C++ STL, which is defined in header file. begin () is used to access the element which is at the very beginning of the associated map container. This function returns an iterator which points to the first element of the container.

What does map () return in Python?

Now, map () returns a map object, which is an iterator that yields items on demand. That’s why you need to call list () to create the desired list object. For another example, say you need to convert all the items in a list from a string to an integer number. To do that, you can use map () along with int () as follows:


1 Answers

If the map is empty, the begin and end iterators are equal, i.e. returns mymap->end().

like image 114
Luchian Grigore Avatar answered Sep 28 '22 09:09

Luchian Grigore