vector within a vector

A

Angela

It's late. Why is the compiler saying that this is the wrong syntax:

vector<vector<cIndicatorValue>> ranks;

One other question:
If I want to access one of cIndicatorValue's functions, I have to
declare a vector iter, right? Can I add an integer to an a vector iter
to move it through the array?
 
K

Kenneth Rose

Angela said:
It's late. Why is the compiler saying that this is the wrong syntax:

vector<vector<cIndicatorValue>> ranks;

Because the '>>' immediately after the cIndicatorValue is being
interpretted as an operator. Try using whitespace:

vector said:
One other question:
If I want to access one of cIndicatorValue's functions, I have to
declare a vector iter, right? Can I add an integer to an a vector iter
to move it through the array?

If you declare an inter on ranks, then you will be iterating through
each of the vectors in ranks (since ranks holds vectors, not
cIndicatorValues).

For your second question, yes, you can add an integer (iterators are
just pointers, so adding an integer will result in pointer arithmetic,
which generally does the right thing). However, if you find that you
need to add an integer, why not just maintain a counter yourself instead
of using an iterator:

for( int i = 0; i < ranks.size(); i++ ) {
// do something with ranks here...

i += 3; // maintain the index yourself and add what you want
}

/<en
 
R

Rolf Magnus

Kenneth said:
For your second question, yes, you can add an integer (iterators are
just pointers, so adding an integer will result in pointer arithmetic,
which generally does the right thing).

No, iterators are not just pointers. vector iterators can be pointers,
but are not required to.
 
A

Adam Fineman

Angela said:
It's late. Why is the compiler saying that this is the wrong syntax:

vector<vector<cIndicatorValue>> ranks;

The compiler interprets '>>' as an operator. You need some whitespace:

vector<vector<cIndicatorValue> > ranks;

I prefer 'vector< vector<cIndicatorValue> > ranks', but they are
syntactically equivalent. Also, typedefs can make the syntax more
readable; see the example below.
One other question:
If I want to access one of cIndicatorValue's functions, I have to
declare a vector iter, right?

That's one way. You can also use operator[].
Can I add an integer to an a vector iter
to move it through the array?

If it works, it's only by accident. Iterators _can_ be implemented as
pointers, but they don't have to be. Look at std::advance().

Here's an example:


#include <vector>
#include <iostream>
#include <iterator>

using std::vector;
using std::cout;
using std::eek:stream;

class cIndicatorValue
{
public:
cIndicatorValue()
: d_iv(0)
{}

cIndicatorValue(int iv)
: d_iv(iv)
{}

friend ostream& operator<<(ostream&, const cIndicatorValue&);

private:
int d_iv;
};


ostream&
operator<<(ostream& os, const cIndicatorValue& iv)
{
cout << "[ " << iv.d_iv << " ]";
return os;
}


int
main()
{
typedef vector<cIndicatorValue> row_t;
typedef vector<row_t> rank_t;
rank_t ranks;

for (int i = 0; i < 5; ++i)
{
row_t row;
for (int j = 0; j < 5; ++j)
{
cIndicatorValue iv = (i * 10) + j;
row.push_back(iv);
}
ranks.push_back(row);
}

rank_t::size_type x = 2;
row_t::size_type y = 3;

cout << "ranks[" << x << "][" << y << "] = "
<< ranks[x][y]
<< '\n';

rank_t::const_iterator xi = ranks.begin();
std::advance(xi, x);
row_t::const_iterator yi = xi->begin();
std::advance(yi, y);

cout << "ranks[" << x << "][" << y << "] = "
<< *yi
<< '\n';

return 0;
}

- Adam
 
D

Dan Cernat

It's late. Why is the compiler saying that this is the wrong syntax:

vector<vector<cIndicatorValue>> ranks;

One other question:
If I want to access one of cIndicatorValue's functions, I have to
declare a vector iter, right? Can I add an integer to an a vector iter
to move it through the array?

one can do it
vector <vector <cIndicatorValue> > ranks;

// later

ranks[3][5].MethodOfcIndicatorValue();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top