我想從文件中讀取單詞並將它們存儲到向量中,但索引不起作用。讓它保持seg故障的原因是什麼?爲什麼push_back()起作用?使用索引和push_back()之間的機制差異是什麼?在向量中存儲字符串有seg故障
vector<string> readWordToArray(string fileName, int wordCount){
vector<string> wordArray;
fstream inFile;
inFile.open(fileName);
string word;
int index = 0;
while(inFile >> word){
// doesnt work, need to change to wordArray.push_back(word);
wordArray[index] = word;
index++;
}
return wordArray;
}
你讀過['vector :: push_back'文檔](http://www.cplusplus.com/reference/vector/vector/push_back/)和['vector :: operator []'documentation](http://www.cplusplus.com/reference/vector/vector/operator [] /)並注意區別? –