最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

C++ STL容器和算法:詳解和實例演示

2023-04-29 19:44 作者:小萬哥丶  | 我要投稿

C++ STL(標準模板庫)提供了一組豐富的容器和算法,使得開發(fā)者能夠更加高效地編寫程序。本文將介紹STL中的一些常用容器和算法。

容器

vector

vector是一個動態(tài)數(shù)組,可以在運行時調(diào)整大小。它的優(yōu)點在于可以快速地訪問元素,缺點是在插入和刪除元素時需要移動后面的元素。

#include <vector>
#include <iostream>

using namespace std;

int main() {
? vector<int> v;
? v.push_back(1);
? v.push_back(2);
? v.push_back(3);

? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;

? v.pop_back();

? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;

? return 0;
}

除了push_backpop_back,vector還提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用v.front()v.back()分別訪問首元素和尾元素,使用v.insert()v.erase()在任意位置插入和刪除元素。此外,vector還提供了v.empty()v.size()分別判斷容器是否為空和獲取容器大小。

list

list是一個雙向鏈表,可以在任意位置插入和刪除元素,但訪問元素比較慢。

#include <list>
#include <iostream>

using namespace std;

int main() {
? list<int> l;
? l.push_back(1);
? l.push_back(2);
? l.push_back(3);

? for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
? ? ? cout << *it << " ";
? }
? cout << endl;

? l.pop_back();

? for (list<int>::iterator it = l.begin(); it != l.end(); it++) {
? ? ? cout << *it << " ";
? }
? cout << endl;

? return 0;
}

vector一樣,list也提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用l.front()l.back()分別訪問首元素和尾元素,使用l.insert()l.erase()在任意位置插入和刪除元素。此外,list還提供了l.empty()l.size()分別判斷容器是否為空和獲取容器大小。

map

map是一個鍵值對容器,可以快速地根據(jù)鍵值查找對應(yīng)的值。

#include <map>
#include <iostream>

using namespace std;

int main() {
? map<string, int> m;
? m["a"] = 1;
? m["b"] = 2;
? m["c"] = 3;

? for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
? ? ? cout << it->first << ":" << it->second << " ";
? }
? cout << endl;

? m.erase("b");

? for (map<string, int>::iterator it = m.begin(); it != m.end(); it++) {
? ? ? cout << it->first << ":" << it->second << " ";
? }
? cout << endl;

? return 0;
}

vector、list一樣,map也提供了很多其他的成員函數(shù)和迭代器,可以方便地訪問和修改元素。比如,可以使用m.find()查找元素,使用m.insert()插入元素,使用m.erase()刪除元素。此外,map還提供了m.empty()m.size()分別判斷容器是否為空和獲取容器大小。

算法

除了容器,STL還提供了一些常用的算法,可以方便地操作容器中的元素。

sort

sort是一個排序算法,可以快速地將數(shù)組或容器中的元素按照指定規(guī)則排序。

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int main() {
? vector<int> v;
? v.push_back(3);
? v.push_back(2);
? v.push_back(1);

? sort(v.begin(), v.end());

? for (int i = 0; i < v.size(); i++) {
? ? ? cout << v[i] << " ";
? }
? cout << endl;

? return 0;
}

sort的第一個參數(shù)是要排序的容器的起始地址,第二個參數(shù)是要排序的容器的結(jié)束地址。這里使用了vectorbegin()end()函數(shù)獲取迭代器,也可以使用數(shù)組名和數(shù)組長度作為參數(shù)。

sort默認是升序排序,可以通過第三個參數(shù)指定排序規(guī)則。比如,可以使用greater<int>()降序排序,使用less<int>()升序排序。

find

find是一個查找算法,可以快速地在數(shù)組或容器中查找指定元素。

#include <algorithm>
#include <vector>
#include <iostream>

using namespace std;

int main() {
? vector<int> v;
? v.push_back(1);
? v.push_back(2);
? v.push_back(3);

? vector<int>::iterator it = find(v.begin(), v.end(), 2);

? if (it != v.end()) {
? ? ? cout << "found " << *it << endl;
? } else {
? ? ? cout << "not found" << endl;
? }

? return 0;
}

find的第一個參數(shù)是要查找的容器的起始地址,第二個參數(shù)是要查找的容器的結(jié)束地址,第三個參數(shù)是要查找的元素。find返回一個迭代器,指向第一個等于要查找元素的位置,如果沒有找到則返回容器的結(jié)束地址。

除了find,STL還提供了很多其他的查找算法,比如find_if可以根據(jù)指定規(guī)則查找元素,binary_search可以判斷容器中是否含有指定元素,lower_boundupper_bound可以查找元素的下界和上界。

結(jié)論

本文介紹了C++ STL中的一些常用容器和算法,它們可以大大提高開發(fā)效率,開發(fā)者應(yīng)該熟練掌握它們的使用。除了本文介紹的容器和算法,STL還提供了很多其他的容器和算法,可以根據(jù)具體的需求選擇使用。在使用STL時,要注意容器和算法的復雜度,避免出現(xiàn)性能問題。


C++ STL容器和算法:詳解和實例演示的評論 (共 條)

分享到微博請遵守國家法律
霍城县| 杂多县| 林甸县| 武强县| 兴文县| 安阳县| 岑巩县| 印江| 雅江县| 中超| 温泉县| 舒兰市| 苍南县| 怀宁县| 韶关市| 瑞安市| 滨州市| 舞阳县| 乌拉特前旗| 岳西县| 木里| 紫云| 瓮安县| 两当县| 温宿县| 拉萨市| 济南市| 左云县| 临颍县| 乌审旗| 德清县| 沐川县| 彭山县| 江永县| 望城县| 花垣县| 卢氏县| 嘉荫县| 红安县| 淮阳县| 阿巴嘎旗|