Jump to content

  • Log In with Google      Sign In   
  • Create Account


Help to understand my own misstake?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Conny14156   Members   -  Reputation: 258

Like
0Likes
Like

Posted 28 March 2014 - 07:16 PM

Hi,

Today/Yesterday I been having a few problem in my code which I manged to "solve", but I was wondering why my original approach didnt work? 

Basically I have this std::map with a vector inside it like

std::map<int, std::vector<GameObject*>> testMap;

than i started to insert stuffs like so

mapTest.insert(std::pair<int, std::vector<GameObject*>>(renderID,{gameObjectTest})); //first try
mapTest.insert(mapTest.end(),std::pair<int, std::vector<GameObject*>>(renderID,{gameObjectTest})); //second try

I also tried to have a tempVector where I push_back the gameObjectTest in it and just had tempVector instead of { gameObjectTest } 

 

At this point I got no syntax error or the likes, and when I ran the code it works great.

But when I tried to access the gameobject via 

mapTest.at(0).at(0);

I get a std::out_of_range error,

but instead of using .at I tried to use

std::map<int, std::vector<GameObject*>>::iterator it = mapTest.begin();
it->second.at(0);

for now it works and I get no std::out_of_range error. I was wondering how come .at doesnt work but .begin (aka iterator) works?


Edited by Conny14156, 28 March 2014 - 07:25 PM.


Sponsor:

#2 Diusrex   Members   -  Reputation: 639

Like
6Likes
Like

Posted 28 March 2014 - 07:56 PM

What was the value for renderID?

 

Because when you use mapTest.at(0), it tries to find an element whose key is 0 and if there is no element with a key of 0 in the map, then it throws a on out_of_range error. 

If you wanted to access the gameObjectTest that you added, then you would want to use mapTest.at(renderID).

 

When you use begin() though, it doesn't care what the key is, it just starts at the beginning of the map, which would be the pair that you added in.


Edited by Diusrex, 28 March 2014 - 07:58 PM.


#3 nobodynews   Crossbones+   -  Reputation: 1801

Like
4Likes
Like

Posted 28 March 2014 - 07:57 PM

http://en.cppreference.com/w/cpp/container/map/at

 

std::map::at takes a key, not an index. What your first try was actually accomplishing was returning the value associated with key=0. In the words of that reference:

 

std::out_of_range if the container does not have an element with the specified key

 

You didn't have key=0 in your std::map hence that exception. std::map::begin, however, returns an iterator to the first std::pair in the std::map container. The key isn't used at all. As long as there is one item in your std::map the iterator method will work.


C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


#4 Conny14156   Members   -  Reputation: 258

Like
1Likes
Like

Posted 28 March 2014 - 08:08 PM

i feel stupid and thanks >.<



#5 Lantre   Members   -  Reputation: 184

Like
1Likes
Like

Posted 06 April 2014 - 02:31 PM

Unless you can guarantee that the particular key you want is in the map you may instead want to consider using find and checking the returned iterator is valid.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS