我想知道有没有人知道如何将字符串转换为二维数组?这是我的尝试:
string w;
char s[9][9];
int p=0;
getline(cin, w);
while(p != w.size())
{
for (int k = 0; k < 9; k++)
{
for(int j = 0; j < 9; j++)
{
s[k][j] = w[p];
p++;
}
}
}
cout << "nums are: " << endl;
for(int k = 0; k < 9; k++)
{
for(int j = 0; j <9; j++)
{
cout << s[k][j];
}
}
但数字不能正确打印出来。我希望s [k] [j]打印出w中的所有内容,但它只是打印出乱码。我也注意到如果我做字符串[81]然后我得到了一大堆错误。谁能帮助我?感谢。
答案 0 :(得分:1)
试试这个:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
exit(1);
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
}
}
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
根据我们的聊天情况,您可能需要这样:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
while (!cin.eof())
{
bool bad_input = false;
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
continue;
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
bad_input = true;
break;
}
}
if (bad_input)
continue;
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
}
答案 1 :(得分:0)
你还没有描述你想要做的很好而且你没有描述你遇到的问题,所以以下内容只是基于猜测。
所以看起来你要做的就是采用如下字符串:
快速的棕色狐狸跳过懒狗。
将其放入2D数组中,如:
0 1 2 3 4 5 6 7 8
0 T h e q u i c k
1 b r o w n f o
2 x j u m p e d
3 o v e r t h e
4 l a z y d o g s
5 . x x x x x x x x
6 x x x x x x x x x
7 x x x x x x x x x
8 x x x x x x x x x
您的代码有一个问题是,当您将w
中的值复制到s
时,您无法确保索引p
实际上在范围内。您似乎试图在while(p != w.size())
行中处理此问题;但这是一个外部循环,它不会保护p
不会超出边界并在内部循环中使用。相反,你必须将p++; if (p==w.size()) break;
之类的东西放在最内圈的p
增量内。或者更好的是,您应该遍历字符串而不是数组。类似下面的伪代码会替换整个while(p){for(k){for(j){}}}
循环集。:
for(size_t i=0; i<w.size(); ++i) {
int k = compute target row from i
int j = compute target column from i
s[k][j] = w[i]
}
此外,这里有一些代码可以在您调试时更好地可视化数组。
#include <iostream>
int main() {
char s[9][9] = {"The","quick","brown","fox","jumped","over","the","lazy","dogs."};
// your code to get input and copy it into the array goes here
//
// for(size_t i=0; i<w.size(); ++i) {
// int k = compute target row from i
// int j = compute target column from i
// s[k][j] = w[i]
// }
std::cout << " 0 1 2 3 4 5 6 7 8\n";
for (int i=0; i<9; ++i) {
std::cout << i;
for (int j=0; j<9; ++j)
std::cout << ' ' << s[i][j];
std::cout << '\n';
}
}
如果您在没有任何更改的情况下运行此程序,则输出应如下所示:
0 1 2 3 4 5 6 7 8
0 T h e
1 q u i c k
2 b r o w n
3 f o x
4 j u m p e d
5 o v e r
6 t h e
7 l a z y
8 d o g s .