新聞中心
C++ 是初學者可能遇到的最強大、最令人生畏的編程語言之一。原因很簡單。它需要大量代碼來實現(xiàn)所需的輸出。標準模板庫或 STL 可以幫助您解決這個難題。

考慮到為排序和搜索等功能編寫代碼所消耗的時間和精力,STL 可以幫助您只用一行代碼執(zhí)行所有這些操作。這個庫對于解決問題和準備技術面試非常有用。
什么是標準模板庫?
標準模板庫或 STL 是一個 C++ 庫,由預構建的函數(shù)和容器組成。它包括一些用于常見數(shù)據(jù)結構(如向量、堆棧、隊列)的突出模板類,以及一些方便的算法函數(shù)(如二進制搜索),以使編程更容易。
C++ 中的標準模板庫由四個組件組成:
- 算法
- 容器
- 功能
- 迭代器
讓我們更深入地了解一下算法和容器,因為它們是 STL 中最常用的組件。
STL 中的算法
首先,您需要在 C++ 文件中導入
#include
對于即將出現(xiàn)的方法,以具有 {6, 2, 9, 1, 4} 值的數(shù)組變量為例。
int arr[] = {6, 2, 9, 1, 4};
sort()
sort()函數(shù)可幫助您按升序?qū)χ付〝?shù)據(jù)結構內(nèi)的所有元素進行排序。這個函數(shù)有兩個參數(shù):開始迭代器和結束迭代器。
語法:
sort(start_iterator, end_iterator);
這是一個簡單的例子:
sort(arr, arr+5);
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
輸出:
1 2 4 6 9
reverse()
reverse()函數(shù)反轉(zhuǎn)指定數(shù)據(jù)結構中元素的 順序。它接受兩個參數(shù):開始迭代器和結束迭代器。
語法:
reverse(start_iterator, end_iterator);
這是上述方法的一個簡短示例:
reverse(arr, arr+5);
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
輸出:
4 1 9 2 6
*min_element() 和 *max_element()
函數(shù) *max_element() 和*min_element() 分別返回指定數(shù)據(jù)結構內(nèi)的最大值和最小值。這兩個函數(shù)都接受兩個參數(shù):開始迭代器和結束迭代器。
語法:
*max_element(start_iterator, end_iterator);
*min_element(start_iterator, end_iterator);
讓我們找出這些函數(shù)在示例數(shù)組上調(diào)用它們時返回的值:
cout << *max_element(arr, arr+5) << endl;
cout << *min_element(arr, arr+5) << endl;
輸出:
9
1
binary_search()
binary_search ()方法用于查找指定值是否存在于數(shù)據(jù)結構中。它接受三個參數(shù):開始迭代器、結束迭代器和要查找的值。
二進制搜索僅適用于已排序的數(shù)據(jù)結構。因此,您需要先調(diào)用sort()方法,然后再調(diào)用binary_search()方法。
語法:
binary_search(start_iterator, end_iterator, value_to_find)
這是此方法的演示:
sort(arr, arr+5);
binary_search(arr, arr+5, 2) ? cout << "Element found" : cout << "Element not found";
binary_search(arr, arr+5, 7) ? cout << "Element found" : cout << "Element not found";
輸出:
Element found
Element not found
count()
count()方法返回數(shù)據(jù)結構中指定值的出現(xiàn)次數(shù)。它接受三個參數(shù):開始迭代器、結束迭代器和要計數(shù)的值。
語法:
count(start_iterator, end_iterator, value_to_count);
這是此方法的示例:
cout << count(arr, arr+5, 2) << endl;
輸出:
1
STL 中的容器
容器是存儲對象和數(shù)據(jù)的數(shù)據(jù)結構。向量、列表、堆棧、隊列、集合和映射是根據(jù)指定的原始數(shù)據(jù)類型在其中存儲數(shù)據(jù)的一些示例。您可以通過在 C++ 文件中導入它們各自的標頭來使用這些容器。
在初始化容器變量時,您需要在 <>括號內(nèi)提及原始數(shù)據(jù),例如 int、 char、 string 。
讓我們更詳細地探索其中一些容器:
向量 Vector
向量是可調(diào)整大小且使用靈活的動態(tài)數(shù)組。當您從向量中插入或刪除元素時,它會自動調(diào)整向量的大小。這類似于 Java 中的ArrayList 數(shù)據(jù)結構。
句法:
#include
vector
以下是一些重要的向量方法:
- push_back(value):此方法將數(shù)據(jù)附加到向量。
- pop_back():此方法從向量中刪除最后一個元素。
- insert(index, value):此方法在指定位置的元素之前插入新元素。
- size():此方法返回向量的大小。
- empty():此方法檢查向量是否為空。
- front():此方法返回向量的第一個值。
- back() : back 方法返回向量的最后一個值。
- at(index):該方法返回指定位置的值。
- erase(index):擦除方法從給定索引中刪除元素。
- clear():此方法清除向量中的所有項目。
vector < int > v = { 23, 12, 56, 10 };
v.push_back(5);
v.push_back(25);
v.pop_back();
auto i = v.insert(v.begin() + 1, 7);
cout << "The size of the given vector " << v.size() << endl;
if (v.empty()) {
cout << "Vector is empty" << endl;
} else {
cout << "Vector is not empty" << endl;
}
cout << "Element at the first position is " << v.front() << endl;
cout << "Element at the last position is " << v.back() << endl;
cout << "Element at the given position is " << v.at(4) << endl;
v.erase(v.begin() + 1);
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}輸出:
The size of the given vector 6
Vector is not empty
Element at the first position is 23
Element at the last position is 5
Element at the given position is 10
23 12 56 10 5
隊列 Queue
在隊列數(shù)據(jù)結構中,元素從后面插入,從前面刪除。因此,它遵循 FIFO(“先進先出”)方法。
句法:
#include
queuevariable_name;
以下是一些重要的隊列方法:
- push(value ):此方法將元素添加到隊列中。
- pop():此方法刪除隊列的第一個元素。
- size():此方法返回隊列的大小。
- front():此方法返回隊列的第一個元素。
- back():此方法返回隊列的最后一個元素。
queue < int > q;
q.push(30);
q.push(40);
q.push(50);
q.push(60);
q.push(70);
cout << "The first element is " << q.front() << endl;
cout << "The last element is " << q.back() << endl;
cout << "The size of queue is " << q.size() << endl;
q.pop();
cout << "Printing all the elements of the Queue" << endl;
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
輸出:
The first element is 30
The last element is 70
The size of the queue is 5
Printing all the elements of the Queue
40 50 60 70
堆 Stack
堆棧容器在 LIFO 方法上運行。LIFO 代表“后進先出”。數(shù)據(jù)從同一端推送和彈出。
語法:
#include
stackvariable_name;
以下是一些重要的堆棧方法:
- push(value ):此方法將元素壓入堆棧。
- pop():此方法刪除堆棧的頂部元素。
- top():此方法返回棧中最后一個元素的值。
- size():此方法返回堆棧的大小。
- empty():此方法檢查堆棧是否為空。
stack < int > s;
s.push(30);
s.push(40);
s.push(50);
s.push(60);
cout << "The top of the stack contains " << s.top() << endl;
s.pop();
cout << "The top of the stack after performing pop operation: " << s.top() << endl;
cout << "Printing all elements of the stack" << endl;
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
輸出:
The top of the stack contains 60
The top of the stack after performing pop operation: 50
Printing all elements of the stack
50 40 30
集合 Set
集合容器用于保存唯一值,元素的值一旦插入集合就不能更改。集合中的所有元素都以排序方式存儲。set 容器類似于Python 中的 set 數(shù)據(jù)結構
句法:
#include
set
以下是一些重要的設置方法:
- insert(value):此方法在集合中插入元素。
- begin():此方法將迭代器返回到集合的第一個元素。
- end():此方法將迭代器返回到集合的最后一個元素。
- size():此方法返回集合的大小。
- empty():此方法檢查集合是否為空。
- find(value):此方法將迭代器返回到參數(shù)中傳遞的元素。如果未找到該元素,則此函數(shù)將迭代器返回到集合的末尾。
- erase(value):此方法從集合中刪除指定的元素。
set < int > s;
s.insert(20);
s.insert(30);
s.insert(40);
s.insert(50);
s.insert(60);
s.insert(60);
s.insert(60);
auto i = s.begin();
cout << "Element at the first position " << * i << endl;
cout << "The size of the set " << s.size() << endl;
s.find(20) != s.end() ? cout << "Element found" << endl : cout << "Element not found" << endl;
s.erase(30);
cout << "Printing all the elements" << endl;
for (auto i = s.begin(); i != s.end(); i++) {
cout << * i << " ";
}
輸出:
Element at the first position 20
The size of the set 5
Element found
Printing all the elements
20 40 50 60
C++ 不一定很難
就像所有其他技能一樣,練習對于充分利用 STL 至關重要。這些容器和算法可以幫助您節(jié)省大量時間并且易于使用。從練習上面顯示的示例開始,您最終也會開始在自己的項目中使用它。
但是,如果這是您第一次學習 C++,請先學習基礎知識,然后再繼續(xù)了解 STL。
名稱欄目:C++進階教程:C++標準模板庫初學者指南
瀏覽地址:http://m.5511xx.com/article/dhipdpg.html


咨詢
建站咨詢
