C++使用vector实现N行n列的多维数组
找了很多资料,都是要先输入行数和列数,想不明白,我都用vector了,肯定是用的不定长啊,经过不断的测试,终于实现一种,list目前知道怎么添加,但是遍历不出来
double target_1(double x) //目标函数1
{
double y1;
y1 = 3 * pow(x, 2);
return y1;
}
double target_2(double x)//目标函数2
{
double y2;
y2 = 4 - 5 * pow(x, 2);
return y2;
}
vector<vector<double>> feasible_solution()
{
double step = 1;
double y1, y2;
vector<vector<double>> value_2;
//vector<double> value_1;
for (double s = -10; s <= 10;)
{
y1 = target_1(s);
y2 = target_2(s);
value_2.push_back({ y1,y2 });
s += step;
}
return value_2;
}
int main()
{
vector<vector<double>> value_c;
value_c = feasible_solution();
for (int i = 0; i < value_c.size(); i++)
{
for (int j = 0; j < value_c[0].size(); j++)
{
cout << value_c[i][j] << " ";
}
cout << endl;
}
}
结果:
300 -496
243 -401
192 -316
147 -241
108 -176
75 -121
48 -76
27 -41
12 -16
3 -1
0 4
3 -1
12 -16
27 -41
48 -76
75 -121
108 -176
147 -241
192 -316
243 -401
300 -496
请按任意键继续. . .
程序不难,就不注释了,知道怎么用vector就能看出来。C++不比python,对数组和列表的操作稍显复杂。