1

I have lots of arrays with different sizes so they can't be multidimensional array as far as i know, so i named them as follows:

arr1[]
arr2[]
arr3[]
arr4[]
...

now can i use for loop to go over them and access them...

for(int i=1; i<5; i++) {
    for(int j=o; i<etc; j++) {
     cout<<arr[i][[j]<<endl;
    }
}
or cout<<arr+i<<endl;

now this obviously is telling compiler to add arr and i together, but that is not what i want. I want it to print arr1, arr2, etc.

I have tried, putting dot, comma, slashes, etc.

5
  • 1
    Why not use std::vector<std::vector<T>> ? Commented Sep 23, 2014 at 14:52
  • because using array is the requirment, however, is it possible to put all arrays of different sizes into vector Commented Sep 23, 2014 at 14:54
  • 1
    Yes, you can put arrays in arrays by heap allocating them and using pointers Commented Sep 23, 2014 at 14:55
  • can you give some example Commented Sep 23, 2014 at 14:56
  • @MuhammadUmer - because using array is the requirment, You do know that vector uses a dynamic array internally, right? The internal array can be accessed very easily. So what's the actual reason why you can't use vector? Commented Sep 23, 2014 at 15:03

3 Answers 3

4

No. You do need an array here. More specifically, you need a heterogenous container.

With the magic of indirection, you can actually use a homogenous container at the C++ level to achieve these semantics:

std::vector<std::vector<WhateverYourTypeIs>> v;

If you really need only five in the outer array, this will do the job:

std::array<std::vector<WhateverYourTypeIs>, 5> a;

and will simplify your code.

Sign up to request clarification or add additional context in comments.

1 Comment

@Galik: Right, and the OP said "I have lots of arrays with different sizes". That means different types. Granted, now that I'm introducing indirection (via vector) I suppose it's not really heterogenous any more, in the strictest sense.
3

If you are really forced to use plain arrays (and only then), consider using an array of pointers too:

int* const array[] = {arr1, arr2, arr3, arr4, ...};

Thus, you get a pointer to the first element of array arr# with array+#.

You might want a companion-array storing the element counts, too:

size_t carray[] = {sizeof arr1/sizeof *arr1, sizeof arr1/sizeof *arr2,
    sizeof arr1/sizeof *arr3, sizeof arr1/sizeof *arr4, ...};

Also, look at this for a safer way to get the element-count of an array:

template<size_t N, class T>
constexpr size_t element_count(T (&arr)[N])
{return N;}

Comments

0

You are confusing compile/build time and runtime. I would suppose to use only runtime objects like arrays/vectors.

But you could also use macros or variable argument lists: http://www.cprogramming.com/tutorial/c/lesson17.html

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.