0%

STL 容器记录

主要涉及到c++中STL容器的一些用法:vector,set,stack,queue,map,deque及Algorithm,不记得时可以随时查漏补缺。

1
2
转载自@author 鲁伟林
gitHub地址:https://github.com/thinkingfioa/Notes/blob/master/TechFiles/STL_Usage.md

vector

push_back()函数

代码:

1
2
vector<int> myVector;
myVector.push_back(100);

pop_back()函数

代码:

1
从后往前pop

1
2
3
4
5
6
7
vector<int> myVector;
myVector.push_back(100);
myVector.push_back(200);
myVector.push_back(300);
//执行前: 100, 200, 300
myVector.pop_back();
//执行后: 100, 200

insert()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>

int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
//执行前: 100, 100, 100
it = myvector.begin();
it = myvector.insert ( it , 200 );
//执行后: 200, 100, 100, 100

//执行前: 200, 100, 100, 100
myvector.insert (it,2,300);
//执行后: 300, 300, 200, 100, 100, 100

// "it" no longer valid, get a new one:
it = myvector.begin();

//执行前: 300, 300, 200, 100, 100, 100
std::vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
//执行后: 300, 300, 400, 400, 200, 100, 100, 100

int myarray [] = { 501,502,503 };
//执行前: 300, 300, 400, 400, 200, 100, 100, 100
myvector.insert (myvector.begin(), myarray, myarray+3);
//执行后: 501, 502, 503, 300, 300, 400, 400, 200, 100, 100, 100

return 0;
}

erase()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
vector<int> myVector;
for(int i =0; i<10;i++) {
myVector.push_back(i);
}

//执行前: 1, 2, 3, 4, 5, 6, 7, 8, 9
myVector.erase(myVector.begin() +5); // 移除下标为5的数,也就是myVector[5]=6
//执行后: 1, 2, 3, 4, 5, 7, 8, 9

//执行前: 1, 2, 3, 4, 5, 7, 8, 9
myVector.erase(myVector.begin(), myVector.begin() + 3);
//执行后: 4, 5, 7, 8, 9

swap()函数

代码:

1
2
3
4
5
6
7
std::vector<int> foo (3,100);   // three ints with a value of 100
std::vector<int> bar (5,200); // five ints with a value of 200
//执行前: foo: [100, 100, 100]
// bar: [200, 200, 200, 200, 200]
foo.swap(bar);
//执行后: foo: [200, 200, 200, 200, 200]
// bar: [100, 100, 100]

遍历

迭代器遍历:

1
2
3
for (it=myvector.begin(); it<myvector.end(); it++){
std::cout << ' ' << *it;
}

set

1
C++中的Set是一个有序-去重集合

insert()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
std::set<int> myset;
std::set<int>::iterator it;
std::pair<std::set<int>::iterator,bool> ret;

// set some initial values:
for (int i=1; i<=5; ++i) myset.insert(i*10);

//执行前: 10, 20, 30, 40, 50
ret = myset.insert(20); // no new element inserted
//执行后: 10, 20, 30, 40, 50

if (ret.second==false) it=ret.first; // "it" now points to element 20

//执行前: 10, 20, 30, 40, 50
myset.insert (it,25); // max efficiency inserting
myset.insert (it,24); // max efficiency inserting
myset.insert (it,26); // no max efficiency inserting
//执行后: 10, 20, 24, 25, 26, 30, 40, 50

//执行前: 10, 20, 24, 25, 26, 30, 40, 50
int myints[]= {5,10,15}; // 10 already in set, not inserted
myset.insert (myints,myints+3);
//执行后: 5, 10, 15, 20, 24, 25, 26, 30, 40, 50

erase()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::set<int> myset;
std::set<int>::iterator it;

// insert some values:
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

it = myset.begin();
++it; // "it" points now to 20
// 执行前: 10, 20, 30, 40, 50, 60, 70, 80, 90; //(*it) = 20
myset.erase (it);
//执行后: 10, 30, 40, 50, 60, 70, 80, 90;

//执行前: 10, 30, 40, 50, 60, 70, 80, 90;
myset.erase (40);
//执行后: 10, 30, 50, 60, 70, 80, 90;

it = myset.find (60);
//执行前: 10, 30, 50, 60, 70, 80, 90;
myset.erase (it, myset.end());
//执行后: 10, 30, 50;

swap()函数

代码:

1
2
3
4
5
6
7
8
int myints[]={12,75,10,32,20,25};
std::set<int> first (myints,myints+3); // 10,12,75
std::set<int> second (myints+3,myints+6); // 20,25,32
//执行前: first [10, 12, 75]
// second [20, 25, 32]
first.swap(second);
//执行后: first [20, 25, 32]
// second [10, 12, 75]

find()函数

代码:

1
2
3
4
5
6
7
8
9
10
std::set<int> myset;
std::set<int>::iterator it;

// set some initial values:
for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50

// 执行前: 10, 20, 30, 40, 50
it=myset.find(20);
myset.erase (it);
// 执行后: 10, 30, 40, 50

count()函数

代码:

1
由于set集合不重复,所以count(val)返回的结果只有0或者1.

1
2
3
4
5
6
7
8
9
std::set<int> myset;

// set some initial values:
for (int i=1; i<5; ++i) myset.insert(i*3); // set: 3 6 9 12

//返回结果是: 1
myset.count(3);
//返回结果是: 0
myset.count(5);

遍历

1
2
3
4
5
6
int myints[] = {75,23,65,42,13};
std::set<int> myset (myints,myints+5); //13, 23, 42, 65, 75

for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it){
std::cout << ' ' << *it;
}

stack

top()函数

代码:

1
2
3
4
5
std::stack<int> mystack;
mystack.push(10);
mystack.push(20);

mystack.top() -= 5; //mystack.top() 的值为20

push()函数

代码:

1
2
3
std::stack<int> mystack;

for (int i=0; i<5; ++i) mystack.push(i);

pop()函数

1
弹出栈的最上面

swap()函数

代码:

1
2
3
4
5
6
7
8
std::stack<int> foo,bar;
foo.push (10); foo.push(20); foo.push(30);
bar.push (111); bar.push(222);
// 执行前: foo [10, 20, 30]
// bar [111, 222]
foo.swap(bar);
// 执行后: foo [111, 222]
// bar [10, 20, 30]

遍历

1
2
3
4
5
6
7
std::cout << "Popping out elements...";
while (!mystack.empty())
{
std::cout << ' ' << mystack.top();
mystack.pop();
}
std::cout << '\n';

map

1
c++ 中的map是默认按照key的排序。所以每次插入都会做调整。

insert()函数

代码:

1
2
3
4
std::map<char,int> mymap;

// first insert function version (single parameter):
mymap.insert ( std::pair<char,int>('a',100) );

erase()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::map<char,int> mymap;
std::map<char,int>::iterator it;

// insert some values:
mymap['a']=10;
mymap['b']=20;
mymap['c']=30;
mymap['d']=40;
mymap['e']=50;
mymap['f']=60;

it=mymap.find('b');
//执行前: a->10, b->20, c->30, d->40, e->50, f->60
mymap.erase (it); // erasing by iterator
//执行后: a->10, c->30, d->40, e->50, f->60

//执行前: a->10, c->30, d->40, e->50, f->60
mymap.erase ('c'); // erasing by key
//执行后: a->10, d->40, e->50, f->60

it=mymap.find ('e');
//执行前: a->10, d->40, e->50, f->60
mymap.erase ( it, mymap.end() ); // erasing by range
//执行后: a->10, d->40

swap()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
std::map<char,int> foo,bar;

foo['x']=100;
foo['y']=200;

bar['a']=11;
bar['b']=22;
bar['c']=33;
// 执行前: foo [x->100, y->200]
// bar [a->11, b->22, c->33]
foo.swap(bar);
// 执行前: foo [a->11, b->22, c->33]
// bar [x->100, y->200]

遍历

代码:

1
2
3
for (std::map<char,int>::iterator it=foo.begin(); it!=foo.end(); ++it) {
std::cout << it->first << " => " << it->second << '\n';
}

queue

push()函数

代码:

1
2
std::queue<int> myqueue;
myqueue.push (myint);

pop()函数

1
弹出最后一个数。

swap()函数

代码:

1
2
3
4
5
6
7
8
std::queue<int> foo,bar;
foo.push (10); foo.push(20); foo.push(30);
bar.push (111); bar.push(222);
// 执行前: foo[10, 20, 30]
// bar[111, 222]
foo.swap(bar);
// 执行后: foo[111, 222]
// bar[10, 20, 30]

back()函数

代码:

1
队列的最后一位

1
2
3
4
5
myqueue.push(12);
myqueue.push(75); // this is now the back
//执行前: 12, 75
myqueue.back() -= myqueue.front();
//执行后: 12, 63

front()函数

代码:

1
队列的队首

1
2
3
4
5
myqueue.push(77);
myqueue.push(16);
//执行前: 77, 16
myqueue.front() -= myqueue.back(); // 77-16=61
//执行后: 61, 16

遍历

代码:

1
2
3
4
5
6
std::cout << "myqueue contains: ";
while (!myqueue.empty())
{
std::cout << ' ' << myqueue.front();
myqueue.pop();
}

deque

1
双向队列

front()函数

代码:

1
返回队首

1
2
3
4
5
6
7
std::deque<int> mydeque;

mydeque.push_front(77);
mydeque.push_back(20);
//执行前: 77, 20
mydeque.front() -= mydeque.back();
//执行后: 57, 20

back()函数

代码:

1
双向队列的尾部

1
2
3
4
5
6
std::deque<int> mydeque;

mydeque.push_back(10);

while (mydeque.back() != 0)
mydeque.push_back ( mydeque.back() -1 );

push_back()函数

代码:

1
2
3
4
5
6
7
8
std::deque<int> mydeque;
//执行前: []
mydeque.push_back(1);
//执行后: [1]

//执行前: [1]
mydeque.push_back(2);
//执行后: [1,2]

push_front()函数

代码:

1
2
3
4
5
6
7
8
std::deque<int> mydeque (2,100);     // two ints with a value of 100
//执行前: 100, 100
mydeque.push_front (200);
//执行后: 200, 100, 100

//执行前: 200, 100, 100
mydeque.push_front (300);
//执行前: 300, 200, 100, 100

pop_back()函数

代码:

1
2
3
4
5
6
7
8
9
//执行前: 
mydeque.push_back (10);
mydeque.push_back (20);
mydeque.push_back (30);
//执行后: 10, 20, 30

//执行前: 10, 20, 30
mydeque.pop_back();
//执行后: 10, 20

pop_front()函数

代码:

1
2
3
4
5
6
7
8
9
//执行前: 
mydeque.push_back (100);
mydeque.push_back (200);
mydeque.push_back (300);
//执行后: 100, 200, 300

//执行前: 100, 200, 300
mydeque.pop_front();
//执行后: 200, 300

insert()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
std::deque<int> mydeque;

// set some initial values:
for (int i=1; i<6; i++) mydeque.push_back(i); // 1 2 3 4 5

std::deque<int>::iterator it = mydeque.begin();
++it;

//执行前: 1, 2, 3, 4, 5
it = mydeque.insert (it,10);
//执行后: 1, 10, 2, 3, 4, 5
// "it" now points to the newly inserted 10

//执行前: 1, 10, 2, 3, 4, 5
mydeque.insert (it,2,20); // 1 20 20 10 2 3 4 5
//执行后: 1, 20, 20, 10, 2, 3, 4, 5
// "it" no longer valid!

it = mydeque.begin()+2;

//执行前: 1, 20, 20, 10, 2, 3, 4, 5
std::vector<int> myvector (2,30);
mydeque.insert (it,myvector.begin(),myvector.end());
//执行后: 1, 20, 30, 30, 20, 10, 2, 3, 4, 5

erase()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) mydeque.push_back(i);

// erase the 6th element
//执行前: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
mydeque.erase (mydeque.begin()+5);
//执行后: 1, 2, 3, 4, 5, 7, 8, 9, 10

// erase the first 3 elements:
//执行前: 1, 2, 3, 4, 5, 7, 8, 9, 10
mydeque.erase (mydeque.begin(),mydeque.begin()+3);
//执行后: 4, 5, 7, 8, 9, 10

swap()函数

代码:

1
2
3
4
5
6
7
8
9
unsigned int i;
std::deque<int> foo (3,100); // three ints with a value of 100
std::deque<int> bar (5,200); // five ints with a value of 200

//执行前: foo [100, 100, 100]
// bar [200, 200, 200, 200, 200]
foo.swap(bar);
//执行后: foo [200, 200, 200, 200, 200]
// bar [100, 100, 100]

遍历

代码:

1
2
3
for (std::deque<int>::iterator it = mydeque.begin(); it!=mydeque.end(); ++it) {
std::cout << ' ' << *it;
}

1
2
3
4
while (!mydeque.empty()) {
sum+=mydeque.back();
mydeque.pop_back();
}

algorithm

find()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//数组中查询
int myints[] = { 10, 20, 30, 40 };
int * p;

p = std::find (myints, myints+4, 30);
if (p != myints+4)
//输出: 30
else
std::cout << "Element not found in myints\n";

// vector中查询
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;

it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
//输出: 30
else
std::cout << "Element not found in myvector\n";

copy()函数

代码:

1
2
3
4
5
int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);
//执行前: myvector:
std::copy ( myints, myints+7, myvector.begin() );
//执行前: myvector: 10, 20, 30, 40, 50, 60, 70

swap()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
int x=10, y=20;                              // x:10 y:20
//执行前: x:10, y:20
std::swap(x,y); // x:20 y:10
//执行后: x:20, y:10


std::vector<int> foo (4,x), bar (6,y); // foo:4x20 bar:6x10

//执行前: foo[20, 20, 20, 20]
// bar[10, 10, 10, 10]
std::swap(foo,bar); // foo:6x10 bar:4x20
//执行前: foo[20, 20, 20, 20]
// bar[10, 10, 10, 10]

count()函数

代码:

1
2
3
4
5
6
7
8
9
int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
//记录数组myints中,10的个数
int mycount = std::count (myints, myints+8, 10);
//输出结果 mycount = 3

std::vector<int> myvector (myints, myints+8);
//记录数组myvector中,20的个数
mycount = std::count (myvector.begin(), myvector.end(), 20);
//记录数组myvector中,20的个数

replace()函数

代码:

1
2
3
4
5
6
7
8
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
//执行前: myvector: []
std::vector<int> myvector (myints, myints+8);
//执行后: myvector: 10, 20, 30, 30, 20, 10, 10, 20

//执行前: myvector: 10, 20, 30, 30, 20, 10, 10, 20
std::replace (myvector.begin(), myvector.end(), 20, 99);
//执行前: myvector: 10, 99, 30, 30, 99, 10, 10, 99

fill()函数

代码:

1
2
3
4
5
6
7
8
9
10
//执行前 myvector: []
std::vector<int> myvector (8);
//执行后 myvector: 0,0,0,0,0,0,0,0

//执行前 myvector: 0,0,0,0,0,0,0,0
std::fill (myvector.begin(),myvector.begin()+4,5);
//执行后 myvector: 5,5,5,5,0,0,0,0
//执行后 myvector: 5,5,5,5,0,0,0,0
std::fill (myvector.begin()+3,myvector.end()-2,8);
//执行后 myvector: 5,5,5,8,8,8,0,0

reverse()函数

代码:

1
2
3
4
5
6
std::vector<int> myvector;
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

//执行前: 1,2,3,4,5,6,7,8,9
std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1
//执行后: 9,8,7,6,5,4,3,2,1

sort()函数

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//升序
bool myfunction (int i,int j) { return (i<j); }

//升序
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
int myints[] = {32,71,12,45,26,80,53,33};
//执行前: myvector: []
std::vector<int> myvector (myints, myints+8);
//执行后: myvector: 32,71,12,45,26,80,53,33

//缺省的排序是升序(<)
//执行前: 32,71,12,45,26,80,53,33
std::sort (myvector.begin(), myvector.begin()+4);
//执行后: 12,32,45,71,26,80,53,33

//执行前: 12,32,45,71,26,80,53,33 using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction);
//执行后: 12,32,45,71,26,33,53,80

// using object as comp
//执行后: 12,32,45,71,26,33,53,80
std::sort (myvector.begin(), myvector.end(), myobject);
//执行后: 12,26,32,33,45,53,71,80
}

merge函数

代码:

1
将两个有序的组合sort1[], sort2[]。归并成一个新的有序组合。

1
2
3
4
5
6
7
8
9
10
11
12
13
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10);

std::sort (first,first+5);
std::sort (second,second+5);
//执行前: first: 5,10,15,20,25
// second: 10,20,30,40,50
// v:[]
std::merge (first,first+5,second,second+5,v.begin());
//执行后: first: 5,10,15,20,25
// second: 10,20,30,40,50
// v:5,10,10,15,20,20,25,30,40,50

min()函数

代码:

1
2
3
4
5
6
7
8
//输出: 1
std::cout << "min(1,2)==" << std::min(1,2) << '\n'
//输出: 1
std::cout << "min(2,1)==" << std::min(2,1) << '\n';
//输出: 'a'
std::cout << "min('a','z')==" << std::min('a','z') << '\n';
//输出: 2.72
std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '\n';

max()函数

代码:

1
2
3
4
5
6
7
8
//输出: 2
std::cout << "max(1,2)==" << std::max(1,2) << '\n'
//输出: 2
std::cout << "max(2,1)==" << std::max(2,1) << '\n';
//输出: 'z'
std::cout << "max('a','z')==" << std::max('a','z') << '\n';
//输出: 3.14
std::cout << "max(3.14,2.72)==" << std::max(3.14,2.72) << '\n';