0

I want to be able to have an empty array when the program starts, fill it up during runtime, and also remove things inside of it. This would probably have to be a multidimentional array. So, how would I do this?

Ex.

int randomNumber;
int RandomString;
int array[] = {};
for(int i;int i < 10; int i ++){

array + random variable;
array + random string;

}

So, after it would look like:

array[] = {{randomint,randomstring},{randomint, rrandomstring}}

Is this possible?

4

4 回答 4

6

You'd do it using a std::vector and push_back() instead. Nuff' said.

于 2012-09-24T22:56:58.227 回答
0

您只需要考虑您希望使用的容器是列表、地图还是矢量,它们都有优点和缺点。在做出决定之前做更多的功课,我的直觉是一个向量或一个列表列表,因为你想要一个空的,然后添加到它。考虑是否要订购此 Container 以及是否要枚举它。

于 2012-09-24T23:25:23.880 回答
0

如果要将预定义变量添加到数组中,可能需要保存两件事,一是变量的值,二是变量本身

如果你想拥有非常简单的变量值,如果你想把它放在数组上而不是在某些动态数据类型上,那么你必须声明一个大小有限的数组来动态扩展数组,你必须使用某种类型像向量或链表这样的数据结构,并在那里添加变量的值,但是如果你想保存它自己的变量,那么你必须声明指针数组并存储变量的地址,这样你就可以能够通过使用该数组访问这些变量。

于 2012-09-24T23:02:42.123 回答
0

您需要做的是构建一个矢量地图,其中包含一对。
然后,当您遍历数据时,您会创建一个新对并分配您需要的值。
然后将其推入向量中。

int randomNumber = 0;
string randomString = "test";

map< int, vector <pair< int, string >>> myArray;

for ( int i = 0; i < 10; i++ )
{
    pair<int, string> p;
    p.first = randomNumber;
    p.second = randomString;

    myArray[i].push_back(p);
}
于 2012-09-25T08:28:17.373 回答