-2
我正在學習C++,並使自己成爲超過10,000行的文本文件。我試圖創建一個字符串數組,並將第一行插入到第一個數組中,將第二行插入到第二個數組中,依此類推。以下是我迄今所做的:如何在C++中分配字符串數組?
ifstream theFile;
string inputFile;
cin >> inputFile;
theFile.open(inputFile.c_str());
const unsigned int ARRAY_CAP = 64U;
string line;
string *lineArr = new string[ARRAY_CAP];
if (theFile.is_open()) {
int lineNumber = 0;
while (!theFile.eof()) {
getline(theFile, line);
lineArr[i] = line;
i++;
}
}
我的一個朋友告訴我分配的字符串數組,因爲我跑出來的內存,但我什至不知道該怎麼做。我怎麼能夠分配字符串數組?
爲什麼不使用'std :: vector''?此外,「while(!theFile.eof())」不會去做你想要的。 –
使用[std :: vector](http://en.cppreference.com/w/cpp/container/vector)忘記分配。 –
@JesperJuhl我也想知道如何分配數組。我的哥哥告訴我,他們一直在大學裏分配陣列。 :/ – James