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.