C++ STL 排序 查找 集合操作
来源:互联网 发布:战地1优化设置 编辑:程序博客网 时间:2024/06/06 02:58
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>
#include <cstdlib>
#include <functional>
#include <numeric>
using namespace std;
template<typename T>
void printVector(vector<T> v)
{
vector<T>::iterator it;
for(it=v.begin();it!=v.end();++it) //使用迭代器输出
cout<<*it<<" ";
cout<<endl;
}
//排序与查找
int main()
{
int A[]={1,2,3};
const int N=sizeof(A)/sizeof(int);
cout<<"The sum is: "<<accumulate(A,A+N,1)<<endl;
cout<<"The sum is: "<<accumulate(A,A+N,1,multiplies<int>())<<endl;
///sort()排序字符串
vector<string> fruits;
fruits.push_back("apple");
fruits.push_back("banana");
fruits.push_back("pear");
fruits.push_back("graphe");
fruits.push_back("cherry");
fruits.push_back("orange");
fruits.push_back("apple");
sort(fruits.begin(),fruits.end(),greater<string>());
copy(fruits.begin(),fruits.end(),ostream_iterator<string>(cout," "));
cout<<endl;
stable_sort(fruits.begin(),fruits.end(),greater<string>());
copy(fruits.begin(),fruits.end(),ostream_iterator<string>(cout," "));
cout<<endl;
//////binary_search() 二分查找//////////////////////////////////////
int B[]={1,2,3,3,3,5,8};
const int M=sizeof(B)/sizeof(int);
for(int i=1;i<=10;i++)
{
cout<<"Searching for "<<i<<": "
<<(binary_search(B,B+M,i)? "present" : "not present")
<<endl;
}
////lower_bound(),upper_bound()////////////////////
int point[10] = {1,3,7,7,9};
int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置
printf("%d/n",tmp);
tmp = lower_bound(point, point + 5, 7) - point;////按从小到大,7最少能插入数组point的哪个位置
printf("%d/n",tmp);
////////merge()/////////////////////////
int A1[]={1,3,5,7};
int A2[]={3,4,6,8};
const int N1=sizeof(A1)/sizeof(int);
const int N2=sizeof(A2)/sizeof(int);
merge(A1,A1+N1,A2,A2+N2,ostream_iterator<int>(cout," "));
cout<<endl;
copy(A1,A1+N1,ostream_iterator<int>(cout," "));
cout<<endl;
////inplace_merge()合并一个中前后两段分别有序的序列,即[first,middle)与[middle,last)分别有序,合并这两个子序列,并把结果保存在[first,last)中。
int A3[]={1,1,2,3,1,2,5,7,9};
inplace_merge(A3,A3+4,A3+9);
copy(A3,A3+9,ostream_iterator<int>(cout," "));
cout<<endl;
/////set_union()///////
int B1[]={1,3,5,7,9,11};
int B2[]={1,1,2,3,5,8,13};
char B3[]={'a','b','B','B','f','H'};
int B4[]={'A','B','b','C','D','F','F','h','h'};
const int L1=sizeof(B1)/sizeof(int);
const int L2=sizeof(B2)/sizeof(int);
const int L3=sizeof(B3)/sizeof(int);
const int L4=sizeof(B4)/sizeof(int);
set_union(B1,B1+L1,B2,B2+L2,ostream_iterator<int>(cout," "));
cout<<endl;
set_union(B3,B3+L3,B4,B4+L4,ostream_iterator<char>(cout," "));
cout<<endl;
return 0;
}
- C++ STL 排序 查找 集合操作
- c++STL排序及相关操作
- 初学Java,集合类的排序,查找,替换操作(三十五)
- 初学Java,集合类的排序,查找,替换操作
- java 集合的常用操作(排序、查找、打乱顺序)
- java 集合的常用操作(排序、查找、打乱顺序)
- 二分查找 C 、C++STL
- c++STL sort()排序
- c++STL Set集合容器
- java集合操作查找表
- Java进阶(三十九)Java集合类的排序,查找,替换操作
- python---实战日志文件中查找特定字段,文件操作读写、排序、替换、列表、集合
- C排序集合
- STL标准容器排序操作
- c++STL之排序函数
- c++ stl集合操作函数若干
- 数组简单操作排序、查找
- 集合操作集合排序,内容筛选
- 数学分析告诉我们的……(转)
- u-boot的Makefile分析
- Valgrind 使用简单说明
- 如何看系统的指标
- OpenCV2.2+VC2005 error LNK2019错误解决办法
- C++ STL 排序 查找 集合操作
- linux内存管理之非连续物理地址分配(vmalloc)
- UML学习笔记之顺序图
- OpenCV2.2+VC2005 fatal error C1083: Cannot open include file:'opencv2/core/core_c.h': No such file or directory
- 我学会的第一句英语
- c语言函数
- 函数名: bar画一个二维条形图
- assert() 函数用法
- 业内的一些个事