泛型算法——容器元素的排序算法(统计长度不小于6的单词个数)sort unique stable_sort count_if
2007-03-09 11:07
302 查看
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
// 打印容器内容
void printVec(const vector<string> &vecT)
{
vector<string>::const_iterator vecIt= vecT.begin();
for (; vecIt != vecT.end(); vecIt ++)
{
cout<<*vecIt<<endl;
}
cout<<endl;
}
void iniVec( vector<string> &vecT)
{
vecT.erase(vecT.begin(),vecT.end());
char ch = 'a';
string strT = "";
for (int i=0;i<10;i++)
{
vecT.push_back(strT+ch++);
}
}
void printList(const list<string> &listT)
{
list<string>::const_iterator listIt= listT.begin();
for (; listIt != listT.end(); listIt ++)
{
cout<<*listIt<<endl;
}
cout<<endl;
}
bool isShorter(const string &s1,const string &s2)
{
return s1.size() < s2.size();
}
bool GT6(const string &s)
{
return s.size()>=6;
}
string make_plural(size_t st,const string &s1,const string &s2)
{
return st>1 ? s1+s2 : s1;
}
int main()
{
cout<<"以下是容器元素的排序算法(统计长度不小于6的单词个数)"<<endl;
vector<string> vecTest;
string next_word;
while(cin>>next_word)
{
vecTest.push_back(next_word);
if(vecTest.size()>3 )
break;
}
printVec(vecTest);
sort(vecTest.begin(),vecTest.end());
printVec(vecTest);
vector<string>::iterator end_unique =
unique(vecTest.begin(),vecTest.end());
vecTest.erase(end_unique , vecTest.end());
stable_sort(vecTest.begin(),vecTest.end(),isShorter);
printVec(vecTest);
vector<string>::size_type wc =
count_if(vecTest.begin() , vecTest.end(),GT6);
cout<<wc <<" "<<make_plural(wc,"word","s")
<< " 6 characters or longer"<<endl;
getch();
return 0;
}
输出
以下是容器元素的排序算法(统计长度不小于6的单词个数)
sdfadfdfsa
asdfasdf
asdfasd
as
sdfadfdfsa
asdfasdf
asdfasd
as
as
asdfasd
asdfasdf
sdfadfdfsa
as
asdfasd
asdfasdf
sdfadfdfsa
3 words 6 characters or longer
#include <conio.h>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
// 打印容器内容
void printVec(const vector<string> &vecT)
{
vector<string>::const_iterator vecIt= vecT.begin();
for (; vecIt != vecT.end(); vecIt ++)
{
cout<<*vecIt<<endl;
}
cout<<endl;
}
void iniVec( vector<string> &vecT)
{
vecT.erase(vecT.begin(),vecT.end());
char ch = 'a';
string strT = "";
for (int i=0;i<10;i++)
{
vecT.push_back(strT+ch++);
}
}
void printList(const list<string> &listT)
{
list<string>::const_iterator listIt= listT.begin();
for (; listIt != listT.end(); listIt ++)
{
cout<<*listIt<<endl;
}
cout<<endl;
}
bool isShorter(const string &s1,const string &s2)
{
return s1.size() < s2.size();
}
bool GT6(const string &s)
{
return s.size()>=6;
}
string make_plural(size_t st,const string &s1,const string &s2)
{
return st>1 ? s1+s2 : s1;
}
int main()
{
cout<<"以下是容器元素的排序算法(统计长度不小于6的单词个数)"<<endl;
vector<string> vecTest;
string next_word;
while(cin>>next_word)
{
vecTest.push_back(next_word);
if(vecTest.size()>3 )
break;
}
printVec(vecTest);
sort(vecTest.begin(),vecTest.end());
printVec(vecTest);
vector<string>::iterator end_unique =
unique(vecTest.begin(),vecTest.end());
vecTest.erase(end_unique , vecTest.end());
stable_sort(vecTest.begin(),vecTest.end(),isShorter);
printVec(vecTest);
vector<string>::size_type wc =
count_if(vecTest.begin() , vecTest.end(),GT6);
cout<<wc <<" "<<make_plural(wc,"word","s")
<< " 6 characters or longer"<<endl;
getch();
return 0;
}
输出
以下是容器元素的排序算法(统计长度不小于6的单词个数)
sdfadfdfsa
asdfasdf
asdfasd
as
sdfadfdfsa
asdfasdf
asdfasd
as
as
asdfasd
asdfasdf
sdfadfdfsa
as
asdfasd
asdfasdf
sdfadfdfsa
3 words 6 characters or longer
相关文章推荐
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- 《C++ Primer》学习笔记:map容器insert操作的使用——编写程序统计并输出所读入的单词出现的次数
- Python 列表count()函数元素次数统计
- 启动Spark Shell,在Spark Shell中编写WordCount程序,在IDEA中编写WordCount的Maven程序,spark-submit使用spark的jar来做单词统计
- 关联容器map应用-统计单词出现频率
- stl 的find_if算法用来查找容器内的符合条件的元素
- C++统计单词个数及排序:容器按照value排序
- 运行Hadoop自带的wordcount单词统计程序
- 最近用来练手的一个小程序-----统计一遍文章里单词的长度并以直方图表示
- remove/remove_if算法配合容器的erase方法实现容器删除元素功能
- hive学习之WordCount单词统计
- 用array_count_values统计一篇英文文档中每个单词的出现次数,结果用表格展示出来
- HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
- python之单词统计(words count)
- 单词统计word count
- Uva156 map容器的使用 sortvecor And map.count
- 《征服c指针》学习笔记-----统计文本单词数目的程序word_count