您的位置:首页 > 其它

STL 容器使用方法

2013-09-11 22:21 99 查看
STL容器使用方法

1. vector的使用

vector的主要方法有:constructor(vector()),push_back(),pop_back(),erase(),clear(),insert(),begin(),end(),size(),empty(),front(),back(),resize(),[]

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
int i = 0;
vector<int> iv(2, 9);      //构造函数,两个大小,初始化为10
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

iv.push_back(1);            // 插入元素
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

iv.push_back(2);
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

iv.push_back(3);
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

iv.push_back(4);
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;
for( int i = 0; i < iv.size(); i++)     //遍历元素,输出
{
cout << iv[i] << ' ';
}
cout << endl;

iv.push_back(5);
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;
for( int i = 0; i < iv.size(); i++)
{
cout << iv[i] << ' ';
}
cout << endl;

iv.pop_back();               // 弹出元素
iv.pop_back();
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

iv.pop_back();
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

vector<int>::iterator ivIte = find( iv.begin(), iv.end(), 1);   // 定义迭代器,并查找
if( ivIte != iv.end())
{
iv.erase( ivIte);
}
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;
for( int i = 0; i < iv.size(); i++)
{
cout << iv[i] << ' ';
}
cout << endl;

ivIte = find(iv.begin(), iv.end(), 2);
if( ivIte != iv.end())
{
iv.insert( ivIte, 3, 7);
}
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;
for( int i = 0; i < iv.size(); i++)
{
cout << iv[i] << ' ';
}
cout << endl;

iv.clear();
cout << "size = " << iv.size() << endl;
cout << "capacity = " << iv.capacity() <<endl;

return 0;
}


运行结果:

size = 2

capacity = 2

size = 3

capacity = 4

size = 4

capacity = 4

size = 5

capacity = 8

size = 6

capacity = 8

9 9 1 2 3 4

size = 7

capacity = 8

9 9 1 2 3 4 5

size = 5

capacity = 8

size = 4

capacity = 8

size = 3

capacity = 8

9 9 2

size = 6

capacity = 8

9 9 7 7 7 2

size = 0

capacity = 8

2. list的使用

#include <iostream>
#include <list>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
int i = 0;
list<int> ilist;
cout << "size = " << ilist.size() << endl;

ilist.push_back(0);
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(3);
ilist.push_back(4);
cout << "size = " << ilist.size() << endl;

list<int>::iterator ite;
for( ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << *ite << ' ';
}
cout << endl;

ite = find(ilist.begin(), ilist.end(), 3);
if( ite != ilist.end())
{
ilist.insert(ite, 99);
}
cout << "size = " << ilist.size() << endl;
cout << *ite << endl;

for( ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << *ite << ' ';
}
cout << endl;

ite = find(ilist.begin(), ilist.end(), 1);
if(ite != ilist.end())
{
cout << *( ilist.erase(ite)) << endl;
}

for(ite = ilist.begin(); ite != ilist.end(); ++ite)
{
cout << *ite << ' ';
}
cout << endl;

return 0;
}


运行结果:

size = 0

size = 5

0 1 2 3 4

size = 6

3

0 1 2 99 3 4

2

0 2 99 3 4

3. deque的使用

#include <iostream>
#include <deque>
#include <algorithm>
#include <memory>
using namespace std;

int main()
{
deque<int, allocator<int> > ideq(20, 9);
cout << "size = " << ideq.size() << endl;

for( int i = 0; i < ideq.size(); i++)
{
ideq[i] = i;
}
for( int i = 0; i < ideq.size(); i++)
{
cout << ideq[i] << ' ';
}
cout << endl;

for( int i = 0; i < 3; i++)
{
ideq.push_back(i);
}
for( int i = 0; i < ideq.size(); i++)
{
cout << ideq[i] << ' ';
}
cout << endl;
cout << "size = " << ideq.size() << endl;

ideq.push_back(3);
for( int i = 0; i < ideq.size(); i++)
{
cout << ideq[i] << '';
}
cout << endl;
cout << "size = " << ideq.size() << endl;

ideq.push_front(99);
for( int i = 0; i < ideq.size(); i++)
{
cout << ideq[i] << ' ';
}
cout << endl;
cout << "size = " << ideq.size() << endl;

ideq.push_front(98);
ideq.push_front(97);
for( int i = 0; i < ideq.size(); i++)
{
cout << ideq[i] << ' ';
}
cout << endl;
cout << "size = " << ideq.size() << endl;

deque<int, allocator<int> >::iterator itr;
itr = find(ideq.begin(), ideq.end(), 99);
cout << *itr << endl;
// cout << *(itr.) <<endl;

return 0;
}


运行结果:

size = 20

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2

size = 23

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3

size = 24

99 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3

size = 25

97 98 99 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3

size = 27

99

4. stack的使用

#include <iostream>
#include <algorithm>
#include <stack>
#include <list>

using namespace std;

int main()
{
stack<int, list<int> > myStack;
myStack.push(1);
myStack.push(3);
myStack.push(5);
myStack.push(7);
cout << myStack.size() << endl;
cout << myStack.top() << endl;

myStack.pop();   cout <<myStack.top() << endl;
myStack.pop();   cout <<myStack.top() << endl;
myStack.pop();   cout <<myStack.top() << endl;
cout << myStack.size() << endl;

return 0;
}


结果:

4

7

5

3

1

1

5. queue的使用

#include <iostream>
#include <queue>
#include <list>

using namespace std;

int main()
{
queue<int, list<int> > iqueue;              //注意 两个 > 号不能连着出现,否则出错
iqueue.push(1);
iqueue.push(3);
iqueue.push(5);
iqueue.push(7);

cout << iqueue.size() << endl;
cout << iqueue.front() << endl;

iqueue.pop(); cout << iqueue.front() << endl;
iqueue.pop(); cout << iqueue.front() << endl;
iqueue.pop(); cout << iqueue.front() << endl;
cout << iqueue.size() << endl;

return 0;
}


程序结果:

4

1

3

5

7

1

6. heap的使用

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
{
// test heap 以vector为容器
int ia[9] = {0, 1, 2, 3, 4, 8, 9, 3, 5};
vector<int> ivec(ia, ia+9);

make_heap(ivec.begin(), ivec.end());
for(int i = 0; i < ivec.size(); ++i)
{
cout << ivec[i] << ' ';
}
cout << endl;

ivec.push_back(7);
push_heap(ivec.begin(), ivec.end());
for(int i = 0; i < ivec.size(); ++i)
{
cout << ivec[i] << ' ';
}
cout << endl;

pop_heap(ivec.begin(), ivec.end());
cout << ivec.back() << endl;    // return,但是没有删除
ivec.pop_back();                //真正删除该元素
for(int i = 0; i < ivec.size(); ++i)
{
cout << ivec[i] << ' ';
}
cout << endl;

sort_heap(ivec.begin(), ivec.end());
for(int i = 0; i < ivec.size(); ++i)
{
cout << ivec[i] << ' ';
}
cout << endl;
}

{
//  test heap,以array为底层容器
int ia[9] = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(ia, ia+9);
// array无法改变大小,因此不可以对满载的array进行push_heap()操作

sort_heap(ia, ia+9);
for( int i = 0; i < 9; ++i)
{
cout << ia[i] << ' ';
}
cout << endl;
}

{
// test heap
int ia[6] = {4, 1, 7, 6, 2, 5};
make_heap(ia, ia + 6);
for( int i = 0; i < 6; ++i)
{
cout << ia[i] << ' ';
}
cout << endl;
}

return 0;
}


程序结果:

9 5 8 3 4 0 2 3 1

9 7 8 3 5 0 2 3 1 4

9

8 7 4 3 5 0 2 3 1

0 1 2 3 3 4 5 7 8

0 1 2 3 3 4 5 8 9

7 6 5 1 2 4

priority-heap的使用

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

int main()
{
//大顶堆
int ia[9] = {0, 1, 2, 3, 4, 8, 9, 3, 5};
priority_queue<int> ipq(ia, ia+9);
cout << "size = " << ipq.size() << endl;

for(int i = 0; i < ipq.size(); ++i)
{
cout << ipq.top() << ' ';
}
cout << endl;

while(!ipq.empty())
{
cout << ipq.top() << ' ';
ipq.pop();
}
cout << endl;

//小顶堆
priority_queue< int, vector<int>, greater<int> >ipq_min(ia, ia+9);
cout << "size = " << ipq_min.size() << endl;

for(int i = 0; i < ipq_min.size(); ++i)
{
cout << ipq_min.top() << ' ';
}
cout << endl;

while(!ipq_min.empty())
{
cout << ipq_min.top() << ' ';
ipq_min.pop();
}
cout << endl;
return 0;
}


程序结果:

size = 9

9 9 9 9 9 9 9 9 9

9 8 5 4 3 3 2 1 0

size = 9

0 0 0 0 0 0 0 0 0

0 1 2 3 3 4 5 8 9

7. set和map的使用

set使用:

#include <iostream>
#include <set>
#include <algorithm>

using namespace std;

int main()
{
int i;
int ai[5] = {0, 1, 2, 3, 4};
set<int> iset(ai, ai+5);

cout << "size = " << iset.size() << endl;
cout << "3 count = " << iset.count(3) <<endl;
iset.insert(3);
cout << "size = " << iset.size() << endl;
cout << "3 count = " << iset.count(3) <<endl;

iset.erase(1);
cout << "size = " << iset.size() << endl;
cout << "3 count = " << iset.count(3) <<endl;
cout << "1 count = " << iset.count(1) <<endl;

set<int>::iterator ite1 = iset.begin();
set<int>::iterator ite2 = iset.end();
for( ; ite1 != ite2; ++ite1)
{
cout << *ite1;
}
cout << endl;

ite1 = find(iset.begin(), iset.end(), 3);
if( ite1 != iset.end())
cout << "3 found" << endl;

ite1 = find(iset.begin(), iset.end(), 1);
if( ite1 != iset.end())
cout << "1 not found" << endl;

// *ite1 = 9;   编译错误,只读迭代器无法修改对应的元素

return 0;
}


程序结果:

size = 5

3 count = 1

size = 5

3 count = 1

size = 4

3 count = 1

1 count = 0

0234

3 found

Map的使用:

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
map<string, int> simap;    // 以string为键值,以int为实值
simap[string("jjhou")] = 1;
simap[string("jerry")] = 2;
simap[string("jason")] = 3;
simap[string("jimmy")] = 4;

pair<string, int> value(string("david"), 5);
simap.insert(value);

map<string,int>::iterator simap_iter = simap.begin();
for( ; simap_iter != simap.end(); ++simap_iter)
{
cout << simap_iter->first << ' ' <<simap_iter->second << endl;
}

int number = simap[string("jerry")];
cout << number << endl;

map<string, int>::iterator ite1;
// 面对关联式容器,应该使用其所提供的find 函数搜寻元素,会比使用STL算法find更有效率
ite1 = simap.find(string("mchen"));
if( ite1 == simap.end())
cout << "mchen not found" << endl;

ite1 = simap.find(string("jerry"));
if( ite1 != simap.end())
cout << "jerry found" << endl;

ite1->second = 9;
number = simap[string("jerry")];
cout << number <<endl;

return 0;
}


程序结果:

david 5

jason 3

jerry 2

jimmy 4

jjhou 1

2

mchen not found

jerry found

9

8. hashtable的使用:

#include <iostream>
#include <hash_set.h>
using namespace std;

int main()
{
hashtable<int, int, hash<int>, identity<int>,equal_to<int>, allocator<int> > iht(50, hash<int>(),equal_to<int>());
cout << iht.size() << endl;
cout << iht.bucket_count() << endl;
cout << iht.max_bucket_count() << endl;

iht.insert_unique(59);
iht.insert_unique(63);
iht.insert_unique(108);
iht.insert_unique(2);
iht.insert_unique(53);
iht.insert_unique(55);

cout << iht.size() << endl;

hashtable<int, int, hash<int>, identity<int>,equal_to<int>, allocator<int> >::iterator ite = iht.begin();

for( int i = 0; i < iht.size(); ++i, ++ite)
cout << *ite << ' ';
cout << endl;

for( int i = 0; i < iht.bucket_count(); ++i)
{
int n = iht.elems_in_bucket(i);
if( n != 0)
cout << "bucket[" << i << "] has "<< n << "elems." << endl;
}

for( int i = 0; i <= 47; i++)
{
iht.insert_equal(i);
}
cout << iht.size() << endl;
cout << iht.bucket_count() << endl;

for( int i = 0; i < iht.bucket_count(); i++)
{
int n = iht.elems_in_bucket(i);
if( n != 0)
cout << "bucket[" << i << "] has "<< n << "elems." << endl;
}

ite = iht.begin();
for( int i = 0; i < iht.size(); ++i, ++ite)
cout << *ite << endl;
cout << endl;

cout << *(iht.find(2)) << endl;
cout << iht.count(2) << endl;

return 0;

}


程序结果:

0

53

4294967291

6

53 55 2 108 59 63

bucket[0] has 1elems.

bucket[2] has 3elems.

bucket[6] has 1elems.

bucket[10] has 1elems.

54

97

bucket[0] has 1elems.

bucket[1] has 1elems.

bucket[2] has 2elems.

bucket[3] has 1elems.

bucket[4] has 1elems.

bucket[5] has 1elems.

bucket[6] has 1elems.

bucket[7] has 1elems.

bucket[8] has 1elems.

bucket[9] has 1elems.

bucket[10] has 1elems.

bucket[11] has 2elems.

bucket[12] has 1elems.

bucket[13] has 1elems.

bucket[14] has 1elems.

bucket[15] has 1elems.

bucket[16] has 1elems.

bucket[17] has 1elems.

bucket[18] has 1elems.

bucket[19] has 1elems.

bucket[20] has 1elems.

bucket[21] has 1elems.

bucket[22] has 1elems.

bucket[23] has 1elems.

bucket[24] has 1elems.

bucket[25] has 1elems.

bucket[26] has 1elems.

bucket[27] has 1elems.

bucket[28] has 1elems.

bucket[29] has 1elems.

bucket[30] has 1elems.

bucket[31] has 1elems.

bucket[32] has 1elems.

bucket[33] has 1elems.

bucket[34] has 1elems.

bucket[35] has 1elems.

bucket[36] has 1elems.

bucket[37] has 1elems.

bucket[38] has 1elems.

bucket[39] has 1elems.

bucket[40] has 1elems.

bucket[41] has 1elems.

bucket[42] has 1elems.

bucket[43] has 1elems.

bucket[44] has 1elems.

bucket[45] has 1elems.

bucket[46] has 1elems.

bucket[47] has 1elems.

bucket[53] has 1elems.

bucket[55] has 1elems.

bucket[59] has 1elems.

bucket[63] has 1elems.

0

1

2

2

3

4

5

6

7

8

9

10

11

108

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

53

55

59

63

2

2

Hash_set的使用:

#include <iostream>
#include <hash_set.h>
#include <cstring>

using namespace std;

struct eqstr
{
bool operator() (const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

void lookup( const hash_set<const char*, hash<const char*>, eqstr>& Set, const char* word)
{
hash_set<const char*, hash<const char*>,eqstr>::const_iterator it = Set.find(word);
cout << "  "<< word << ": " << (it != Set.end()?"present" : "not present") << endl;
}

int main()
{

hash_set<const char*, hash<const char*>, eqstr> Set;
Set.insert("kiwi");
Set.insert("plum");
Set.insert("apple");
Set.insert("mango");
Set.insert("apricot");
Set.insert("banana");

lookup(Set, "mango");
lookup(Set, "apple");
lookup(Set, "durian");

hash_set<const char*, hash<const char*>, eqstr>::iteratorite1 = Set.begin();
hash_set<const char*, hash<const char*>, eqstr>::iteratorite2 = Set.end();
for(; ite1 != ite2; ++ite1)
{
cout << *ite1 << ' ';
}

return 0;
}


程序结果:

mango: present

apple: present

durian: not present

banana plum mango apple kiwi apricot

hash_map的使用:

#include <iostream>
#include <hash_map.h>
#include <cstring>

using namespace std;

struct eqstr
{
bool operator() (const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};

int main()
{
hash_map<const char*, int, hash<const char*>, eqstr> days;

days["january"] = 31;
days["february"] = 28;
days["march"] = 31;
days["april"] = 30;
days["may"] = 31;
days["june"] = 30;
days["july"] = 31;
days["august"] = 31;
days["september"] = 30;
days["october"] = 31;
days["november"] = 30;
days["december"] = 31;

cout << "september -> " <<days["september"] << endl;
cout << "june -> " << days["june"]<< endl;
cout << "february -> " <<days["february"] << endl;
cout << "december -> " <<days["december"] << endl;

hash_map<const char*, int, hash<const char*>,eqstr>::iterator ite1 = days.begin();
hash_map<const char*, int, hash<const char*>,eqstr>::iterator ite2 = days.end();
for(; ite1 != ite2; ++ite1)
{
cout << ite1->first << ' ';
}

return 0;
}


程序结果:

september -> 30

june -> 30

february -> 28

december -> 31

september june july may january februarydecember march april november october a

august
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: