#include "iostream"
#include "string.h"
#include "iostream"
#include "fstream"
#include <vector>
#include <algorithm>
using namespace std;
typedef struct url_n_time
{
string url;
int times;
bool operator <(const url_n_time& i1)const
{
return times < i1.times;
}
}url_n_time;
/*
bool operator <(const url_n_time& i1, const url_n_time i2)
{
if(i1.times >= i2.times)
{
return true;
}
else
{
return false;
}
}*/
vector<url_n_time> vec_url_n_time(0);
bool url_times_cmp(const url_n_time& url1, const url_n_time& url2)
{
if(url1.times >= url2.times)
{
return true;
}
else
{
return false;
}
}
int main()
{
char ch_array[201];
ifstream in("1.txt");
string str;
string str_last;
vector<string> vec_str(0);
int lineNum=0;
url_n_time url_n_time_temp;
in.getline(ch_array, 200);
str_last = ch_array;
url_n_time last_url_data;
last_url_data.url = str_last;
last_url_data.times = 1;
while(in.getline(ch_array,200))
{
lineNum++;
str = ch_array;
if( !str.compare(str_last) )
{
++last_url_data.times;
}
else
{
vec_url_n_time.push_back(last_url_data);
str_last = str;
last_url_data.url = str_last;
last_url_data.times = 1;
}
}
vec_url_n_time.push_back(last_url_data);
cout << "total urls:" << vec_url_n_time.size() << endl;
/*
sort(vec_url_n_time.begin(), vec_url_n_time.end());
*/
sort(vec_url_n_time.begin(), vec_url_n_time.end(),url_times_cmp);
vector<url_n_time>::iterator it;
for(it=vec_url_n_time.begin(); it!=vec_url_n_time.end(); it++)
{
/*cout << "url:" << it->url.c_str() << "times:" << it->times << endl;*/
cout << it->url.c_str() << " " << it->times << endl;
}
return 0;
}
分享到:
相关推荐
详细解说 STL 排序(Sort)
详细解说STL排序 详细解说STL排序 详细解说STL排序
STL_Sort 排序的资料 STL_Sort 排序的资料 STL_Sort 排序的资料
对于C++的STL的双向链表,排序算法有的模板并没有实现,因此给出来,大家参考。
一个定义了整数排序的工程,相比课本上的要更加高效和简洁
STL对于C++编程者而言,相信都非常喜爱吧。但是其中的排序准则,你亲自试过修改吗?如何修改?请参考本文档的思路和实现过程吧。
用STL实现排序,里面用到Set , vector 等,程序简短,高效。
STL中的set和map都是自动排序的,但是如何修改其排序准则呢?本文档给出了修改思路和具体的实现代码。对于STL编程爱好者而言,实在是不可多得的好资料啊。
用STl实现基数排序算法 功能强大非常适合数据结构课程设计
详细解说 STL 排序,可以下载看看。了解多点stl排序问题
STL中的排序算法一览
简单举例普通数组、vector、对象排序 ,列举了默认排序、自定义排序,在自定义排序函数注释部分 点明了容易出问题的地方
使用VC++控制台应用程序编写,测试了:vector对象的排序,对象中的大小无序,有重复。
C++11中有无序map即: unordered_map 可以不自动排序, 那么C++98中只有map 如何避免自动排序。
该程序采用STL的sort排序函数,对CSV中的数据进行排序(排序时不包括首行(第0行)。可以按字符、数值和IP排序。排序方式还包括普通排序(整行作为一个参数进行比较)、将整行数据从指定列开始作为字符串起始按字符...
实现对文件读写的操作。利用STL实现对有很好的移植性
VC++2012 MFC实现调用TetGen对stl模型数据进行四面体剖分
本篇文章介绍了,关于STL中sort()对struct排序的方法。需要的朋友参考下
随便写的一个基数排序算法,很简单,用STL实现