Jump to content
  • Advertisement
Sign in to follow this  

questions about stl

This topic is 4720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have 3 questions about the stl, which I cannot solve myself: 1 Why doesn't anyone use std::deque? Is it slower than std::vector? 2 I know how to iterate a vector from front to back, but how about back to front? 3 Is it really necessary to use iterators instead of for(int i=0; i<vector.size(); i++) thanks!

Share this post


Link to post
Share on other sites
Advertisement
2) You can use a reverse_iterator, and rbegin/rend:


std::vector<CSomething>::reverse_iterator it( vector.rbegin() );
while ( it != vector.rend() )
{
// do something

++it;
}


3) for a vector i guess both would be ok, since getting the nth element is a simple function. For other containers this is usually a different story, as they'll have to reiterate from the start to find the nth element.

If you're using iterators on a vector you don't have to change anything if you replace the vector by a list or set later on. And only a small change if you change the container to a map.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalasjniekof
Hi I have 3 questions about the stl, which I cannot solve myself:

1 Why doesn't anyone use std::deque? Is it slower than std::vector?

2 I know how to iterate a vector from front to back, but how about back to
front?

3 Is it really necessary to use iterators instead of
for(int i=0; i<vector.size(); i++)
thanks!


1 - deque

2 - reversible container

std::vector<int> v;
for(std::vector<int>::reverse_iterator ri = v.rbegin();ri != v.rend();++ri)
...


3 - not necessary for vector, but a good idea. for example std::map needs an iterator for that purpose.

edit: the best reference i know of

Share this post


Link to post
Share on other sites
Quote:
Original post by Kalasjniekof
Hi I have 3 questions about the stl, which I cannot solve myself:

1 Why doesn't anyone use std::deque? Is it slower than std::vector?


Usually, but not always. But it's usually not enough slower to make a difference, and will sometimes be much faster (in the sorts of cases where lists are faster). It's sort of a hybrid, after all.

Anyway, the best reason for using std::vector over std::deque (unless you've already profiled and it's really that critical) is because the vector's storage will be contiguous (a single, solid block in memory); this makes it much easier to interface with old C APIs and in other situations where a plain array is needed.

Quote:
2 I know how to iterate a vector from front to back, but how about back to front?


You will need a reverse iterator. Incrementing the iterator causes it to move backwards, so all you need to do is obtain the required iterators. For most if not all containers, it's a simple matter of replacing .begin() and .end() calls with .rbegin() and .rend().

Quote:
3 Is it really necessary to use iterators instead of
for(int i=0; i<vector.size(); i++)
thanks!


No, but it will protect you from changes in the container type. If you later want to use a std::list instead, you may find that you were smart enough to typedef the container, but you'll still have problems with those for loops because the list doesn't offer operator[]. Indeed, iteration is more natural for a list; there's no way to offer a general list operator[] except by having it search from the beginning every time, which would slow things down horribly. (Because of that, and because it doesn't really make much sense to have it anyway, the standard library simply doesn't provide it.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!