473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

vector within a vector

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
4 5860
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
16
by: Honestmath | last post by:
Hi, I added the following line to my code within a class declaration: std::vector<Date> m_duedates(100); I also tried: std::vector<Date> m_duedates(100, Date());
3
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are...
2
by: Marcus | last post by:
I have a vector within a vector within a map. The innermost vector houses a struct with some ints and floats. The map keys off the secondary vector which acts as a wrapper for multiple vector...
2
by: mj | last post by:
Hi, I recently have found it necessary to move from fortran to c++ for scientific programming... I'm working on a program that needs to resize a 2d vector of vectors within a function... This...
4
by: sreedhar.cs | last post by:
Hi all, In my application,I want to place a vector in a specific location in shared memory.(a user supplied pointer). I understand that the STL allocator mechanism places the data objects within...
9
by: Christian Chrismann | last post by:
Hi, I've a runtime problem with STL vectors. Here is the simplified version of the code: template <class Tclass A { ... private: vector<T*myvector; typename vector<T*>::itarator mIt;
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.