您的位置:首页 > 产品设计 > UI/UE

字符串中统计单词个数

2015-12-27 20:49 302 查看
用到的几个小知识 :

1.字符串流是以空格作为一个字符串的结束标志,采用stringstream来提取单词。

2.为了消除重复单词,需要先将vector排序,使得重复的单词相邻出现,不先排序无法正确“删除”重复元素

3.使用unique STL算法来重排vector,使得不重复的元素出现在vector的开始部分。

代码如下:

#include "stdafx.h"
#include <string.h>
#include <algorithm>
#include <vector>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <sstream>
#include <iterator>
#include <functional>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

stringstream ss;
string str = "the quick red fox jumps over the slow red turtle";
string strWord;
vector<string> vecWords;

ss.clear();
ss.str(str);
while (ss >> strWord)
{
vecWords.push_back(strWord);
strWord.clear();
}

cout << "sort before:" << endl;
copy(vecWords.begin(), vecWords.end(), ostream_iterator<string>(cout, " "));
cout << endl;

cout << "sort after and unique before:" << endl;

//从大到小排序
//sort(vecWords.begin(), vecWords.end(),greater<string>());
//默认是从小大的字典排序
sort(vecWords.begin(), vecWords.end());
copy(vecWords.begin(), vecWords.end(), ostream_iterator<string>(cout, " "));
cout << endl;

vector<string> vecWordsBackup(vecWords);

cout << "sort and unique afer:" << endl;
unique(vecWords.begin(),vecWords.end());
copy(vecWords.begin(), vecWords.end(), ostream_iterator<string>(cout, " "));
cout << endl;

cout << "sort and unique and erase after:" << endl;
vecWordsBackup.erase(unique(vecWordsBackup.begin(),vecWordsBackup.end()), vecWordsBackup.end());
copy(vecWordsBackup.begin(), vecWordsBackup.end(), ostream_iterator<string>(cout, " "));
cout << endl;

cout << "total words:"<<vecWordsBackup.size() <<endl;

return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息