日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2024/10/8 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【小白学习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 operationsort(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 10myarray.fill(8);cout << "\nFilled myarray : ";for (auto i : myarray)cout << i << ' ';return 0; }

輸出如下

Size of array: 5myarray contents: 1 1 2 3 5 sorted myarray: 1 1 2 3 5 The first element of myarray 1 FRONT myarray: 1 BACK myarray: 5Filled 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 operationvector<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 v2v1.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 vectorvector<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";elsecout<<"\nElement not found";}

總結

以上是生活随笔為你收集整理的【小白学习C++ 教程】二十一、C++ 中的STL容器Arrays和vector的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。