vectors inside a vector

B

bejiz

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:

#include<iostream>
#include<vector>
using namespace std;

typedef vector<double>vect;
typedef vector<vect>matr;



matr permute(vect A)
{
for(double v = 1; v <= 3; v++)A.push_back(v);cout<<"az";
matr B; vect b;
for(int i = 0; i < 3; i++)
{cout << "ezr\n";
for(int j = 0; j < 3; i++)
{
cout<<"iz";
b.push_back(A);b.push_back(A[j]);
if(j!=i) B.push_back(b);else cout << "er";
cout<<"uz\n";b.clear();
vect H = B.at(i);
cout<<"er";
vect G = B.at(i+1);
}
}
return B;
}
int main()
{
vect Z;
permute(Z);

}


Thanks for some help.
 
W

werasm

bejiz said:
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:

I don't like to nitpick about style, but your indentation and
usage of braces does not convey your intent. Or at least,
I'm not sure what you intent is as result of your usage
of braces. Fix that and I might consider helping you.

Especially the first for loop in permute confuses me.

Regards,

Werner
 
D

Default User

bejiz said:
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:
for(double v = 1; v <= 3; v++)A.push_back(v);cout<<"az";
matr B; vect b;
for(int i = 0; i < 3; i++)
{cout << "ezr\n";
for(int j = 0; j < 3; i++)
{
cout<<"iz";
b.push_back(A);b.push_back(A[j]);
if(j!=i) B.push_back(b);else cout << "er";
cout<<"uz\n";b.clear();
vect H = B.at(i);
cout<<"er";
vect G = B.at(i+1);



Good grief. Go back over that an format it to something legible. ONE
statement per line. Consistent indentation. Then explain what you
expected the code to do, and what it actually did.




Brian
 
H

hurcan solter

vect H=B.at(i)

B is empty at this moment ( for i=j=0) and I wholeheartedly agree
with critique about the style...
 
T

tony_in_da_uk

typedef vector<double>vect;
typedef vector<vect>matr;

Thinking aloud - so to speak - this is reasonable.
matr permute(vect A)

This should probably be...
matr permute(const vect& A)
....indicating that you won't change A, and don't actually need to
create a copy of all the data in A: just having some kind of reference
to the input data is sufficient.

By way of style, everyone has their own ideas, but it's pretty common
for uppercase identifiers to be reserved for the preprocessor. Most
people will also start variable names with a lower case letter, though
some will then use upper case letters to start words embedded in the
identifier (likeThis), while others (myself included) use underscores
(like_this), and a few others use both (like_This). Less common, I
personally use mixed-case words for types (so I would use Vector and
Matrix as the type identifiers).
{
for(double v = 1; v <= 3; v++)A.push_back(v);cout<<"az";

So you're populating A with { 1, 2, 3 }... fine.
matr B; vect b;

Again, B should be lower case. Something like "result" or "permuted"
would be a better name.
for(int i = 0; i < 3; i++)
{cout << "ezr\n";
for(int j = 0; j < 3; i++)
{
cout<<"iz";
b.push_back(A);b.push_back(A[j]);


The first time through this will push_back 1, 1.
if(j!=i) B.push_back(b);else cout << "er";

Not the first time, but sometimes you push_back the vector b on B...
cout<<"uz\n";b.clear();

You clear the elements out of b.
vect H = B.at(i);
cout<<"er";
vect G = B.at(i+1);

All this stuff does nothing except generate an exception if you can't
access B and B[i+1]. Given an iteration pushes back at most one
element into B (and the first iteration where i == j doesn't even do
that), this is guaranteed to throw an exception. If I assume it's
just you wanting to "explore" the data being generated and not part of
the algorithm, and ignore these lines, I can move onto considering the
algorithm.

So, considering the i == 0 loop: when j is also 0 we skip the
B.push_back so there's no net affect (be better to have an "if (i ==
j) continue;" just inside the for loop). Then for j == 1 and j == 2
we push back first { 1 , 2 } then { 1, 3 }.

Then you're looping on i, which will end up doing a B.push_back for
{ 2, 1 } then { 2, 3 }, then { 3, 1 } and { 3, 2 }.
}
}
return B;}

So your return value will be { {1,2}, {1,3}, {2,1}, {2,3}, {3,1},
{3,2} }.
int main()
{
vect Z;
permute(Z);

You're not storing the result anywhere. How about trying:

matr m = permute(Z);

And you might want to inspect the results:

cout << "[0,0] == " m[0][0]\n";
cout << "[1,0] == " m[1][0]\n";

Or you could loop over them:

for (matr::const_iterator mi = m.begin(); m != m.end(); ++m)
{
cout << "{ ";
for (vect::const_iterator vi = mi->begin(); vi != mi->end(); +
+vi)
{
cout << *vi << ' ';
}
cout << "}\n";
}

Cheers,

Tony
 
B

BobR

bejiz said:
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::eek:stream &cout ){
// - suggestion: pass by reference -
// matr permute( vect &A, std::eek:stream &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 ); 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::eek:stream &out ){
vect Z;
permute( Z, out );
return 0;
} // main()

int main(){
try{
bejizmain( std::cout );
}
catch( std::eek:ut_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.
 
B

BobR

Thinking aloud - so to speak - this is reasonable.
This should probably be...
matr permute(const vect& A)
...indicating that you won't change A, and don't actually need to
create a copy of all the data in A: just having some kind of reference
to the input data is sufficient.


So you're populating A with { 1, 2, 3 }... fine.


Again, B should be lower case. Something like "result" or "permuted"
would be a better name.

Don't you think that a for loop using 'j' amd post-incrementing 'i' is a
little funny?
An error below is a blessing, else it's an infinite loop.
{
cout<<"iz";
b.push_back(A);b.push_back(A[j]);


The first time through this will push_back 1, 1.


But, 'j' is ALWAYS zero.
 
T

tony_in_da_uk

HOW can it be 'fine'. You just told him/her to pass by **const** ref.

- in general, "class" inputs to functions should be const-ref

- in this example, the OP probably isn't really using "A" as a
function argument (though he's not explicitly clearing it before using
push_back on it either), which suggests it should be a local variable

- when I say fine, I'm simply acknowledging that actual behaviour of
the original code is vaguely comprehensible
Don't you think that a for loop using 'j' amd post-incrementing 'i' is a
little funny?
An error below is a blessing, else it's an infinite loop.

Yes - well spotted.
But, 'j' is ALWAYS zero.

As you say, that appears to be a typo.
 
B

BobR

- in general, "class" inputs to functions should be const-ref

First, I hope you did notice that I put a big-grin(<G>) on that line.

I would probably have filled vector 'A' in main (or another function), then
passed it to 'permute' as const ref.. So, I agree with you there.
- in this example, the OP probably isn't really using "A" as a
function argument (though he's not explicitly clearing it before using
push_back on it either), which suggests it should be a local variable

If the above (const ref) isn't used, yep. It looks like the OP just wanted
some data to experiment with multi-array (vector of vectors). Probably
copy-pasted the second for(), but forgot to change the 'i' to 'j' ( they
look alike in some editors, so, I usually use 'x,y,z' for index vars
(hang-over from 3D graphics.:-})).
- when I say fine, I'm simply acknowledging that actual behaviour of
the original code is vaguely comprehensible

That statement makes me think you missed my big-grin. said:
Yes - well spotted.

I thought that some of these young whipper-snappers with better eye-sight
would have spotted it, but the OPs formatting was not the best it could be.
:-}
Actually, I didn't spot it until I had removed all but the couts, and got
locked in a infinite loop.
 
B

bejiz

BobR said:
file://b.push_back( A ); file://b.push_back( A[j]


Reason #245 never to use Outlook Express.

Brian


Thanks all of you for your answers. I'll take care of the
indentation in the future.
It was because I didn't store the result of the function permute
that I couldn't print any result. The "i" in the "j" loop is a simple
typo, of course it didn't risk to work like that.
I rarely use the exception and try catch but it is an occasion to
relearn this. In this case, the debugger didn't spot any particular
problem but just complained about a subscript out of range.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

vector of vectors 3
pointer to a vector 7
Vectors vs Arrays performance 24
Filter sober in c++ don't pass test 0
printing vectors 4
Chatbot 0
vector of structs 7
Vector iterator problem 3

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top