By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

vector within a vector

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Angela wrote:
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< 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?


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[i] here...

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

/<en

Jul 19 '05 #2

P: n/a
Kenneth Rose wrote:
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.

Jul 19 '05 #3

P: n/a
Angela wrote:
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::ostream;

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

--
Reverse domain name to reply.

Jul 19 '05 #4

P: n/a
el*********@hotmail.com (Angela) wrote in message news:<b2*************************@posting.google.c om>...
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();
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.