我已经编写了代码来存储2D数组中的值,如下所示格式,并且工作正常。
1. Menu1
1.1 Menu1.1
1.2 Menu1.2
2. Menu2
但我想将数组值存储在变量中,然后赋值变量(其中包含数组值)以填充MyPoints数组,它不起作用!!
//Create array
double MyPoints[25][3] = {{4,3,1},{6,4,1},{7,3,1},{7,6,1},{6,7.5,1},{3,6,1},{4,5,2},{5,6.5,2},{6,9,2},{3.5,8,2},{6,5,3},{6,6,3},{9,4,3},{9,5,3},{8,4,4},{9.5,3,4},{10,4,4},{11,6,4},{9,6,4},{8,7,5},{10,7,5},{11,9,5},{8,10,5}} ;
cout << "Array Filled" << endl;
// Print Array
for (int c = 0; c < 25; c++)
{
for (int d = 0; d < 3; d++)
{
cout << MyPoints [c][d] << " Hellow ";
}
cout << endl;
}
我做错了什么?如何纠正它。
答案 0 :(得分:0)
#include <iostream>
int main()
{
double MyPoints[25][3] = {{
0,
}};
std::string text = "{4,3,1},{6,4,1},{7,3,1},{7,6,1},{6,7.5,1},{3,6,1},{4,5,2},{5,6.5,2},{6,9,2},{3.5,8,2},{6,5,3},{6,6,3},{9,4,3},{9,5,3},{8,4,4},{9.5,3,4},{10,4,4},{11,6,4},{9,6,4},{8,7,5},{10,7,5},{11,9,5},{8,10,5}";
for (int c = 0; c < 25; c++)
{
std::sscanf(text.c_str(), "{%lf,%lf,%lf},", &MyPoints[c][0], &MyPoints[c][1], &MyPoints[c][2]);
text = text.substr(text.find("},") + 2);
}
// Print Array
for (int c = 0; c < 25; c++)
{
for (int d = 0; d < 3; d++)
std::cout << MyPoints[c][d] << " Hellow ";
std::cout << std::endl;
}
}
不使用正则表达式或字符串流的代码(几乎是C,而不是C ++)。您还可以按std::cin >> M[x][y]
或M[x][y]=value
修改二维数组的值。