424,996 Members | 896 Online
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 #include using namespace std; typedef vectorvect; typedef vectormatr; 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
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 vect; typedef vectormatr; 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 #include #include typedef std::vector

 P: n/a vect; typedef vectormatr; 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. // 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"

 P: n/a ) 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. > 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"

### This discussion thread is closed

Replies have been disabled for this discussion.