首页 > 其他 > 详细

STL的容器类和迭代器(一)

时间:2021-02-15 23:01:49      阅读:27      评论:0      收藏:0      [点我收藏+]

1. 容器类介绍

1.1. 容器介绍

(1)容器是盛放东西的东西,这里被盛放的一般是数据对象,用来盛放的是容器类

(2)C语言语法内置的数组与结构体,就是语言源生支持的容器

(3)C++容器通过类库的方式提供,容器类库被模板技术泛化后,就是STL容器了

 

1.2. STL中的容器类

(1)序列容器。元素在容器中的位置与元素的值无关,容器是不排序的(类似C数组),包括array、vector、deque、list、forward_list等几个

(2)排序容器。数据插入时即自动按照值从小到大排列好,包括set、multiset、map、multimap等

(3)哈希容器。哈希容器中的元素是未排序的,元素的位置由哈希函数确定,即遵守一定规则的<key, value>对式存储,包括unordered_set、unordered_map、hash_set、hash_multiset、hash_map、hash_multimap

 

2. 容器类array的初步使用

2.1. array的特性

(1)array 是定长、同类型多元素、内存中连续排布的一种容器

(2)array其实就是C语言数组的C++ template封装

 

2.2. array的构造与初始化

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     
 9     array<int, 3> a1;                //正确,定义了未初始化,值是随机的
10     //array<int, 3> a2(1, 4, 5);    //错误,C++无法提供无数个构造函数来匹配
11     array<int, 3> a3{1, 4, 5};        //正确,属于聚合初始化
12     array<int, 3> a4 = {1, 4, 5};    //正确,初始化赋值
13     array<int, 3> a5 = a4;            //拷贝构造
14     
15     return 0;
16 }
array的构造与初始化

 

2.3. array的元素访问

(1)at方法

(2)operator[]实现的C数组式访问

(3)front和back方法返回第1个和最后1个元素

(4)data返回真实存储内存中首元素首地址的值

 

2.4. array的容量设置和获取

(1)容量设置只能在定义时一次设定,且必须设定,设定后不能修改

(2)empty

(3)size

(4)max_size

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     array<int, 3> a1{2, 4, 6};
 9     
10     cout << "a1.at(0) = " << a1.at(0) << endl;
11     cout << "a1.at(1) = " << a1.at(1) << endl;
12     cout << "a1.at(2) = " << a1.at(2) << endl;
13     
14     cout << "a1[0] = " << a1[0] << endl;
15     cout << "a1[1] = " << a1[1] << endl;
16     cout << "a1[2] = " << a1[2] << endl;
17     
18     cout << "a1.front() = " << a1.front() << endl;
19     cout << "a1.back() = " << a1.back() << endl;
20     
21     int* p = a1.data();
22     cout << "a1.data() = " << a1.data() << ", *p = " << *p << endl;
23     
24     cout << "a1.empty() = " << a1.empty() << endl;
25     cout << "a1.size() = " << a1.size() << endl;
26     cout << "a1.max_size() = " << a1.max_size() << endl;
27      
28     return 0;
29 }
30 
31 /*
32 输出结果:
33 a1.at(0) = 2
34 a1.at(1) = 4
35 a1.at(2) = 6
36 a1[0] = 2
37 a1[1] = 4
38 a1[2] = 6
39 a1.front() = 2
40 a1.back() = 6
41 a1.data() = 0x7ffc0b2328ec, *p = 2
42 a1.empty() = 0
43 a1.size() = 3
44 a1.max_size() = 3
45 */
array的元素访问和容量获取

 

2.5. array的fill和swap操作

(1)fill

以指定值填充容器

(2)swap

交换内容

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     array<int, 5> a1{1, 2, 3, 2, 5};
 9     
10     cout << "a1[0] = " << a1[0] << endl; 
11     cout << "a1[1] = " << a1[1] << endl; 
12     cout << "a1[2] = " << a1[2] << endl; 
13     cout << "a1[3] = " << a1[3] << endl; 
14     cout << "a1[4] = " << a1[4] << endl; 
15     array<int, 5> b1{6, 6, 6, 6, 6};
16     a1.swap(b1);
17     cout << "a1[0] = " << a1[0] << endl; 
18     cout << "a1[1] = " << a1[1] << endl; 
19     cout << "a1[2] = " << a1[2] << endl; 
20     cout << "a1[3] = " << a1[3] << endl; 
21     cout << "a1[4] = " << a1[4] << endl; 
22     a1.fill(5);
23     cout << "a1[0] = " << a1[0] << endl; 
24     cout << "a1[1] = " << a1[1] << endl; 
25     cout << "a1[2] = " << a1[2] << endl; 
26     cout << "a1[3] = " << a1[3] << endl; 
27     cout << "a1[4] = " << a1[4] << endl; 
28     
29     return 0;
30 }
31 
32 /*输出结果:
33 a1[0] = 1
34 a1[1] = 2
35 a1[2] = 3
36 a1[3] = 2
37 a1[4] = 5
38 a1[0] = 6
39 a1[1] = 6
40 a1[2] = 6
41 a1[3] = 6
42 a1[4] = 6
43 a1[0] = 5
44 a1[1] = 5
45 a1[2] = 5
46 a1[3] = 5
47 a1[4] = 5
48 */
fill和swap示例

 

2.6. array的非成员函数使用

(1)operator函数重载

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     array<int, 3> a1{1, 3, 5};
 9     array<int, 3> a2{2, 4, 6};
10     
11     cout << "a1 == a2 ?" << (a1 == a2) << endl;
12     cout << "a1 > a2 ? " << (a1 > a2) << endl;
13     cout << "a1 < a2 ? " << (a1 < a2) << endl;
14     
15     return 0;
16 }
17 
18 /*
19 输出结果:
20 a1 == a2 ?0
21 a1 > a2 ? 0
22 a1 < a2 ? 1
23 */
operator函数重载

 

(2)get

从array中提取第I个元素,且I必须是范围[0, N)中的整数值

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     array<int, 3> a1{1, 3, 5};
 9     array<int, 3> a2{2, 4, 6};
10     
11     int x = 0;
12     x = get<0, int, 3>(a1);
13     cout << "x = " << x << endl;
14     
15     x = get<1, int, 3>(a1);
16     cout <<  "x = " << x << endl;
17     
18     x = get<2, int, 3>(a1);
19     cout << "x = " << x << endl;
20     
21     return 0;
22 }
23 
24 /*
25 输出结果:
26 x = 1
27 x = 3
28 x = 5
29 */
get

 

 

(3)swap

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     array<int, 3> a1{1, 2, 3};
 9     array<int, 3> a2{2, 4, 8};
10     
11     swap<int, 3>(a1, a2);
12     
13     cout << "a[0] = " << a1[0] << endl;
14     cout << "a[1] = " << a1[1] << endl;
15     cout << "a[2] = " << a1[2] << endl;
16     
17     return 0;
18 }
19 
20 /*
21 输出结果:
22 a[0] = 2
23 a[1] = 4
24 a[2] = 8
25 */
swap

 

(4)to_array(C++20)

将一个C数组转换为C++中的array

std::to_array("foo");  //创建std:: array<char, 4>{‘f‘, ‘o‘, ‘o‘, ‘\0‘}

std::array{"foo"};    //创建std::array<const char*, 1>{+"foo"}

技术分享图片
 1 #include <iostream>
 2 #include <array>
 3 
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     int a[3] = {1, 3, 5};
 9     array<int, 3> b = to_array<int, 3>(a);
10     
11     cout << "b[0] = " << b[0] << endl;
12     cout << "b[1] = " << b[1] << endl;
13     cout << "b[2] = " << b[2] << endl;
14     
15     return 0;
16 }
to_array(C++20)

 

STL的容器类和迭代器(一)

原文:https://www.cnblogs.com/Ramentherapy/p/14403847.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!