   Naming in a loop
  Hello all. :)I am wondering how to create variables with unique names in a loop. For example, I want to do something like this:for (int i = 0; i < 20; i++) {    int myNumberandi = 2;}However, instead of creating (and overwriting) myNumberandi 20 times, I would like to have the following variables:...myNumber0;myNumber1;myNumber2;...since they where created in a loop. How would I do this?An other thing, how would I access such variables in a loop? For example, something like this:for (int i = 0; i < 20; i++) {    myNumberandi += i;}(again, myNumberandi should be myNumber0, myNumber1, myNumber2, etc depending on teh value of i).Thanks in advance! :)   
  o_o;You can't do it and certainly don't want to do it. I'd suggest you looking into arrays (or vectors if you're using C++, or whatever). They're probably what you need. That way, instead of havingnumber0number1number2etc.You havenumber[0]number[1]number[2]etc.EDIT: oh, and by the way, the number between braces could be anything, including a full expression (e.g. i + j * k =P). Just make sure that the values don't go outside the array.   ;  Assuming C, C++,Java or C# (and probably more), this won't work. You can, however, achieve the same effect by using arrays!int myNumbers[20];for (int i = 0; i < 20; i++) {    myNumbers = 2;}for (int i = 0; i < 20; i++) {    myNumbers += 2;};  What you're looking for is an array.An array is simply a set of a single data type, all in a row.Assuming you're working with C, you would do something like:int myNumbers[20];   // declare an array of 20 intfor (int i = 0; i < 20; i++){    myNumbers = i;}So, each time you see 'myNumbers' that means that it is referring to the array item 'i'. For example, if 'i' was 5, that means that it is referring to the 6th int in the array. It is the 6th because it counts myNumbers[0] as the first item in the array.From there, you can do whatever you want with the array items. You can use 'myNumbers' just like you would any other int.Does that answer your question?;  Thank you all! :)The reason I posted this was mainly because I cant create a vector containing vectors, and thus wanted to create a few vectors in a loop. Never thought of using arrays (I use vectors too much :P).Thanks again! :)   ;Quote:Original post by pseudobotThank you all! :)The reason I posted this was mainly because I cant create a vector containing vectors, and thus wanted to create a few vectors in a loop. Never thought of using arrays (I use vectors too much :P).Thanks again! :)Why is it that you can't create a vector of vectors?std::vector<std::vector<int>> will create a vector with a vector of int's inside it. That way you can do myVector[5][1] = 10.Or is there any other restriction that is stopping you?   ;Quote:Original post by CodeLuggageWhy is it that you can't create a vector of vectors?std::vector<std::vector<int>>I think you meantstd::vector<std::vector<int> >Maybe this is the exact problem the OP was facing.   ;Quote:Original post by DevFredQuote:Original post by CodeLuggageWhy is it that you can't create a vector of vectors?std::vector<std::vector<int>>I think you meantstd::vector<std::vector<int> >Maybe this is the exact problem the OP was facing.What? Am I missing something? Because those two are identical on my screen. :P   ;Quote:Original post by CodeLuggageQuote:Original post by DevFredQuote:Original post by CodeLuggageWhy is it that you can't create a vector of vectors?std::vector<std::vector<int>>I think you meantstd::vector<std::vector<int> >Maybe this is the exact problem the OP was facing.What? Am I missing something? Because those two are identical on my screen. :PCodeLuggage - DevFred's example has a space between the last two > symbols. Some compilers have a work around for this, but technically in your example the last >> is interpreted by the scanner as the bitshift operator and causes a compile error, hence the whitespace in DevFred's version.It is possible that this compiler error is what was causing the OP to have trouble creating a vector of vectors.;Quote:Original post by AardvajkQuote:Original post by CodeLuggageQuote:Original post by DevFredQuote:Original post by CodeLuggageWhy is it that you can't create a vector of vectors?std::vector<std::vector<int>>I think you meantstd::vector<std::vector<int> >Maybe this is the exact problem the OP was facing.What? Am I missing something? Because those two are identical on my screen. :PCodeLuggage - DevFred's example has a space between the last two > symbols. Some compilers have a work around for this, but technically in your example the last >> is interpreted by the scanner as the bitshift operator and causes a compile error, hence the whitespace in DevFred's version.It is possible that this compiler error is what was causing the OP to have trouble creating a vector of vectors.Ah I see! I never thought of that, and yes it might very well be because of that. Anyway, hope you figure it out, OP :)   