STL常规使用

#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;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值