简体   繁体   中英

How can I achieve this result in c++? Arrays pointing to arrays

I'm trying to do the following operations using c/c++. In order to explain how the code should work, I've written this example python code.

A=[1,2]
B=[3,4]
C=[5,6]

X=[A,B,C]

print(X[2][1]) ##this should print the second element of the third array (6)

When in python I create a list of precedently created list, it's just like defining a bidimensional array(obviously without the possibility to call by name each sub-array).

In c/c++ I'm not able to do this. I read on the internet that when I point to an array, I will always get the first element, is it true?

I'm able to create a bidimensional array in c++, but i need to define first each array (A,B,C) and then list all of them in the big array X. How can I achieve this task?

Here:

int A[]={1,2};
int B[]={3,4};
int C[]={5,6};
int* X[]={A,B,C};
printf("%d\n",X[2][1]);

You can also use std::vector for this job.

int A[]={1,2};
int B[]={3,4};
int C[]={5,6};
std::vector<int> a(A,A+1);
std::vector<int> b(B,B+1);
std::vector<int> c(C,C+1);
std::vector<std::vector<int> > x;
x.push_back(a); x.push_back(b); x.push_back(c);
printf("%d\n",x[2][1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM