#include <stdio.h>
#include <algorithm>
#include<stdlib.h>//随机数函数的头文件
#include <string>
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
struct node
{
string s;
int val;
};
bool num_greater(int n) {
return (n>10000);
}
bool func(int i, int j)
{ return (i < j); }
bool comp_as_int(node node1,node node2)
{
return (node1.val < node2.val);
}
int main()
{
vector<int > v;
for(int j=0;j<100;j++)
{
int a=rand();
v.push_back(a);
}
vector<int>::const_iterator i;//迭代器 可以粗略理解为指针
for(i=v.begin();i!=v.end();i++)
{
printf("%d\n",*i);
}
//sort(v.begin(),v.end());
//partition(v.begin(),v.end(),num_greater);
partial_sort(v.begin()+3,v.begin()+10,v.end(),func);
//nth_element(v.begin(),v.begin()+10,v.end());//使得 将第k_th元素放到它该放的位置上,左边元素都小于等于它,右边元素都大于等于它.
printf("\n\n\n\n");
for(i=v.begin();i!=v.end();i++)
{
printf("%d\n",*i);
}
/* vector<node> v1;
node node1;
node1.s='a';
node1.val=20;
v1.push_back(node1);
node1.s='b';
node1.val=5;
v1.push_back(node1);
node1.s='d';
node1.val=13;
v1.push_back(node1);
node1.s='c';
node1.val=13;
v1.push_back(node1);
//vector<>::const_iterator i;//迭代器 可以粗略理解为指针
stable_sort(v1.begin(), v1.end(), comp_as_int);
for(int i=0;i<v1.size();i++)
cout<<v1[i].s<<" "<<v1[i].val<<endl;*/
/*set<int > s;
for(int j=0;j<1000;j++)
{
int a=rand();
s.insert(a);
}
set<int>::const_iterator i1;//常量迭代器 可以粗略理解为指针
for(i1=s.begin();i1!=s.end();i1++)
{
printf("%d\n",*i1);
}*/
/*map<int, string> mapStudent;
for(int i=0;i<1000;i++)
{
int a=rand();
mapStudent.insert(map<int, string>::value_type (a, "student_one"));
}
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
*/
return 0;
}
STL常规使用
最新推荐文章于 2023-04-05 20:24:41 发布