码迷,mamicode.com
首页 > 编程语言 > 详细

STL容器及常用算法

时间:2016-08-02 17:07:03      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

1. vector

vector<int>iv(2,9);

iv.size();

iv.push_back(1);

iv.pop_back();

vector<int>::iteratorivite=find(iv.begin(),iv.end(),1);  //STLfind()函数

iv.erase(ivite);

iv.insert(ivite,3,7);

iv.clear();

2. list

list<int>ilist;

ilist.size();

ilist.push_back(1);

list<int>::iterator ite;

ite=find(ilist.begin(),ilist.end(),3);

ilist.insert(ite,99);

ilist.erase(ite);

list的元素操作:

push_front()、push_back()、erase()、pop_front()、pop_back()、clear()、remove()、unique()、splice()、merge()、reverse()、sort()

ilist.splice(ite,ilist2);

ilist.reverse();

ilist.sort();                       //自身的sort()函数

3. deque

deque<int,alloc,8>ideq(20,9);

ideq.size();

ideq.push_back(3);

ideq.push_front(3);

deque<int,alloc,32>::iteratoritr;

itr=find(ideq.begin(),ideq.end(),99);

clear、erase、insert

4. stack

empty()

size()

top()

pop()

push()

5. queue

empty()

size()

front()

back()

push()

pop()

6. heap

#include<vector>

int ia[9]={0,1,2,3,4,5,6,7,8,9};

vector<int>ivec(ia,ia+9);

make_heap(ivec.begin(),ivec.end());                //STL库函数

push_heap(ivec.begin(),ivec.end());

pop_heap(ivec.begin(),ivec.end());

sort_heap(ivec.begin(),ivec.end());

7.priority_queue

#include<queue>

priority_queue<int>ipq(ia,ia+9);

size()

top()

pop()

8.set

#include<set>

set<int>iset(ia,ia+5);

iset.size();

iset.count(3);

iset.insert(3);

iset.erase(1);

set<int>::iteratorite=iset.begin();

ite1=find(iset.begin(),iset.end(),1);              //STL函数

ite1=iset.find(3);                            //自身的find函数

企图通过迭代器来改变set元素,是不被允许的。

9.map

#include<map>

Map<string,int>simap;

Simap[string(“jjhou”)]=1;

Pair<string,int>value(string(“davaid”),5);

Simap.insert(value);

Map<string,int>::iteratorsimap_iter=simap.begin();

Simap_iter->first;

Simap_iter->second;

Map<string,int>::iteratorite1;

Ite1=simap.find(string(“jerry”));            //自身的find()函数

 

10.算法

Copy()

int myints[]={10,20,30,40,50,60,70};

std::vector<int> myvector (7);

std::copy ( myints, myints+7,myvector.begin() );

 

 

count()

int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
int mycount = std::count (myints, myints+8, 10);
std::cout << "10 appears " << mycount << " times.\n";

 

search()

std::vector<int>haystack;

// set somevalues:        haystack: 10 20 30 40 5060 70 80 90

for (inti=1; i<10; i++) haystack.push_back(i*10);

// usingdefault comparison:

intneedle1[] = {40,50,60,70};

std::vector<int>::iteratorit;

it =std::search(haystack.begin(), haystack.end(), needle1, needle1+4);

if(it!=haystack.end())

std::cout<< "needle1 found at position " << (it-haystack.begin())<< ‘\n‘;

else std::cout << "needle1 notfound\n";

 

find()

intmyints[] = { 10, 20, 30, 40 };

int * p;

p =std::find (myints, myints+4, 30);

 

sort()

bool myfunction (int i,int j) { return (i<j); }

intmyints[] = {32,71,12,45,26,80,53,33};

std::vector<int>myvector (myints, myints+8);

// usingdefault comparison (operator <):

std::sort(myvector.begin(), myvector.begin()+4);          

// usingfunction as comp

std::sort(myvector.begin()+4, myvector.end(), myfunction);

// usingobject as comp

std::sort(myvector.begin(), myvector.end(), myobject);   

 

STL容器及常用算法

标签:

原文地址:http://blog.csdn.net/yutianxin123/article/details/52095801

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!