By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,996 Members | 896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,996 IT Pros & Developers. It's quick & easy.

vectors inside a vector

P: n/a
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[i]);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.

Aug 6 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a

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

Aug 6 '07 #2

P: n/a
bejiz wrote:
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[i]);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
Aug 6 '07 #3

P: n/a

vect H=B.at(i)

B is empty at this moment ( for i=j=0) and I wholeheartedly agree
with critique about the style...
Aug 6 '07 #4

P: n/a
On Aug 7, 6:24 am, bejiz <bruno.jul...@wanadoo.frwrote:
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[i]);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[i] 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

Aug 7 '07 #5

P: n/a

bejiz <br**********@wanadoo.frwrote 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<doublevect;
typedef std::vector<vectmatr;

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
Aug 7 '07 #6

P: n/a

<to***********@yahoo.co.ukwrote in message...
On Aug 7, 6:24 am, bejiz <bruno.jul...@wanadoo.frwrote:
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.
{
for(double v = 1; v <= 3; v++)A.push_back(v);cout<<"az";

So you're populating A with { 1, 2, 3 }... fine.
HOW can it be 'fine'. You just told him/her to pass by **const** ref. <G>
// ref: matr permute(vect const &A)
>
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++)
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[i]);b.push_back(A[j]);

The first time through this will push_back 1, 1.
But, 'j' is ALWAYS zero.

--
Bob R
POVrookie
Aug 7 '07 #7

P: n/a
On Aug 7, 12:25 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
So you're populating A with { 1, 2, 3 }... fine.

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
for(int j = 0; j < 3; i++)
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.
The first time through this will push_back 1, 1.

But, 'j' is ALWAYS zero.
As you say, that appears to be a typo.

Aug 7 '07 #8

P: n/a

<to***********@yahoo.co.ukwrote in message...
On Aug 7, 12:25 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
So you're populating A with { 1, 2, 3 }... fine.
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
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. <G>
>
for(int j = 0; j < 3; i++)
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.
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.

--
Bob R
POVrookie
Aug 7 '07 #9

P: n/a
BobR wrote:

file://b.push_back( A[i] ); file://b.push_back( A[j]
Reason #245 never to use Outlook Express.


Brian
Aug 7 '07 #10

P: n/a
On 7 août, 19:22, "Default User" <defaultuse...@yahoo.comwrote:
BobR wrote:
file://b.push_back( A[i] ); 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.

Aug 7 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.