小白学习C++ 教程二十一C++ 中的STL容器Arrays和vector

Posted 刘润森!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小白学习C++ 教程二十一C++ 中的STL容器Arrays和vector相关的知识,希望对你有一定的参考价值。

@Author:Runsen

C++的标准模板库(STL)是提供数组、向量、队列等数据结构的模板类的集合。STL是由容器、算法、迭代器组成的库。

容器

容器存储对象和数据。它们基本上是基于模板的泛型类。

STL中的容器分为以下几种:

  • 顺序容器
    可以以顺序或线性方式访问的容器称为顺序容器。

Array, vector, queue, deque, list, map, set 是线性存储数据的 STL 容器,可以按顺序访问。

  • 关联容器

关联容器是实现排序数据结构的容器。这些容器可以快速搜索。关联容器的一些示例是 Map、Set、MultiMap、Multiset 等。这些容器通常以键/值对的方式实现。

Arrays

声明Arrays 容器的一般语法是:

array<object_type, size> array_name;

上面的声明创建了一个数组容器“array_name”,其大小为“size”,对象类型为“object_type”。

我们也可以初始化这个数组容器,如下所示,

array<int,5> myarray = 1,1,2,3,5;

数组容器支持的一些功能包括:

  • At:返回数组容器中给定位置的值。如果指定的位置超出数组限制,则抛出“Out_of_range”异常。
  • Front:返回数组容器中的第一个元素。
  • Back:如果容器被完全填满,则返回数组容器中的最后一个元素,另一个返回容器中最右边的元素。
  • Fill:为数组容器中的每个元素分配一个给定的值。
  • Swap:交换具有相同类型和相同大小索引的两个数组的内容。
  • Empty:用于检查数组容器是否为空的布尔函数。
  • Size:返回数组容器中的元素数。
  • Max_size:返回数组容器的最大大小。
  • Begin:返回指向数组容器开头的迭代器,即数组的第一个元素。
  • End:返回指向数组容器中最后一个元素旁边位置的迭代器。
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
 
using namespace std;
 
int main() 
  array<int, 5> myarray = 1, 1, 2, 3, 5;
   
  cout << "Size of array: " << endl;
  cout << myarray.size() << endl;
   
  cout << "myarray contents: "  << endl;
  for (auto i : myarray)
    cout << i << ' ';
     
  // sort operation
 sort(myarray.begin(), myarray.end());
    
   cout << "\\nsorted myarray : ";
   for (auto i : myarray)
      cout << i << ' ';
   
   cout<<"\\nFirst element of myarray "<<myarray.at(0);
   cout<<endl;
    
   cout<<"FRONT myarray: "<<myarray.front();
   cout<<endl;
   cout<<"BACK myarray: "<<myarray.back();
   cout<<endl;
    
   // Filling ar2 with 10
   myarray.fill(8);
    
  cout << "\\nFilled myarray : ";
  for (auto i : myarray)
     cout << i << ' ';
 
return 0;

输出如下

Size of array:
5

myarray contents: 1 1 2 3 5
sorted myarray: 1 1 2 3 5
The first element of myarray 1
FRONT myarray: 1
BACK myarray: 5

Filled myarray: 8 8 8 8 8

vector

array是固定大小的静态数组。

如果在程序中间我们必须在数组中存储更多元素,那么当我们尝试存储超出数组限制的元素时,肯定会得到“out_of_bound”异常。

vector是动态数组容器,可在插入或删除元素时自动调整其大小。向量的存储由向量容器本身处理。

vector中的元素存储在连续的位置。就像数组一样,向量元素也可以使用迭代器遍历和访问。

vector支持以下迭代器函数来遍历元素:

  • begin() –返回指向向量容器第一个元素的迭代器。
  • end() –返回指向向量中最后一个元素之后的元素的迭代器。
  • rbegin() –返回指向向量容器中最后一个元素的反向迭代器。
  • rend() –返回指向向量容器第一个元素的反向迭代器。
  • cbegin() –返回指向向量容器中第一个元素的常量迭代器。
  • cend() –返回指向向量容器最后一个元素之后的元素的常量迭代器。
  • crbegin() –返回指向向量容器中最后一个元素的反向常量迭代器。
    -crend() –返回指向向量容器中第一个元素的反向常量迭代器。
#include <iostream>
#include <vector>
using namespace std;
int main()
   
      vector<int> v1;
  
      for (int i = 1; i <= 5; i++)
         v1.push_back(i+1);
  
      for (auto i = v1.begin(); i != v1.end(); ++i)
         cout << *i << " ";
  
         cout << "\\nOutput of Vector with rbegin and rend: ";
      for (auto itr = v1.rbegin(); itr != v1.rend(); ++itr)
         cout << *itr << " ";
   
         cout << "\\nOutput Vector of with cbegin and cend: ";
      for (auto itc = v1.cbegin(); itc != v1.cend(); ++itc)
         cout << *itc << " ";
  
         cout << "\\nOutput Vector of with crbegin and crend : ";
      for (auto icr = v1.crbegin(); icr != v1.crend(); ++icr)
         cout << *icr << " ";
         return 0;

输出如下:

Output of Vector with rbegin and rend: 6 5 4 3 2
Output Vector of with cbegin and cend: 2 3 4 5 6
Output Vector of with crbegin and crend: 6 5 4 3 2

函数 size() 返回向量容器中的元素数。这是 std::vector 类的内置函数,可直接用于查找向量的大小。

我们还可以将向量调整为所需的大小,使其可以容纳“n”个元素。这是通过 std::vector类的“resize()”函数实现的。resize 函数以向量的大小为参数,然后将向量容器的大小调整为指定的大小。

#include <iostream>
#include <vector>
using namespace std;
int main()

   vector<int> myvec = 1, 1, 2, 3, 5, 8;
    cout << "Vector Size : " << myvec.size();
   cout << "\\nVector elements are: ";
   for (auto it = myvec.begin(); it != myvec.end(); it++)
                 cout << *it << " ";
   myvec.resize(4);
   cout << "\\nVector Size after resize: " << myvec.size();
   cout << "\\nVector elements after resizing are: ";
   for (auto it = myvec.begin(); it != myvec.end(); it++)
                 cout << *it << " ";
   return 0;

输出如下:

Vector Size : 6
Vector elements are: 1 1 2 3 5 8
Vector Size after resize: 4
Vector elements after resizing are: 1 1 2 3

对向量进行排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()

    vector<int> myvec =  10,50,30,20,60,40 ;

    for (auto i = myvec.begin(); i < myvec.end(); ++i)
    
        cout << *i << " ";
    
    cout << endl;

    sort(myvec.begin(), myvec.end());

    for (auto i = myvec.begin(); i < myvec.end(); ++i)
    
        cout << *i << " ";
    
    cout << endl;

输出如下

10 50 30 20 60 40
10 20 30 40 50 60

向量类 std::vector 提供了另一个将值插入向量的函数Insert 。Insert 函数允许我们在指定位置之前向向量中插入元素。

#include <iostream>
#include <vector>
using namespace std;

int main()

	vector<int> myvec =  2,3,4 ;

	for (int i = 0; i < myvec.size(); i++)
		cout << myvec[i] << " ";
	cout << endl;
	myvec.insert(myvec.begin(), 20);
	myvec.insert(myvec.begin() + 1, 30);
	for (int i = 0; i < myvec.size(); i++)
		cout << myvec[i] << " ";

输出如下

2 3 4
20 30 2 3 4

vector 类还为我们提供了将一个向量的内容与另一个相同类型和大小的向量的内容交换或交换的能力。这是通过矢量内置函数“swap”实现的。

#include <iostream>
#include <vector>
using namespace std;

int main()

    // swap operation
    vector<int> v1, v2;
    v1.push_back(1);
    v1.push_back(3);
    v2.push_back(5);
    v2.push_back(7);

    cout << "\\nVector 1: ";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";

    cout << "\\nVector 2: ";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";

    // Swaps v1 and v2
    v1.swap(v2);

    cout << "\\nAfter Swap \\nVector 1: ";
    for (int i = 0; i < v1.size(); i++)
        cout << v1[i] << " ";

    cout << "\\nVector 2: ";
    for (int i = 0; i < v2.size(); i++)
        cout << v2[i] << " ";

输出如下

Vector 1: 1 3
Vector 2: 5 7
After Swap
Vector 1: 5 7
Vector 2: 1 3

函数“find”用于查找向量中是否存在特定元素(称为键)。一旦找到该值,函数就会返回。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
  
int main()

   // Assign vector
   vector<int> myvec = 1,1,2,3,5,8;
   cout<<"\\nInput vector: ";
   for(auto it=myvec.begin();it<myvec.end();it++)
      cout<<*it<<" ";
   int key;
   cout<<"\\nEnter the key to be searched: "; cin>>key;
   if(find(myvec.begin(),myvec.end(),key)!= myvec.end())
      cout<<"\\nElement found";
   else
      cout<<"\\nElement not found";
  

以上是关于小白学习C++ 教程二十一C++ 中的STL容器Arrays和vector的主要内容,如果未能解决你的问题,请参考以下文章

小白学习C++ 教程二十二C++ 中的STL容器stackqueue和map

小白学习C++ 教程二十二C++ 中的STL容器stackqueue和map

小白学习C++ 教程二十C++ 中的auto关键字

小白学习C++ 教程二十三如何安装和使用 C++ 标准库

小白学习C++ 教程二十三如何安装和使用 C++ 标准库

小白学习C++ 教程十一C++类中访问修饰符