vs網(wǎng)站中的輪播怎么做軟文寫作技巧有哪些
C++之STL整理(2)之vector用法(創(chuàng)建、賦值、方法)整理
注:整理一些突然學(xué)到的C++知識,隨時(shí)mark一下
例如:忘記的關(guān)鍵字用法,新關(guān)鍵字,新數(shù)據(jù)結(jié)構(gòu)
C++ 的vector用法整理
- C++之STL整理(2)之vector用法(創(chuàng)建、賦值、方法)整理
- 一、vector的初始化
- 1、默認(rèn)構(gòu)造函數(shù)
- 2、拷貝構(gòu)造函數(shù)copy區(qū)間
- 3、指定數(shù)量和元素值的構(gòu)造函數(shù)
- 4、指定數(shù)量的構(gòu)造函數(shù)
- 5、拷貝構(gòu)造函數(shù)
- 二、vector的初始化-賦值
- 1、.assign(beg, end) 賦值操作
- 2、.assign(n, elem) 賦值操作
- 3、重載等號操作符 operator=
- 4、直接列表初始化 `Vector<T> {,};`
- 5、swap 函數(shù)
- 三、數(shù)據(jù)得增刪查改
- 1、at(int id)接口
- 2、front()接口
- 3、back()接口
- 4、[id]直接取
- 5、插入函數(shù)
- 6、尾部添加 push_back(ele)
- 7、尾部刪去 pop_back()
- 8、刪區(qū)間
- 9、刪指定位置
- 10、清空
- 四、其他接口
- 1、size()成員函數(shù)
- 2、empty()
- 3、resize(int num)
- 4、capacity()
- 5、reserve(int len)
- 總結(jié)
提示:本文為 C++ 中 vector構(gòu)造、賦值、接口 的寫法和舉例
一、vector的初始化
??根據(jù)vector的以下封裝好的構(gòu)造函數(shù),現(xiàn)在示例每種構(gòu)造方式的創(chuàng)建:
vector構(gòu)造函數(shù):vector<T> v; //默認(rèn)構(gòu)造函數(shù)
vector(v.begin(), v.end());//將v[begin(), end())區(qū)間中的元素copy給對象。
vector(n, elem);//構(gòu)造函數(shù)將n個(gè)elem賦給對象。
vector(n);//構(gòu)造函數(shù)將n個(gè)0或空賦值給對象。
vector(const vector &vec);//拷貝構(gòu)造函數(shù)。
1、默認(rèn)構(gòu)造函數(shù)
創(chuàng)建一個(gè)空的vector。
#include <iostream>
#include <vector> int main() { std::vector<int> v; // 創(chuàng)建一個(gè)空的vector std::cout << "Size of vector v: " << v.size() << std::endl; // 輸出:Size of vector v: 0 return 0;
}
2、拷貝構(gòu)造函數(shù)copy區(qū)間
假設(shè)有一個(gè)已存在的vector,我們想要?jiǎng)?chuàng)建一個(gè)新的vector并拷貝其中一部分元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> v2(v.begin(), v.begin() + 3); // 拷貝前3個(gè)元素 for (int num : v2) { std::cout << num << " "; // 輸出:1 2 3 } std::cout << std::endl; return 0;
}
3、指定數(shù)量和元素值的構(gòu)造函數(shù)
創(chuàng)建一個(gè)包含特定數(shù)量且所有元素都具有相同值的vector。
#include <iostream>
#include <vector> int main() { std::vector<int> v(5, 10); // 創(chuàng)建一個(gè)包含5個(gè)10的vector for (int num : v) { std::cout << num << " "; // 輸出:10 10 10 10 10 } std::cout << std::endl; return 0;
}
4、指定數(shù)量的構(gòu)造函數(shù)
創(chuàng)建一個(gè)包含特定數(shù)量的vector,所有元素默認(rèn)初始化為該類型的默認(rèn)值(對于基本類型如int,默認(rèn)值為0)。
#include <iostream>
#include <vector> int main() { std::vector<int> v(5); // 創(chuàng)建一個(gè)包含5個(gè)0的vector for (int num : v) { std::cout << num << " "; // 輸出:0 0 0 0 0 } std::cout << std::endl; return 0;
}
5、拷貝構(gòu)造函數(shù)
從另一個(gè)已存在的vector創(chuàng)建一個(gè)新的vector,作為它的拷貝。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; std::vector<int> v2(v); // 使用拷貝構(gòu)造函數(shù)創(chuàng)建v2,它是v的拷貝 for (int num : v2) { std::cout << num << " "; // 輸出:1 2 3 4 5 } std::cout << std::endl; return 0;
}
二、vector的初始化-賦值
vector常用賦值方式:
assign(beg, end);//將[beg, end)區(qū)間中的數(shù)據(jù)拷貝賦值。
assign(n, elem);//將n個(gè)elem拷貝賦值。
vector& operator=(const vector &vec);//重載=
Vector<T> = {,,,};//直接賦值一個(gè)數(shù)組
Vector<T> {,,,};
swap(vec);// 與vec的元素互換。
1、.assign(beg, end) 賦值操作
使用區(qū)間迭代器將另一個(gè)容器或數(shù)組中的數(shù)據(jù)拷貝到vector中。
#include <iostream>
#include <vector>
#include <algorithm> // for std::next int main() { std::vector<int> v = {1, 2, 3, 4, 5}; int arr[] = {6, 7, 8, 9, 10}; v.assign(std::begin(arr), std::end(arr)); // 賦值arr數(shù)組到v for (int num : v) { std::cout << num << " "; // 輸出:6 7 8 9 10 } std::cout << std::endl; return 0;
}
2、.assign(n, elem) 賦值操作
將n個(gè)值為elem的元素拷貝到vector中。
#include <iostream>
#include <vector> int main() { std::vector<int> v; v.assign(5, 10); // 賦值5個(gè)10到v for (int num : v) { std::cout << num << " "; // 輸出:10 10 10 10 10 } std::cout << std::endl; return 0;
}
3、重載等號操作符 operator=
使用重載的等號操作符將一個(gè)vector的內(nèi)容賦給另一個(gè)vector。
#include <iostream>
#include <vector> int main() { std::vector<int> v1 = {1, 2, 3, 4, 5}; std::vector<int> v2; v2 = v1; // 使用重載的等號操作符賦值v1到v2 for (int num : v2) { std::cout << num << " "; // 輸出:1 2 3 4 5 } std::cout << std::endl; return 0;
}
4、直接列表初始化 Vector<T> {,};
在創(chuàng)建vector對象時(shí),直接使用列表初始化語法。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {21, 22, 23, 24, 25}; // 直接列表初始化 for (int num : v) { std::cout << num << " "; // 輸出:21 22 23 24 25 } std::cout << std::endl; return 0;
}
5、swap 函數(shù)
使用std::swap或vector的成員函數(shù)swap來交換兩個(gè)vector的內(nèi)容。
#include <iostream>
#include <vector>
#include <algorithm> // for std::swap int main() { std::vector<int> v1 = {1, 2, 3}; std::vector<int> v2 = {4, 5, 6}; std::swap(v1, v2); // 使用std::swap交換v1和v2的內(nèi)容 // 輸出交換后的v1 for (int num : v1) { std::cout << num << " "; }std::cout << std::endl; return 0;
}
三、數(shù)據(jù)得增刪查改
vector數(shù)據(jù)存取操作,主要有at()、front()、back()接口和括號[]取得方法。
vector插入和刪除操作,主要有insert、erase、clear、pushback、popback接口。
1、at(int id)接口
at成員函數(shù)用于通過索引訪問元素,并且在索引越界時(shí)會(huì)拋出std::out_of_range
異常。
#include <iostream>
#include <vector>
#include <stdexcept> // for std::out_of_range int main() { std::vector<int> v = {10, 20, 30, 40, 50}; try { std::cout << v.at(2) << std::endl; // 輸出:30 std::cout << v.at(10) << std::endl; // 拋出std::out_of_range異常 } catch (const std::out_of_range& e) { std::cerr << "Index out of range: " << e.what() << std::endl; } return 0;
}
2、front()接口
front成員函數(shù)返回容器中第一個(gè)元素的引用。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; std::cout << v.front() << std::endl; // 輸出:10 return 0;
}
3、back()接口
back成員函數(shù)返回容器中最后一個(gè)元素的引用。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; std::cout << v.back() << std::endl; // 輸出:50 return 0;
}
4、[id]直接取
operator[]通過索引直接訪問元素,如果越界,行為是未定義的(通常會(huì)導(dǎo)致程序崩潰)。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; std::cout << v[2] << std::endl; // 輸出:30 // std::cout << v[10] << std::endl; // 未定義行為,通常會(huì)導(dǎo)致程序崩潰 return 0;
}
5、插入函數(shù)
insert(const_iterator pos, int count, ele)
在迭代器pos指向的位置插入count個(gè)值為ele的元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 40, 50}; v.insert(v.begin() + 1, 2, 30); // 在索引1的位置插入2個(gè)30 for (int num : v) { std::cout << num << " "; // 輸出:10 30 30 20 40 50 } std::cout << std::endl; return 0;
}
6、尾部添加 push_back(ele)
在容器的尾部插入一個(gè)元素ele。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30}; v.push_back(40); // 在尾部插入40 for (int num : v) { std::cout << num << " "; // 輸出:10 20 30 40 } std::cout << std::endl; return 0;
}
7、尾部刪去 pop_back()
刪去容器的最后一個(gè)元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40};v.pop_back(); // 刪除最后一個(gè)元素40 for (int num : v) { std::cout << num << " "; // 輸出:10 20 30 } std::cout << std::endl; return 0;
}
8、刪區(qū)間
erase(const_iterator start, const_iterator end)
刪去從迭代器start到end(不包括end)之間的所有元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; v.erase(v.begin() + 1, v.begin() + 3); // 刪除索引1到2之間的元素(即20和30) for (int num : v) { std::cout << num << " "; // 輸出:10 40 50 } std::cout << std::endl; return 0;
}
9、刪指定位置
erase(const_iterator pos)
刪除迭代器pos指向的元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; v.erase(v.begin() + 2); // 刪除索引2的元素(即30) for (int num : v) { std::cout << num << " "; // 輸出:10 20 40 50 } std::cout << std::endl; return 0;
}
10、清空
clear()
刪掉容器中的所有元素。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; v.clear(); // 刪除所有元素 if (v.empty()) { std::cout << "Vector is empty." << std::endl; // 輸出:Vector is empty. } return 0;
}
這些操作提供了對vector容器內(nèi)容的靈活操作,可以輕松地插入和刪除元素,以滿足程序的需要。使用迭代器(或索引)時(shí),請確保它們指向有效的容器位置,以避免未定義行為或異常。
四、其他接口
1、size()成員函數(shù)
返回容器中的元素個(gè)數(shù)。這個(gè)數(shù)量等于當(dāng)前vector實(shí)際包含的元素?cái)?shù)。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30, 40, 50}; std::cout << "Size of vector: " << v.size() << std::endl; // 輸出:Size of vector: 5 return 0;
}
2、empty()
empty成員函數(shù)檢查容器是否為空。如果容器中沒有元素,它返回true;否則返回false。
#include <iostream>
#include <vector> int main() { std::vector<int> v; if (v.empty()) { std::cout << "Vector is empty." << std::endl; // 輸出:Vector is empty. } else { std::cout << "Vector is not empty." << std::endl; } return 0;
}
3、resize(int num)
resize成員函數(shù)用于改變?nèi)萜鞯拇笮?。如果新的大小num大于當(dāng)前大小,則容器會(huì)增長,新添加的元素會(huì)被初始化為默認(rèn)值(對于內(nèi)置類型,通常是0)。如果num小于當(dāng)前大小,則末尾超出的元素會(huì)被刪除。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30}; v.resize(5); // 容器增長,新元素初始化為0 for (int num : v) { std::cout << num << " "; // 輸出:10 20 30 0 0 } std::cout << std::endl; v.resize(2); // 容器縮短,超出的元素被刪除 for (int num : v) { std::cout << num << " "; // 輸出:10 20 } std::cout << std::endl; return 0;
}
resize(int num, elem)
是resize的另一種重載形式。它允許你為新的元素指定一個(gè)初始值elem。如果容器增長,新添加的元素會(huì)被初始化為elem。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30}; v.resize(5, 42); // 容器增長,新元素初始化為42 for (int num : v) { std::cout << num << " "; // 輸出:10 20 30 42 42 } std::cout << std::endl; return 0;
}
4、capacity()
capacity成員函數(shù)返回容器當(dāng)前分配的存儲(chǔ)空間大小。這通常大于或等于size返回的值,因?yàn)関ector為了效率可能會(huì)預(yù)留一些額外的空間。
#include <iostream>
#include <vector> int main() { std::vector<int> v = {10, 20, 30}; std::cout << "Size: " << v.size() << ", Capacity: " << v.capacity() << std::endl; // 輸出可能類似于:Size: 3, Capacity: 3 或 Size: 3, Capacity: 4(取決于實(shí)現(xiàn)) return 0;
}
5、reserve(int len)
reserve成員函數(shù)用于預(yù)分配容器的存儲(chǔ)空間。它并不改變?nèi)萜鞯拇笮?#xff08;即size的值不變),但會(huì)增加容器的capacity。預(yù)分配空間可以提高插入元素的效率,因?yàn)楫?dāng)容器需要增長時(shí),它可能不需要重新分配整個(gè)存儲(chǔ)空間。reserve成員函數(shù)用于預(yù)分配容器的存儲(chǔ)空間,它可以幫助提高插入元素的效率,因?yàn)楫?dāng)容器需要增長時(shí),如果已經(jīng)有足夠的預(yù)留空間,它就可以避免重新分配整個(gè)存儲(chǔ)空間。
這里是一個(gè)更詳細(xì)的例子,展示了如何使用reserve來預(yù)分配vector的存儲(chǔ)空間:
#include <iostream>
#include <vector> int main() { std::vector<int> v; // 初始時(shí),size和capacity都是0 std::cout << "Initial size: " << v.size() << ", Initial capacity: " << v.capacity() << std::endl; // 使用reserve預(yù)分配至少10個(gè)元素的存儲(chǔ)空間 v.reserve(10); // size仍然是0,因?yàn)槲覀儧]有添加任何元素 // 但capacity至少為10 std::cout << "After reserve, size: " << v.size() << ", Capacity: " << v.capacity() << std::endl; // 添加元素,直到達(dá)到或超過預(yù)留的capacity for (int i = 0; i < 15; ++i) { v.push_back(i); } // size現(xiàn)在是15,因?yàn)槲覀兲砑恿?5個(gè)元素 // capacity可能已經(jīng)增長,以容納更多的元素 std::cout << "After adding elements, size: " << v.size() << ", Capacity: " << v.capacity() << std::endl; return 0;
}
輸出可能類似于:
Initial size: 0, Initial capacity: 0
After reserve, size: 0, Capacity: 10
After adding elements, size: 15, Capacity: 20 (或更大,取決于實(shí)現(xiàn))
capacity的確切值可能因不同的vector實(shí)現(xiàn)而異。在上面的例子中,當(dāng)添加超過預(yù)留capacity的元素時(shí),vector可能會(huì)再次分配內(nèi)存,并可能增加其capacity。這樣做是為了避免在每次添加元素時(shí)都重新分配內(nèi)存,從而提高性能。在實(shí)踐中,如果你知道將要向vector中添加大量元素,使用reserve來預(yù)分配足夠的空間通常是一個(gè)好習(xí)慣,因?yàn)檫@可以避免不必要的內(nèi)存分配和元素復(fù)制。