0

我正在尝试从字典文件中读取单词,并且在从文件中读取的第 4 行收到段错误。从我读过的关于 C++ 中的向量的内容来看,要使用插入命令,我需要指定一个迭代器以用作占位符。为此,我在向量的开头实例化了一个迭代器。

 vector<string>::iterator it = dictionaryFile.begin();

在我的 for 循环中,我使用增量运算符递增迭代器指向的位置

it++;

我基于www.cplusplus.com上的示例。迭代器适用于前 3 行,然后发生段错误。我已经仔细检查了我的文件,以确保 getline 没有读取错误字符。我相信这个错误与我为确保向量没有溢出而进行的保留调用有关。

由于我在声明时没有使用关键字 new,所以向量的容量是否限制为 5?我找不到任何使用新关键字的例子!

我已经包含了导致此错误的代码段。请注意我已经重新设计了几次以试图解决这个段错误,这不是我最初的实现。我将不胜感激 C++ 大师可能拥有的任何见解。感谢您的时间。

vector<string> dictionaryFile (5, "");                                                                     //Declaration of vector that will hold the words in the dictionary

ifstream input;                                                                                                  //Declaration of input file stream
input.open(inst.c_str());

/********************** Test to see if file was opened ********************************/                
if(!input){
    cerr << "The file " << inst <<" does not exists in this directory" << '\n';
}
/********************** File is successfully opened**********************************/

string temporaryProcessingString = "";                                                                //This string will temporarily hold values read in from the file
vector<string>::iterator it = dictionaryFile.begin();                                                 //Creates iterator to step through the vector and fix this wild shit

for(int i = 0; getline(input, temporaryProcessingString); i++){                                 //See I can follow directions given in class no eof used.
    cout << "Does this print before SegFault 1 " << endl;
    if(dictionaryFile.size() >= (dictionaryFile.capacity() - dictionaryFile.size()) ){       //If current size is greater the 50% capacity reserve size*2 more memory
        int oldSize;
        oldSize = dictionaryFile.size();
        cout << "Does this print before SegFault 2 " << endl;
        dictionaryFile.reserve(oldSize + oldSize);                                                  //Reservation new capacity for vector containing dictionary
    }

/** this is a test bracket that solely keeps track of the vectors size and capacity in the terminal COMMENT OUT BEFORE SUBMITTING*/
cout << "________________________________________________" << '\n';
cout << "Loop Run: " << i << endl;
cout << "Size: " << dictionaryFile.size() << ", Capacity: " << dictionaryFile.capacity() << endl;
cout << "________________________________________________" << '\n';
dictionaryFile.insert(it, temporaryProcessingString); /*******************************THIS LINE CAUSES THE SEGFAULT! UNFORTUNATELY IT IS ALSO THE LINE THAT MOVES THE DATA INTO THE VECTOR************************/
it++;
cout << "Dictionary Entry: " << dictionaryFile[i] << endl;

}
cout << "Dictionary Entry Primary Test: " << dictionaryFile[0] << endl;
4

1 回答 1

7

When your vector's size approaches its current capacity (or, in your case, when you call vector::reserve, see the section on iterator validity here), an internal algorithm will re-allocate it and potentially move it somewhere else, making all iterators to elements in the vector invalid.

As your program only seems to insert at the end anyways, use vector::push_back instead of vector::insert to prevent this. You can then also skip the reserveing - the internal algorithms are quite good (you don't have to be afraid of the vector doing reallocation for every single new element - the algorithms are smarter than that).

于 2013-02-13T01:54:54.590 回答