Re: vectors inside a vector
bejiz <bruno.julier@wanadoo.fr> wrote in message...
Hello,
I would like to make a vector which can store vectors within. It is
for finding the permutations of some numbers. I thought it would be
easy to write some line of code to do this, but apparently, there is a
problem for reading the vector within a vector.
Here is my code. I have added some lines for printing words so that I
could guess where the problem is:
[ mess snip ]
You had an error in your nested for loops.
#include<iostream>
#include<vector>
#include <stdexcept>
typedef std::vector<double> vect;
typedef std::vector<vect> matr;
matr permute( vect A, std::ostream &cout ){
// - suggestion: pass by reference -
// matr permute( vect &A, std::ostream &cout ){
for( double v = 1; v <= 3; ++v )
A.push_back( v );
cout<<"A.size()="<<A.size()<<std::endl;
cout<<"az";
matr B;
vect b;
for( size_t i(0); i < A.size(); ++i ){
cout << "ezr\n";
// for( int j = 0; j < 3; i++){ // i++ ??
for( size_t j(0); j < A.size(); ++j ){
cout<<"iz";
file://b.push_back( A[i] ); file://b.push_back( A[j] );
b.push_back( A.at( i ) );
b.push_back( A.at( j ) );
if( j != i )
B.push_back( b );
else cout << "er";
cout<<"uz"<<i<<" "<<j<<"\n";
b.clear();
if( j != i )
vect H = B.at( i );
cout<<"er";
if( B.size() > size_t(i+1))
// comment that 'if' line for exception test.
vect G = B.at( i+1 ); // leave this one
} // for(j)
} // for(i)
return B;
} // permute(vect)
int bejizmain( std::ostream &out ){
vect Z;
permute( Z, out );
return 0;
} // main()
int main(){
try{
bejizmain( std::cout );
}
catch( std::out_of_range const &Oor ){
std::cout<<" caught: "<<Oor.what()<<std::endl;
}
catch( std::exception const &Se ){
std::cout<<" error: " << Se.what() << std::endl;
}
catch( ... ){
std::cout<<" error: catch( ... )"<< std::endl;
}
} // main()
Once you get it all running and tested, you can remove the 'try-catch'
structure.
--
Bob R
POVrookie