abnormal program termination
//对于string对象 line1 line2 line3
//sentence = line1 + line2 + line3
//计算sentence中有多少个单词,并指出最长和最短的单词
//如果有多个最长和最短的单词,则将他们全部输出
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string line1 = "We were her pride of 10 she named us:";
string line2 = "Benjiamin, Phoenix, the Prodigal";
string line3 = "and perspicacious pacific Suzanne";
string sentence = line1 + ' ' + line2 + ' ' + line3;
string separators(" \t:,\v\n\f\r"); //分隔字符
string word;
//最长,最短,单次长度,单词数目
string::size_type maxLen,minLen,wordLen,count = 0;
//存放最长最短单词的容器
vector<string> longestWords,shortestWords;
//用来确定单词的起始位置,终止位置
string::size_type startPos = 0, endPos = 0;
//每次循环处理一个单词
while ((startPos = sentence.find_first_not_of(separators,endPos)) != string::npos) //确定单词初始位置startPos
{
++count;
//确定单词结束位置endPos
endPos = sentence.find_first_of(separators,endPos);
if (endPos == string::npos)
{
wordLen = sentence.size() - startPos; //最后一个单词
}
else
{
wordLen = endPos - startPos;
}
word.assign(sentence.begin() + startPos,sentence.begin() + endPos); //获取单词
//设置下一次的查找位置
startPos = sentence.find_first_not_of(separators, endPos);
if (count == 1) //找到的是第一个单词
{
maxLen = minLen = wordLen;
longestWords.push_back(word);
shortestWords.push_back(word);
}
else
{
if (wordLen > maxLen) //当前单词比目前的最长单词更长
{
maxLen = wordLen;
longestWords.clear(); //清空
longestWords.push_back(word);
}
else if (wordLen == maxLen) //等长
longestWords.push_back(word);
if (wordLen < minLen) //当前单词比目前的最短单词更短
{
minLen = wordLen;
shortestWords.clear(); //清空
shortestWords.push_back(word);
}
else if (wordLen == minLen) //等长
shortestWords.push_back(word);
}
}
//输出单词数目
cout<< "word amount: " << count <<endl;
vector<string>::iterator iter;
//输出最长单词
cout << "longest word(s):" << endl;
iter = longestWords.begin();
while (iter != longestWords.end())
cout << *iter++ <<endl;
//输出最短单词
cout << "shortest word(s):" <<endl;
iter = shortestWords.begin();
while (iter != shortestWords.end())
cout << *iter++ <<endl;
return 0;
}
//调试没error有warning,运行中止
[解决办法]
#include<iostream>#include<string>#include<vector>using namespace std;int main(){ string line1 = "We were her pride of 10 she named us:"; string line2 = "Benjiamin, Phoenix, the Prodigal"; string line3 = "and perspicacious pacific Suzanne"; string sentence = line1 + ' ' + line2 + ' ' + line3+' '; //后面还要加个结束符 string separators(" \t:,\v\n\f\r"); //分隔字符 string word; //最长,最短,单次长度,单词数目 string::size_type maxLen,minLen,wordLen,count = 0; //存放最长最短单词的容器 vector<string> longestWords,shortestWords; //用来确定单词的起始位置,终止位置 string::size_type startPos = 0, endPos = 0; //每次循环处理一个单词 while ((startPos = sentence.find_first_not_of(separators,endPos)) != string::npos) //确定单词初始位置startPos { ++count; //确定单词结束位置endPos endPos = sentence.find_first_of(separators,endPos); if(startPos>=endPos) { ++endPos; continue; } if (endPos == string::npos) { wordLen = sentence.size() - startPos; //最后一个单词 } else { wordLen = endPos - startPos; } word.assign(sentence.begin() + startPos,sentence.begin() + endPos); //获取单词 //设置下一次的查找位置 ++endPos; //加1,使往后移一个位置,往后查找 startPos = sentence.find_first_not_of(separators, endPos); if (count == 1) //找到的是第一个单词 { maxLen = minLen = wordLen; longestWords.push_back(word); shortestWords.push_back(word); } else { if (wordLen > maxLen) //当前单词比目前的最长单词更长 { maxLen = wordLen; longestWords.clear(); //清空 longestWords.push_back(word); } else if (wordLen == maxLen) //等长 longestWords.push_back(word); if (wordLen < minLen) //当前单词比目前的最短单词更短 { minLen = wordLen; shortestWords.clear(); //清空 shortestWords.push_back(word); } else if (wordLen == minLen) //等长 shortestWords.push_back(word); } } //输出单词数目 cout<< "word amount: " << count <<endl; vector<string>::iterator iter; //输出最长单词 cout << "longest word(s):" << endl; iter = longestWords.begin(); while (iter != longestWords.end()) cout << *iter++ <<endl; //输出最短单词 cout << "shortest word(s):" <<endl; iter = shortestWords.begin(); while (iter != shortestWords.end()) cout << *iter++ <<endl; return 0;}