3

Is it possible to make variable number of variables? For instance, say I want to declare some unknown number of integers, is there a way to have the code automatically declare

int n1;
int n2;
.
.
.
int nx;

where x is the final number of variables required.

A potential application requiring this would be reading a .csv file with unknown number of rows and columns. Right now, the only way I can think to do this without variable number of variables is either a 2D vector, or coding in more columns than possibly can be in any input file the program receives

thkala
  • 84,049
  • 23
  • 157
  • 201
Andy
  • 41
  • 2
  • 3
  • 10
    what's wrong with an array? – ircmaxell Feb 05 '11 at 04:53
  • @irc: I'm guess array won't work for him because it still has a limited number of elements. But a vector would solve that problem. – Ayush Feb 05 '11 at 04:59
  • I think you're looking for an eval() function. It can create dynamic variables for you in MATLAB and Actionscript, but I doubt it's available in C++. http://www.google.co.in/search?q=eval+function+in+c%2B%2B&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Nav Feb 05 '11 at 05:30
  • On the other hand, you'd be better off working with an std::list (coz vectors require a lot of internal re-allocation and copying for the kind of application you're suggesting). If you're working with strings, there's std::rope http://www.sgi.com/tech/stl/Rope.html – Nav Feb 05 '11 at 05:37
  • @Nav: `list`'s are arguably the last choice container, `vector`'s and `dequeue`'s out-perform it in nearly every case. And `rope` is not part of the standard library. – GManNickG Feb 05 '11 at 08:49
  • @Gman: Ah...I keep forgetting about the existence of dequeue. How would vector outperform list (in this case)? Re-allocations will be much more costly. I've read in Effective STL that rope isn't part of the standard library, but had wanted to ask someone why a container that isn't part of the standard library is so condemned? Even Meyers seems to condemn it the same way you did. Is it because it doesn't provide the basic guarantee of exception safety or something? – Nav Feb 05 '11 at 09:09
  • @Nav: It's because it's not C++. You can't say "Use C++ and use `std::rope`.", because C++ simply has no such thing. We want to program in standard C++. – GManNickG Feb 05 '11 at 09:10
  • @GMan: Ok, so it's about portability http://stackoverflow.com/questions/2826431/stl-rope-when-and-where-to-use. Thanks :) – Nav Feb 05 '11 at 09:19
  • @Nav: "How would vector outperform list (in this case)? Re-allocations will be much more costly." -- Not necessarily. Populating a list with `N` objects will require `N` allocations. Assuming a vector doubles its size on each reallocation, populating it will require `log2(N)` allocations, and up to `2*N` object moves. So the vector will be cheaper to populate, unless the objects are expensive to move, and cheaper to access randomly. Lists become cheaper if you need to frequently insert and remove objects in the middle of a large sequence. – Mike Seymour Feb 05 '11 at 11:42
  • Thanks Mike. I agree with that. – Nav Feb 08 '11 at 11:38

2 Answers2

8

Yes. (better and possible!)

int x[100]; //100 variables, not a "variable" number, but maybe useful for you!

int *px = new int[n];// n variables, n is known at runtime;

//best
std::vector<int> ints; //best, recommended!

Read about std::vector here:

http://www.cplusplus.com/reference/stl/vector/

See also std::list and other STL containers!


EDIT:

For multidimensional, you can use this:

//Approach one!
int **pData = new int*[rows]; //newing row pointer
for ( int i = 0 ; i < rows ; i++ )
     pData[i] = new int[cols]; //newing column pointers

//don't forget to delete this after you're done!
for ( int i = 0 ; i < rows ; i++ )
     delete [] pData[i]; //deleting column pointers
delete [] pData; //deleting row pointer

//Approach two
vector<vector<int>> data;

Use whatever suits you, and simplifies your problem!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Oh really? I'd say it's A variable with a fixed amount of element (100). – Loïc Faure-Lacroix Feb 05 '11 at 04:57
  • @Loic: See all. Choose whatever you need! – Nawaz Feb 05 '11 at 04:57
  • @Loic: not in case of std::vector – Ayush Feb 05 '11 at 04:58
  • 2
    @all I wrote my comment when there was only "int x[100];". I thought it was lazy to just answer that since it doesn't solve his problem if he reads a csv file of more than X rows. – Loïc Faure-Lacroix Feb 05 '11 at 05:10
  • @Loic: that's SO style. people here post an incomplete answer first, then adds more to it, making it better. sometime something is mentioned just for the sake of flow, not necessarily answers the question. – Nawaz Feb 05 '11 at 05:14
  • Yes, so a vector works fine in the 1 dimensional case. However, the problem really arises in higher dimensional cases. For instance, in the .csv file, both row and column count are unknown. Two dimensional vectors are rather cumbersome in this scenario because the nature structure of a 2D vector is not the structure of the .csv file. The 2D vector groups data "horizontally" while in the .csv file, I am looking to group data "vertically" – Andy Feb 05 '11 at 05:28
  • 1
    @Nawaz: "that's SO style. people here post an incomplete answer first, then adds more to it, making it better" - I agree it happens sometimes, Nawaz, but would it be correct to call it SO style? – Nav Feb 05 '11 at 05:40
  • @Nav: Probably yes, because to get upvotes, people want to post as early as possible. Well if you disagree then I would say, alright SO Users style :P – Nawaz Feb 05 '11 at 05:49
5

Use either

std:vector<int> n

or

int* n = new int[x];
hiddensunset4
  • 5,825
  • 3
  • 39
  • 61
John
  • 5,561
  • 1
  • 23
  • 39
  • Upvoted because this answer is identical to Nawaz, yet it received a downvote? Wheres the logic. And it address the OP's question. – hiddensunset4 Feb 05 '11 at 04:57
  • @dcousens Probably that "it doesn't contribute anything new to [so]". [Meta](https://meta.stackoverflow.com/questions/365184). [Although...](https://meta.stackexchange.com/questions/9731) in this case both come up with their answer independently. – user202729 Mar 29 '18 at 10:06