【vc】【STL源码】vector,deque与sort的用法比较及入门
2007-06-10 15:27
375 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <stdio.h>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// sort的定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <algorithm>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// vector的定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <vector>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// deque的定义
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
#include <deque>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// 同样,引入std命名空间
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using namespace std;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// 用数组保存
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int friends[1000];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// 用vector保存
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
vector<int> vtFriends;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
// 用deque保存
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
deque<int> dqFriends;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
int main()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int T;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &T);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int iCase;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (iCase = 1; iCase <= T; iCase++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 每次要将vector和deque请空
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vtFriends.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dqFriends.clear();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("Scenario #%d: ", iCase);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int n, f, tn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d%d", &n, &f);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 由于后面要找三次,所以先把n的值保存下来
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
tn = n;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (i = 0; i < f; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 保存到数组
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
scanf("%d", &friends[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 保存到vector
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
vtFriends.push_back(friends[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 保存到deque
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
dqFriends.push_back(friends[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 排序,如果不给第三个参数,默认是按升序排
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// sort实现的是快速排序,它是不稳定排序,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 需要稳定排序时,用stable_sort,用法与sort完全一样
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 对数组进行排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sort(friends, friends + f);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 对vector进行排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sort(vtFriends.begin(), vtFriends.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 对deque进行排序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
sort(dqFriends.begin(), dqFriends.end());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在数组里查找
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (i = f - 1; i >= 0; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n -= friends[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (n <= 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在vector里查找
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 先恢复n的值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n = tn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (i = f - 1; i >= 0; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n -= vtFriends.at(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (n <= 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 在deque里查找
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 先恢复n的值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n = tn;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (i = f - 1; i >= 0; i--)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// deque的at方法效率比vector低,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 因为deque的元素在内存上不完全是连续的
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n -= dqFriends.at(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (n <= 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (i < 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
puts("impossible");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
printf("%d ", f - i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
puts("");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
使用的我们学院一个牛人写的一篇东西哈,这里介绍vector,deque和sort的用法,这些都是STL的内容,给你一个网站吧,http://www.stlchina.org/;
不过一般好像都不提倡一开始就在程序中使用STL,那些算法都自己写写最好,然后等学会了再用也会理解得更深入的!
相关文章推荐
- STL中常用的vector,map,set,sort 用法
- STL中的常用的vector,map,set,Sort用法
- STL容器类vector,list,deque的比较
- STL中的常用的vector,map,set,Sort用法 ——20110419
- STL vector+sort排序和multiset/multimap排序比较
- c++的STL模板库中3种容器类:vector,list,deque的比较
- STL中的常用的vector,map,set,Sort用法
- STL容器用法速查表:list,vector,stack,queue,deque,priority_queue,set,map
- STL 入门--vector list deque 区别
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- STL中的常用的vector,map,set,Sort用法
- STL中的常用的vector,map,set,Sort用法
- c++ list, vector,deque, map,set 区别与用法比较
- STL中的常用的vector,map,set,Sort用法(转)
- STL中的常用的vector,map,set,Sort用法
- STL中的常用的vector,map,set,Sort用法
- STL中的常用的vector,map,set,Sort用法
- STL中的常用的vector,map,set,Sort用法收藏
- STL容器 vector,list,deque 性能比较
- STL中的常用的vector,map,set,Sort用法