go through vectors? which way is better?

How can I use one index to iterate between all the values

for example:

1
2
3
4
5
6
7
8
9
//I defined
vector<typeA> VA(XD,typeA());
vector<typeB> VB(XD,typeB());

//then the standard method to iterate is:
for(vector<typeA>::Size_type i=0; i<VA.size(); i++) {
    VA[i].functionA();
    VB[i].functionB();
}


the other way, use int,

1
2
3
4
5
6
7
8
9
//I defined
vector<typeA> VA(XD,typeA());
vector<typeB> VB(XD,typeB());

//then the standard method to iterate is:
for(int i=0; i<VA.size(); i++) {
    VA[i].functionA();
    VB[i].functionB();
}


none of above make me feel formal, i don't know how do you guys do this. Thanks.
Last edited on
the standard is using vector<type>::size_type, but when I use typeA, I feel typeB is ignored, vice versa. While use int looks not formal, I don't know how you guys do this.
It's safe to use std::size_t: vector can't physically have size_type larger than size_t and both types are guaranteed to be unsigned, so conversion from size_type to size_t is safe.

if you are feeling particularly paranoid, youd could check that both vectors' size_types are the same (with a static_assert, for example)

PS:
I don't know how you guys do this

I use iterators (or higher-level concepts) to iterate
Last edited on
You could use std::size_t from cstddef.
Usually it should be identical to vector::size_type.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <cstddef>

using namespace std; // although this is a bad habit

//I defined
vector<typeA> VA(XD,typeA());
vector<typeB> VB(XD,typeB());

//then the standard method to iterate is:
for(size_t i=0; i<VA.size(); i++) {
    VA[i].functionA();
    VB[i].functionB();
}


the standard is using vector<type>::size_type, but when I use typeA, I feel typeB is ignored, vice versa.

I know I shouldn't bring this up, but this indeed reeks of poor design doesn't it?

http://www.cplusplus.com/reference/vector/vector/#types
Last edited on
uh, I believe these are all I can do, maybe I am too paranoid....
The bigger problem is why are you using the size of vector A and assuming that vector b is the same size? That is a much bigger issue then worrying about using an int vs. size_t or whatever. You need to iterate over them with separate loops, or design a short template function that uses iterators.

1
2
3
4
5
6
7
8
template <class InputIterator_t>
void doSomething(InputIterator_t first, InputIterator_t last)
{
    while (first != last)
    {
        // do something
    }
}

If you had an abstract base class for typeA and typeB say typeAB you could then have a single vector of typeAB and iterate over all of them checking for whether you have a typeA or typeB. As @kempofighter pointed out you need to use an iterator not a size_t.
closed account (3qX21hU5)
Iterating over two vectors is called a zip in functional programming I believe. And if I am not mistaken boost has a zip iterator which should be able to help you out with this.

http://www.boost.org/doc/libs/1_46_0/libs/iterator/doc/zip_iterator.html .

Topic archived. No new replies allowed.