【发布时间】:2013-07-14 15:26:57
【问题描述】:
我正在尝试将文本文件中的字符串值添加到向量中。在这样做的同时,我还在检查字符串中的字符数,然后当到达具有所需字符数的单词时,我将该字符串添加到向量中。
但是当我调试我的程序时,我一直看到越界异常,它打印出的字符串比所需的字符数多。
vector<string> words;
vector<string> chosenWords;
ifstream in("WordDatabase.txt");
while(in) {
string word;
in >> word;
cout << word << endl;
//push in selected words based on num of chars
words.push_back(word);
}
for(vector<string>::iterator itr=words.begin(); itr!=words.end();++itr)
{
if((*itr).length() >= 2 || (*itr).length() <= 7)
{
cout << (*itr) << endl;
chosenWords.push_back(*itr);
}
}
【问题讨论】:
-
越界错误对应哪一行?
-
长度为 2+ 或 7- 这听起来很奇怪。确定你说的不是 && 吗?
-
该死的。你在 2 分钟内解决了它,而我花了 2-3 小时思考它。谢谢博格!
-
@KerrekSB 哈你什么意思?
标签: c++ vector iterator text-files