C++ std::bad_alloc error with std::vector
C++ std::bad_alloc error with std::vector
我知道这个问题以前有人问过,但答案似乎对提问者的代码太具体了。
如果这被认为是一个无用的重复问题,请随时将其删除或标记为重复。
据我所知,我的代码对于我试图实现的目标是正确的,即计算文本文件中指定单词的出现次数。我将不胜感激任何帮助,谢谢。
错误
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
我的主要部分.cpp(导致问题)
//vectors to hold words from two files
vector<string> wordlist;
vector<string> file;
vector<int> searchVec;
//while a is not an empty string
while (a != "") {
//getNextWord() returns each word in order from a text file
//a is a play script, b is a list of words to search for
a = infile.getNextWord();
b = infile2.getNextWord();
file.push_back(a);
//counts total words in play script
count++;
//increments and adds each word from the list of words to search for to the vector
for (int i = 0; b != ""; i++) {
wordlist.push_back(b);
}
//cycles through words to search for
for (int j = 0; j < wordlist.size(); j++) {
//cycle through te play script
for (int k = 0; k < file.size(); k++) {
//if the current play script word is the same as the test word, up the count
if (file[k] == wordlist[j]) {
searchCount++;
}
}
//print appropriate output, re-initialise the count to 0 for the next word
cout << "The word " << wordlist[j] << " occurs " << searchCount << " times." << endl;
searchCount = 0;
}
}
你认为这个循环是如何终止的?
for (int i = 0; b != ""; i++) {
wordlist.push_back(b);
}
相关文章:
- Problems with std::cin.fail()
- std::adjacent_difference with std::chrono time_point
- specialized std::default_delete with QQmlComponent
- Threads with Classes and std::packaged_task
- std::array initalization with Visual Studio 2010
- 如何使用 std::swap with array?
- 返回 std::map with get 方法
- std::getline with std::fstream
- Printing CComBSTR with iostream (std::wcout)
- std::p riority_queue<struct with const member&> - 'operator =' 函数不可用
- CreateProcess with std::string
- 在 lambda(parameter) 中为 std::transform with std::sregex_token
- Spirit X3, ascii::cntrl why disparity with std::iscntrl?
- EventTask with std::condition_variable
- C++20 with u8, char8_t and std::string
- std::function std::bind with lambda overload ambiguity
- 如何使用 std::sort with pair and references
- Returning std::vector with std::move
- dlopen 从 C C++ lib,然后使用 std::string with function
- 如何使用 std::transform with container without push_back.