要保存「單詞」(字符串)的二維數組,需要三維字符數組,因爲字符串是一維字符數組。
您的代碼應的東西如下所示:
int i = 0; // current position in the 2-dimensional matrix
// (if it were transformed into a 1-dimensional matrix)
int o = 0; // character position in the string
int nMax = 20; // rows of your matrix
int mMax = 3; // columns of your matrix
int oMax = 20; // maximum string length
char a[nMax][mMax][oMax] = {0}; // Matrix holding strings, zero fill to initialize
char delimiter = '|';
while (db.get(ch)) { // Assumes this line fills ch with the next character from the stream
if (ch == delimiter) {
i++; // increment matrix element
o = 0; // restart the string position
}
else {
o++; // increment string position
a[i/mMax][i % mMax][o] = ch;
}
}
對於輸入流"one|two|three|four|five|six|seven"
這將返回一個字符串,它看起來像一個數組:
{{"one", "two", "three"}, {"four", "five", "six"}, {"seven"}}
請改善您的代碼。什麼是數據庫?什麼是其他一切? –
done ............ –