Sign in to follow this  

[c++]vector

This topic is 3318 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

Hy. I'm still study the stl and container. I have created a vector<type>vVector: I 'm insert in the vector a series of data for ex 012345 and i would to recuperate so: 012345. For the insertion i use push_back(item); for reading from the first to the last in the above order what is the method? must use a reverse_iterator? how? thanks.

Share this post


Link to post
Share on other sites
The preferred way is to use iterators:

#include <iostream>
#include <vector>

int main()
{
std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it)
std::cout << *it;
}

If that's too complicated for you, you can fall back to using an index variable:

#include <iostream>
#include <vector>

int main()
{
std::vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for (int i = 0; i < v.size(); ++i)
std::cout << v[i];
}

But that's "not very C++" ;)

Share this post


Link to post
Share on other sites
I'll prefix this post with an admission that at one point I loved STL and used it everywhere, but now I'm no longer keen on it at all.... but i don't want this turning into a flame war......

Quote:
Original post by DevFred
The preferred way is to use iterators:


I'm not so sure it is anymore. Both AMD and Intel recommend using indices because it's easier for the compiler to optimize, and things like openMP can't use iterators in #pragma omp parallel for constructs.

Quote:
But that's "not very C++" ;)


It is, it's just not very STL. However, for 64bit compatibility, do yourself a favour and use size_t instead of int....

    for (size_t i=0; i < v.size(); ++i)
std::cout << v[i];



As an aside, my opinion of STL has dropped a fair amount after being forced to rewrite STL with something that:

a. doesn't cause so many problems when exposed as part of a plug-in API
b. works nicely with custom allocators on all platforms
c. works nicely with aligned data structures.

However, unless you have the time to write your own containers, chances are you'll want to be using STL. (If you write a useful dynamic library, it's worth ensuring your don't use STL in the public interfaces....)

So anyhow, the point of my little rant is to say I don't think it's right to insist that STL is the correct C++ thing to do, because these days I'm inclined to say that it normally isn't....

Share this post


Link to post
Share on other sites
Iterators are the C++ way of iterating over a collection, that's just how it is. If at some point you decide to use a list instead of a vector, the v[i] will become a performance virus. Other data structures don't even provide the indexing operator.

Share this post


Link to post
Share on other sites
Quote:

Iterators are the C++ way of iterating over a collection, that's just how it is. If at some point you decide to use a list instead of a vector, the v[i] will become a performance virus. Other data structures don't even provide the indexing operator.

std::list is one of those, actually, so the refactor from vector to list isn't trivial because code using indexes will fail to compile (and this is good, because the performance characteristics are so different, as you noted, that more than a simple type change should be necessary to maintain optimum usage).

Share this post


Link to post
Share on other sites
What is sometimes not obvious when writing templates is the usefulness iterator::begin() and iterator::end() and operator !=, because with them you can write generic code that works for native arrays, vectors, lists, and other container types which implement them, e.g. (must hurry up, hence --> pseudo-code, you'll get the point):


template <typename T> void foo (const T begin, const T end) {
for (T curr=begin; curr != end; ++curr) {
...do something with (*curr)...
}
}

int abc [3] = {0,1,2};
foo (abc, abc+sizeof(abc));

vector<raboof> vec;
...
foo (vec.begin(), vec.end());


Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:
Original post by DevFred
Other data structures don't even provide the indexing operator.

std::list is one of those, actually

Silly me, I confused std::list with java.util.LinkedList...

Share this post


Link to post
Share on other sites

This topic is 3318 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this