Fork me on GitHub

STL—— 容器(vector)的各种功能方法

1. 获取容器的元素个数 size() 

 使用 vectorName.size() 可以输出这个容器中类型的个数,如下代码:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num(3, 111);
 9     cout << "num 的元素个数:" << num.size() << endl;
10 
11     return 0;
12 }

打印结果:

 

 

 

2. 获取容器的大小 capacity()

 使用 vectorName.capacity() 可以输出这个容器分配了多少个元素的内存空间,如下代码: 

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num(5, 111);
 9     cout << "num 容器的大小:" << num.capacity() << endl; 
10 
11     return 0;
12 }

打印结果:

 

 

 

 

3. vector 容器是否为空 empty() 

使用 vectorName.empty() 方法可以获取容器是否为空,如果为空将会返回 true ,否则返回false,如下:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num;
 9 
10     if (num.empty())
11     {
12         cout << "容器 num 为空" << endl;
13     }
14 
15     return 0;
16 }

打印结果:

 

 

 

 

4. 重新调整 vector 的大小: resize()

缩小:使用 vectorName.resize() 可以重新分配 vector 的大小,重新分配后 vector 的元素个数将会改变,但是容器的大小不会改变,如下代码:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num(5, 888);
 9 
10     printf("使用 resize 之前\n");
11     cout << "使用 resize 之前 num 的元素数量:" << num.size() << endl;
12     cout << "使用 resize 之前 num 的空间大小:" << num.capacity() << endl;
13     
14     for (int i = 0; i < num.size(); i++)
15     {
16         cout << num[i] << endl;
17     }
18 
19     num.resize(2);
20 
21     cout << "使用 resize 之后 num 的元素数量:" << num.size() << endl;
22     cout << "使用 resize 之后 num 的空间大小:" << num.capacity() << endl;
23     for (int i = 0; i < num.size(); i++)
24     {
25         cout << num[i] << endl;
26     }
27 
28     return 0;
29 }

打印结果:

 

 增加:上边是将 vector 的容量缩小,resize() 的另一种用法可以将 vector 扩容,如下:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     vector<int>num(5, 888);
 9 
10     printf("使用 resize 之前\n");
11     cout << "使用 resize 之前 num 的元素数量:" << num.size() << endl;
12     cout << "使用 resize 之前 num 的空间大小:" << num.capacity() << endl;
13     
14     for (int i = 0; i < num.size(); i++)
15     {
16         cout << num[i] << endl;
17     }
18 
19     num.resize(10, 111);    //可以将 vector 扩容,将5变为10,并且将新扩展的元素初始化为 111,如果不写 111 会将新增加的元素初始化为 0
20 
21     cout << "使用 resize 之后 num 的元素数量:" << num.size() << endl;
22     cout << "使用 resize 之后 num 的空间大小:" << num.capacity() << endl;
23     for (int i = 0; i < num.size(); i++)
24     {
25         cout << num[i] << endl;
26     }
27 
28     return 0;
29 }

 

打印结果:

 

5. 获取&修改 vector 容器的第一个和最后一个元素的值 front() 和 back() 

获取:使用 vectorName.front() 与 vectorName.back() 来获取 vector 的第一个元素与最后一个元素的引用,如下代码:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int test[] = { 111,222,333,444,555,666,777,888,999 };
 9     vector<int>num(test, test + 9);
10 
11     cout << "num 的第一个元素为:" << num.front() << endl;
12     cout << "num 的最后一个元素为:" << num.back() << endl;
13 
14     return 0;
15 }

打印结果:

 

 

 

 修改:因为返回的是其引用,那么我们也可以进行赋值操作,如下代码:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int test[] = { 111,222,333,444,555,666,777,888,999 };
 9     vector<int>num(test, test + 9);
10 
11     cout << "num 的第一个元素为:" << num.front() << endl;
12     cout << "num 的最后一个元素为:" << num.back() << endl;
13 
14     cout << "=========改变首尾的值========" << endl;
15     num.front() = 1;
16     num.back() = 9;
17 
18     for (int i = 0; i < num.size(); i++)
19     {
20         cout << num.at(i) << endl;
21     }
22 
23     return 0;
24 }

打印结果:

 

 

 

 

 

 

 

 

=========================================================================================================================

posted @ 2020-04-13 02:38  索智源  阅读(314)  评论(0编辑  收藏  举报