0

I need to create a number of arrays of a certain object where the number I need is dependent on a separate variable the best way to explain it is with a psudo code example:

int num = 4;
for(int i=0;i<num;i++){
   object_type arrayi [dynamic size];
}

So i need 4 arrays each with the names array0,array1,array2, and array3 and they must all be dynamic arrays. Is there anyway to do this in C++?

user1828256
  • 25
  • 1
  • 3
  • 1
    And you're not using [`std:vector<>`s](http://en.cppreference.com/w/cpp/container/vector) because...? – WhozCraig Aug 07 '13 at 16:42

4 Answers4

5
std::array<std::vector<object_type>, 4> array;
for (auto & v : array)
    v.resize(dynamic_size);

The names are array[0], array[1], etc... instead of array1, array2, etc... But who cares? If you absolutely must have those names, then Cassio's answer is your best bet.

Pre C++11 alternative:

std::vector<object_type> array[4];
for (size_t i=0; i<4; ++i)
    array[i].resize(dynamic_size);

If you want a variable number of arrays, then you can use a vector of vectors, and actually, the initialization for that is even easier. It doesn't require a loop, you can do it in the constructor.

std::vector<std::vector<object_type>> array(num, std::vector<object_type>(dynamic_size));
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • 2
    is C++11 common enough to use it as default in examples now? – Graham Griffiths Aug 07 '13 at 16:48
  • 2
    @GrahamGriffiths: Yes, I think so. Those who are restricted from using it are probably professional programmers and can figure out how to adjust the code on their own. New programmers have little reason to use old compilers. But I will add an alternative anyway. – Benjamin Lindley Aug 07 '13 at 16:50
  • @BenjaminLindley - I think the question isn't only "can people use C++11" but rather "should you explain using c++11". C++11 is more complicated than "vanila" c++ in many ways, and for someone who obviously doesn't know the basics... I find it's better to explain with simple language concepts (manbe even c-like). – rabensky Aug 07 '13 at 17:13
  • 1
    @cluracan: Well then we disagree strongly. Yes, C++11 is more complicated, because it has more stuff. But using the new stuff in place of the old stuff (rather than in addition to), where applicable, makes the code simpler, in my opinion. – Benjamin Lindley Aug 07 '13 at 17:18
  • @BenjaminLindley: just notice that you're explaining something to a person that doesn't even know the most basic syntax (objecti => object[i], variable scoping etc) and you're giving him an answer using 2 different kind of templates (type and number), two new class objects he never heard of (why not vector of vectors) and a for loop he never met. Personally I think telling someone "here are small changes to make your code work. you failed to notice (a) and (b)" is better than "everything you're doing is completely wrong the real answer is almost like a different language". but to each his own – rabensky Aug 07 '13 at 17:27
  • @cluracan: If he has questions about it, he will ask them, or do research on his own. This is a good thing. If I just point out little things to fix just to get his code working, he will write crappy code for the next few years and think it is okay. It is not. – Benjamin Lindley Aug 07 '13 at 17:33
  • @BenjaminLindley: no, it really isn't. If he has a few questions he might ask. if he has a lot he'll be frustrated - and might decide c++ is too hard and either give up or move to another language. It's like explaining how to solve general linear equations to a person asking how much is 4+5. When you teach, you have to go in small steps. if the learning curve is too steep people will go away – rabensky Aug 07 '13 at 17:42
  • @GrahamGriffiths Yes and no. If the answer is targeting professionals in the industry, most definitely not. Students, however, will almost certainly have a fairly recent version of VC++ or g++; both of which support a little of C++11. (Given the nature of this question, it seems fair to assume that he is _not_ a professional in the industry.) – James Kanze Aug 07 '13 at 18:07
2

Yes, use std::vector<object_type> instead. You can resize to an arbitrary size. Otherwise for arrays you can use dynamic allocation with

ObjectType* myArray = new ObjectType[number];

but using std::vector instead is recommended.

SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
0

Reading the OP again, it seems to me that the number of arrays is not known at compile time. In this case, you can use a std::vector<std::vector<object_type>>:

#include <vector>
// ...
// int num = ???, dynamic_size = ???;
std::vector<std::vector<object_type>> vs(num);
for (auto& v: vs)
    v.resize(dynamic_size);

then you can use vs[i][j] to get a reference to the j-th element of the i-th array (vector).

Piece of advise: Don't use this (std::vector<std::vector<double>>) for linear algebra matrices.

Bonus: In C++14 (actually this is a C99 feature that some compilers allow in C++ as an extension) you'll be able to do this:

#include <vector>
// ...
// int num = ???, dynamic_size = ???;
std::vector<object_type> vs[num];
for (auto& v: vs)
    v.resize(dynamic_size);

For more information see this post.

Community
  • 1
  • 1
Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
0

If there is a way to dynamically create variables like the way you want within C++, I haven't heard of it.

If performance is an issue and you need to construct a bunch of 1-d arrays (rather than an array of arrays or a vector of arrays) then you could do code generation at build time to make as many as you want. That's outside of C++ though; it's a pre-build command that outputs a C++ text file.

If performance isn't an issue, then constructing a vector of arrays like Benjamin has done will work great.

John
  • 15,990
  • 10
  • 70
  • 110
  • 1
    You are right, C++11 doesn't allow things like `void f(int n) { double x[n]; // ... }`. However C99 allows it (it's called variable length arrays). Some compilers allow it as an extension and in C++14 this will be legal (however with different semantics than C99). – Cassio Neri Aug 07 '13 at 16:59