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++?

1
  • 1
    And you're not using std:vector<>s because...? Commented Aug 7, 2013 at 16:42

4 Answers 4

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));
Sign up to request clarification or add additional context in comments.

8 Comments

is C++11 common enough to use it as default in examples now?
@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.
@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).
@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.
@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
|
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.

Comments

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.

1 Comment

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).
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.