longshen
C++博客
首頁
聯系
聚合
管理
隨筆 - 26 文章 - 6 trackbacks - 0
<
2025年6月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
常用鏈接
我的隨筆
我的評論
我參與的隨筆
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆分類
acm總結(3)
ASP.NET(1)
p2p(1)
poj(7)
VC++(6)
程序員(7)
隨筆檔案
2011年10月 (1)
2010年12月 (1)
2010年9月 (1)
2010年4月 (4)
2009年11月 (4)
2009年9月 (1)
2009年7月 (5)
2009年5月 (6)
2009年4月 (3)
朋友
cqh
大學室友...
搜索
最新評論
1.?re: acm博弈題 -- 個人小結[未登錄]
1730用博弈論是怎么做的?
--zj
2.?re: acm博弈題 -- 個人小結
樓主,你的博文中有一處錯誤, ((+)為 按位與)是錯的,(+)應該為按位異或
--913614263@qq.com
3.?re: 解決 error LNK2019: 無法解析的外部符號 問題
InternetGetCookieA
在用wininet庫吧。
--張
4.?re: poj 1947 Rebuilding Roads -- 樹形DP
good~好代碼,膜拜
--czy
5.?re: ACM總結 -- 退役貼
ym~
--july
閱讀排行榜
1.?解決 error LNK2019: 無法解析的外部符號 問題(50306)
2.?ACM總結 -- 退役貼(4813)
3.?acm博弈題 -- 個人小結(3110)
4.?poj 1947 Rebuilding Roads -- 樹形DP(2263)
5.?poj 1191 棋盤分割 -- 動態規劃(1594)
評論排行榜
1.?poj 1947 Rebuilding Roads -- 樹形DP(2)
2.?acm博弈題 -- 個人小結(2)
3.?解決 error LNK2019: 無法解析的外部符號 問題(1)
4.?ACM總結 -- 退役貼(1)
5.?Unicode與多字節的基本運用 -- Windows編程(0)
【轉】STL中sort的用法舉例
轉自:
http://blog.csdn.net/whitefoxx/archive/2010/07/03/5710848.aspx
對象數組排序這里展示了兩種方法,定義比較函數或通過重載比較運算符使得類本身是可以比較的,就像基本類型一樣。
定義比較函數,既可以通過定義比較運算符(如operator <),也可以直接定義函數(如compare)。
重載運算符之后,可以在sort函數中通過less或greater或less_equal等來調整升序還是降序,默認是升序。
另外,重載運算符后,函數bool operator < 就不要了,否則用g++編譯出錯。
#include
<
algorithm
>
#include
<
iostream
>
#include
<
vector
>
using
namespace
std;
class
MyClass
{
public
:
int
id;
MyClass()
{}
MyClass(
int
i): id( i )
{}
bool
operator
<
(
const
MyClass
&
b )
const
{
return
id
<
b.id;
}
bool
operator
>
(
const
MyClass
&
b )
const
{
return
id
>
b.id;
}
}
;
/**/
/*
bool operator < ( MyClass a, MyClass b )
{
return a.id < b.id;
}
*/
bool
compare( MyClass a, MyClass b )
{
return
a.id
<
b.id;
}
int
main()
{
//
數組
cout
<<
"
數組
"
<<
endl;
MyClass arr[
10
];
srand(time(NULL));
for
(
int
i
=
0
; i
<
10
; i
++
)
arr[i].id
=
rand()
%
101
;
cout
<<
"
before sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
arr[i].id
<<
endl;
sort(arr,arr
+
10
,less
<
MyClass
>
());
cout
<<
"
after sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
arr[i].id
<<
endl;
//
動態數組vector
cout
<<
"
動態數組vector
"
<<
endl;
vector
<
MyClass
>
list;
for
(
int
i
=
0
; i
<
10
; i
++
)
list.push_back( MyClass( rand()
%
101
) );
cout
<<
"
before sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list[i].id
<<
endl;
sort(list.begin(),list.end(),greater
<
MyClass
>
());
cout
<<
"
after sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list[i].id
<<
endl;
//
定義比較函數
cout
<<
"
定義比較函數
"
<<
endl;
vector
<
MyClass
>
list2;
for
(
int
i
=
0
; i
<
10
; i
++
)
list2.push_back( MyClass( rand()
%
101
) );
cout
<<
"
before sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list2[i].id
<<
endl;
sort(list2.begin(),list2.end(),compare);
cout
<<
"
after sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list2[i].id
<<
endl;
//
使得類本身就是可以比較的
cout
<<
"
使得類本身就是可以比較的
"
<<
endl;
vector
<
MyClass
>
list3;
for
(
int
i
=
0
; i
<
10
; i
++
)
list3.push_back( MyClass( rand()
%
101
) );
cout
<<
"
before sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list3[i].id
<<
endl;
sort(list3.begin(),list3.end());
cout
<<
"
after sort
"
<<
endl;
for
(
int
i
=
0
; i
<
10
; i
++
)
cout
<<
list3[i].id
<<
endl;
return
0
;
}
posted on 2010-12-08 20:46
longshen
閱讀(1244)
評論(0)
編輯
收藏
引用
所屬分類:
VC++
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
【轉】STL中sort的用法舉例
解決 error LNK2019: 無法解析的外部符號 問題
第3章 內核對象 -- Windows核心編程
Unicode與多字節的基本運用 -- Windows編程
SDK之文件API
內存不夠處理 -- new_handler
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright ©2025 longshen Powered by:
博客園
模板提供:
滬江博客
无码人妻久久一区二区三区免费
|
麻豆国内精品久久久久久
|
人妻少妇精品久久
|
一级a性色生活片久久无少妇一级婬片免费放
|
国产精品一久久香蕉产线看
|
久久精品男人影院
|
久久综合一区二区无码
|
2020久久精品亚洲热综合一本
|
色综合久久久久无码专区
|
久久精品国产只有精品2020
|
一本色道久久88综合日韩精品
|
国产精品无码久久综合
|
亚洲欧美日韩久久精品
|
国产精品久久影院
|
久久亚洲国产精品成人AV秋霞
|
国产高潮国产高潮久久久91
|
久久久久久无码Av成人影院
|
国内精品久久久久久中文字幕
|
精品熟女少妇AV免费久久
|
久久99热国产这有精品
|
亚洲AV无码久久精品色欲
|
久久久久人妻一区精品
|
99久久久国产精品免费无卡顿
|
久久受www免费人成_看片中文
|
国产99久久九九精品无码
|
久久久久免费看成人影片
|
麻豆av久久av盛宴av
|
久久久久亚洲AV无码去区首
|
婷婷综合久久狠狠色99h
|
久久精品无码午夜福利理论片
|
国产成人精品久久
|
伊人久久综合无码成人网
|
欧美精品国产综合久久
|
性做久久久久久免费观看
|
色婷婷久久久SWAG精品
|
久久亚洲高清综合
|
亚洲国产成人久久精品99
|
天天做夜夜做久久做狠狠
|
一本色综合久久
|
一本色道久久综合亚洲精品
|
久久精品人人做人人爽电影
|