电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > C语言 > 正文
using STL sort
using STL sort
2008-9-15 12:25:02  文/佚名   出处:中国自学编程网   
1 #include "iostream"
2 #include "string.h"
3 #include "iostream"
4 #include "fstream"
5 #include <vector>
6 #include <algorithm>
7
8 using namespace std;
9 typedef struct url_n_time
10 {
11 string url;
12 int times;
13 bool operator <(const url_n_time& i1)const
14 {
15 return times < i1.times;
16 }
17 }url_n_time;
18 /*
19 bool operator <(const url_n_time& i1, const url_n_time i2)
20 {
21 if(i1.times >= i2.times)
22 {
23 return true;
24 }
25 else
26 {
27 return false;
28 }
29 }*/
30 vector<url_n_time> vec_url_n_time(0);
31
32 bool url_times_cmp(const url_n_time& url1, const url_n_time& url2)
33 {
34 if(url1.times >= url2.times)
35 {
36 return true;
37 }
38 else
39 {
40 return false;
41 }
42 }
43
44 int main()
45 {
46 char ch_array[201];
47 ifstream in("1.txt");
48 string str;
49 string str_last;
50 vector<string> vec_str(0);
51 int lineNum=0;
52 url_n_time url_n_time_temp;
53 in.getline(ch_array, 200);
54 str_last = ch_array;
55 url_n_time last_url_data;
56 last_url_data.url = str_last;
57 last_url_data.times = 1;
58 while(in.getline(ch_array,200))
59 {
60 lineNum++;
61 str = ch_array;
62 if( !str.compare(str_last) )
63 {
64 ++last_url_data.times;
65 }
66 else
67 {
68 vec_url_n_time.push_back(last_url_data);
69 str_last = str;
70 last_url_data.url = str_last;
71 last_url_data.times = 1;
72 }
73 }
74 vec_url_n_time.push_back(last_url_data);
75 cout << "total urls:" << vec_url_n_time.size() << endl;
76 /*
77 sort(vec_url_n_time.begin(), vec_url_n_time.end());
78 */
79 sort(vec_url_n_time.begin(), vec_url_n_time.end(),url_times_cmp);
80
81 vector<url_n_time>::iterator it;
82 for(it=vec_url_n_time.begin(); it!=vec_url_n_time.end(); it++)
83 {
84 /*cout << "url:" << it->url.c_str() << "times:" << it->times << endl;*/
85 cout << it->url.c_str() << " " << it->times << endl;
86 }
87 return 0;
88 }
89
  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    用C语言实现的闹钟程序
    C#实现Windows关机的代码
    用动态规划法求组和数的算法
    循环结构程序具有哪3个组成部分?
    const成员函数有什么特点?
    c语言求平均分程序
    一道关于指针的C语言题
    C语言求3个整数的最大公约数
    BM模式匹配算法C语言实现
    从键盘输入任意5个4位整数存入数组a…
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号