Print size of a vector through an iterator?

It = csvMap. Find(commandList. At(lineCount)); if (it == csvMap.end()) { cout second.size() First Since this is after the iterator is the end iterator, you have undefined behavior when dereferencing it.

It = csvMap. Find(commandList. At(lineCount)); if (it == csvMap.end()) { cout second.size() String c = (*it).

First; Since this is after the iterator is the end iterator, you have undefined behavior when dereferencing it.

The end iterator is not an item in the container, as I said. – Fred Nurk Jan 24 at 13:00 @Fred Yea I got you. I'm just telling why I thought it's like this.

So map::end is an empty field? – Beasly Jan 24 at 13:02 @Beasly: For vectors, end() is 1 passed the last element in the vector. For other containers, it is a special iterator that indicates there are no more values in the container.

– Zac Howland Jan 24 at 13:16 1 For vectors, it points to 1 passed the last element. Anytime you do a search (e.g. Find) on any STL collections and it returns end(), the element is not in the collection. End() is not a valid element in any STL collection.

– Zac Howland Jan 24 at 13:32.

Your it is pointing to an invalid location. You need to initailize it with the map's iterator. Something like it = myMap.

Find("aaa"); //Replace it with actual key After doing the find you need to make sure you are having a valid iterator by checking it agains myMap.end(). EDIT You are using uninitialized iterator here: coutsecond.size() You need to initialize the it with the return value of insert function like this: it = csvMap. Insert(....).

First; in these cases.

– Naveen Jan 24 at 12:45 1 As per your EDIT2 the above explanation is still valid. You are using uninitialized iterator. Also, there are many more problems with the posted code while using the iteator.

– Naveen Jan 24 at 12:51 @Naveen the find method is working correctly, I checked though watches. Is enough isn't it? – Beasly Jan 24 at 12:53 1 @Beasly: See the updated post as per edit 3.

– Naveen Jan 247 at 13:17.

Map iterators aren't invalidated unless you erased that specific element. That means that you did something else wrong.

Your collection is rather complex. In order to check the size though you need to know there is an element there at all, i.e. Find did not return "end" of your map.

If it does, you cannot use the iterator returned. Of course how you handle this is your own decision, eg return maybe -1 if the string was not found (with 0 indicating it was found but had no content). Now you have edited your code I immediately spot a bug: if (it == csvMap.end()) { /* * it = csvMap.end() > true * Either when command isn't found or command is the last */ string c = (*it).

First; you cannot dereference end (which it is).

I was taking from the API: An iterator to the element, if the specified key value is found, or map::end if the specified key is not found in the container. That's why I thought when not found it points to the last one... – Beasly Jan 24 at 12:57 When it is not found it does point to map::end but that is not the last element, it is one past the last element. – CashCow Jan 24 at 13:09.

I'm trying to print the size of a vector. Sounds easy, but the vector is in a map. The iterator is intialised like this: it = csvMap.

It's not working, the program crashes. But just to get the size is kind of wasting space isn't it? Is there a better way to do so?

Thanks in advance!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions