我想从文件中读取单词并将它们存储到向量中,但索引不起作用。让它保持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 [] /)并注意区别? –