Hi there!
I'm writing a program which has lots of vectors in it...I've named these vectors sequentially - ie vector1, vector2, vector3 etc.
What I was wondering is, is there a way of displaying the contents of these vectors without manually writing cout << vector1[0] etc?
I know this doesn't work but its essentially what I want to do:

``````number1[0] = 1;
number2[0] = 2;
number3[0] = 3;

for(int i=1; i<=3; i++)
cout << numberi[0];``````

I appologise in advance for my amateur-ness....it's for a university project and we haven't had the time to properly learn c++!

For a start: it is no problem to create a vector of vectors which saves you naming each vector and instead use an index

``````typedef vector<vector<double> > DOUB_VEC_VEC;

DOUB_VEC_VEC myVecs;
myVecs.resize(3); // three vectors of doubles
myVev[0].push_back(2.4);
myVev[0].push_back(3.13);
myVev[1].push_back(5.0);
myVev[4].push_back(42.0);

cout << myVec[0][1] << endl; // displays 3.13``````

of course you can do this in a loop no problem

``````size_t numberOfvectors = 100; // 100 vectors
size_t vecSize[numberOfvectors]; // sizes of the individual vectors
// initialize all the differnt sizes...
//....

// set values
for(size_t vNum=0; vNum<numberOfvectors; vNum++)
{
myVec[vNum].resize(vecSize[vNum]);
for(size_t vInd=0; vInd < vecSize[vNum]; vInd++)
myVec[vNum][vInd] = (double)(rand()%100); // set a rand value between 0..99
}``````

Yeah I think it does answer my question. Although I think I've reached the limit of my programming skills (even with your help) :P
Thanks a lot!

If you want fancy stuff use STL for_each.

So if you have 3 vectors to print then your code would look like this:

``````vector<int> vec1;
vector<char> vec2;
vector<float> vec3;
for_each (vec1.begin(), vec1.end(), print);
for_each (vec2.begin(), vec2.end(), print);
for_each (vec3.begin(), vec3.end(), print);``````

Even if you don't want fancy stuff, I recommend to readup the STL algorithms it makes your code much smaller in size.

Yeah that looks like it could be very handy.
Thanks very much.

If the number of vectors which have elements on which you want to perform a particular operation (for example, print it) is fixed, you could write a simple wrapper function:

``````#include <algorithm>

template< typename SEQ1, typename SEQ2, typename SEQ3, typename FUNCTION >
inline void for_each( SEQ1& seq1, SEQ2& seq2, SEQ3& seq3, FUNCTION do_it )
{
std::for_each( seq1.begin(), seq1.end(), do_it ) ;
std::for_each( seq2.begin(), seq2.end(), do_it ) ;
std::for_each( seq3.begin(), seq3.end(), do_it ) ;
}``````

If not, you could overload the function for two, three, four ... sequences.

Or if you are using a compiler with support for C++0x variadic templates (like g++ 4.5 or 4.6 with -std=c++0X):

``````#include <algorithm>

template< typename FUNCTION, typename SEQUENCE >
inline void apply( FUNCTION do_it, SEQUENCE& seq )
{  std::for_each( seq.begin(), seq.end(), do_it ) ; }

template< typename FUNCTION, typename FIRST_SEQ, typename ...OTHER_SEQS >
inline void apply( FUNCTION do_it, FIRST_SEQ& first, OTHER_SEQS ... rest )
{  apply( do_it, first ) ; apply( do_it, rest ... ) ; }

#include <vector>
#include <list>
#include <iostream>
#include <iomanip>

int main()
{
std::vector<int> vec1 = { 0, 1, 2, 3, 4, 5 } ;
std::vector<int> vec2 = { 9, 8, 7, 6, 4, 5 } ;
std::vector<int> lst3( vec1.size(), 77 ) ;
std::vector<int> lst4( vec1.rbegin(), vec1.rend() ) ;

const int LINESZ = vec1.size() ;
int cnt = 0 ;
auto print_it = [ & cnt, LINESZ ] ( int value )
{
++cnt ;
std::cout << std::setw(3) << value << ( cnt%LINESZ == 0 ? '\n' : ' ' ) ;
} ;

apply( print_it, vec1, lst3 ) ;
apply( print_it, vec1, vec2, lst3, lst4 ) ;
}``````