av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C++進(jìn)階教程:C++標(biāo)準(zhǔn)模板庫(kù)初學(xué)者指南

C++ 是初學(xué)者可能遇到的最強(qiáng)大、最令人生畏的編程語言之一。原因很簡(jiǎn)單。它需要大量代碼來實(shí)現(xiàn)所需的輸出。標(biāo)準(zhǔn)模板庫(kù)或 STL 可以幫助您解決這個(gè)難題。

考慮到為排序和搜索等功能編寫代碼所消耗的時(shí)間和精力,STL 可以幫助您只用一行代碼執(zhí)行所有這些操作。這個(gè)庫(kù)對(duì)于解決問題和準(zhǔn)備技術(shù)面試非常有用。

什么是標(biāo)準(zhǔn)模板庫(kù)?

標(biāo)準(zhǔn)模板庫(kù)或 STL 是一個(gè) C++ 庫(kù),由預(yù)構(gòu)建的函數(shù)和容器組成。它包括一些用于常見數(shù)據(jù)結(jié)構(gòu)(如向量、堆棧、隊(duì)列)的突出模板類,以及一些方便的算法函數(shù)(如二進(jìn)制搜索),以使編程更容易。

C++ 中的標(biāo)準(zhǔn)模板庫(kù)由四個(gè)組件組成:

  1.  算法
  2.  容器
  3.   功能
  4.  迭代器

讓我們更深入地了解一下算法和容器,因?yàn)樗鼈兪?STL 中最常用的組件。

STL 中的算法

頭文件是 STL的一部分,它由幾個(gè)算法函數(shù)組成,可以使用這些算法函數(shù)來代替手動(dòng)編碼它們。包括的一些算法是二進(jìn)制搜索、排序和反向,它們非常有用。

首先,您需要在 C++ 文件中導(dǎo)入標(biāo)頭。語法如下:

#include 

對(duì)于即將出現(xiàn)的方法,以具有 {6, 2, 9, 1, 4} 值的數(shù)組變量為例。

int arr[] = {6, 2, 9, 1, 4};

sort()

sort()函數(shù)可幫助您按升序?qū)χ付〝?shù)據(jù)結(jié)構(gòu)內(nèi)的所有元素進(jìn)行排序。這個(gè)函數(shù)有兩個(gè)參數(shù):開始迭代器和結(jié)束迭代器。

語法:

sort(start_iterator, end_iterator);

這是一個(gè)簡(jiǎn)單的例子:

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ù)結(jié)構(gòu)中元素的 順序。它接受兩個(gè)參數(shù):開始迭代器和結(jié)束迭代器。

語法:

reverse(start_iterator, end_iterator);

這是上述方法的一個(gè)簡(jiǎn)短示例:

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ù)結(jié)構(gòu)內(nèi)的最大值和最小值。這兩個(gè)函數(shù)都接受兩個(gè)參數(shù):開始迭代器和結(jié)束迭代器。

語法:

*max_element(start_iterator, end_iterator);
*min_element(start_iterator, end_iterator);

讓我們找出這些函數(shù)在示例數(shù)組上調(diào)用它們時(shí)返回的值:

cout << *max_element(arr, arr+5) << endl;
cout << *min_element(arr, arr+5) << endl;

輸出:

9
1

binary_search()

binary_search ()方法用于查找指定值是否存在于數(shù)據(jù)結(jié)構(gòu)中。它接受三個(gè)參數(shù):開始迭代器、結(jié)束迭代器和要查找的值。

二進(jìn)制搜索僅適用于已排序的數(shù)據(jù)結(jié)構(gòu)。因此,您需要先調(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()方法返回?cái)?shù)據(jù)結(jié)構(gòu)中指定值的出現(xiàn)次數(shù)。它接受三個(gè)參數(shù):開始迭代器、結(jié)束迭代器和要計(jì)數(shù)的值。

語法:

count(start_iterator, end_iterator, value_to_count);

這是此方法的示例:

cout << count(arr, arr+5, 2) << endl;

輸出:

1

STL 中的容器

容器是存儲(chǔ)對(duì)象和數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)。向量、列表、堆棧、隊(duì)列、集合和映射是根據(jù)指定的原始數(shù)據(jù)類型在其中存儲(chǔ)數(shù)據(jù)的一些示例。您可以通過在 C++ 文件中導(dǎo)入它們各自的標(biāo)頭來使用這些容器。

在初始化容器變量時(shí),您需要在 <>括號(hào)內(nèi)提及原始數(shù)據(jù),例如 int、  char、  string 。

讓我們更詳細(xì)地探索其中一些容器:

向量 Vector

向量是可調(diào)整大小且使用靈活的動(dòng)態(tài)數(shù)組。當(dāng)您從向量中插入或刪除元素時(shí),它會(huì)自動(dòng)調(diào)整向量的大小。這類似于 Java 中的ArrayList 數(shù)據(jù)結(jié)構(gòu)。

句法:

#include

vector vaiable_name;

以下是一些重要的向量方法:

  1.  push_back(value):此方法將數(shù)據(jù)附加到向量。
  2.  pop_back():此方法從向量中刪除最后一個(gè)元素。
  3.  insert(index, value):此方法在指定位置的元素之前插入新元素。
  4.  size():此方法返回向量的大小。
  5.  empty():此方法檢查向量是否為空。
  6.  front():此方法返回向量的第一個(gè)值。
  7.  back() : back 方法返回向量的最后一個(gè)值。
  8.  at(index):該方法返回指定位置的值。
  9.  erase(index):擦除方法從給定索引中刪除元素。
  10.  clear():此方法清除向量中的所有項(xiàng)目。
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

隊(duì)列 Queue

在隊(duì)列數(shù)據(jù)結(jié)構(gòu)中,元素從后面插入,從前面刪除。因此,它遵循 FIFO(“先進(jìn)先出”)方法。

句法:

#include 
queue variable_name;

以下是一些重要的隊(duì)列方法:

  1.  push(value ):此方法將元素添加到隊(duì)列中。
  2.  pop():此方法刪除隊(duì)列的第一個(gè)元素。
  3.  size():此方法返回隊(duì)列的大小。
  4.  front():此方法返回隊(duì)列的第一個(gè)元素。
  5.  back():此方法返回隊(duì)列的最后一個(gè)元素。
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 方法上運(yùn)行。LIFO 代表“后進(jìn)先出”。數(shù)據(jù)從同一端推送和彈出。

語法:

#include 
stack variable_name;

以下是一些重要的堆棧方法:

  1.  push(value ):此方法將元素壓入堆棧。
  2.  pop():此方法刪除堆棧的頂部元素。
  3.  top():此方法返回棧中最后一個(gè)元素的值。
  4.  size():此方法返回堆棧的大小。
  5.  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

集合容器用于保存唯一值,元素的值一旦插入集合就不能更改。集合中的所有元素都以排序方式存儲(chǔ)。set 容器類似于Python 中的 set 數(shù)據(jù)結(jié)構(gòu)

句法:

#include

set variable_name;

以下是一些重要的設(shè)置方法:

  1.  insert(value):此方法在集合中插入元素。
  2.  begin():此方法將迭代器返回到集合的第一個(gè)元素。
  3.  end():此方法將迭代器返回到集合的最后一個(gè)元素。
  4.  size():此方法返回集合的大小。
  5.  empty():此方法檢查集合是否為空。
  6.  find(value):此方法將迭代器返回到參數(shù)中傳遞的元素。如果未找到該元素,則此函數(shù)將迭代器返回到集合的末尾。
  7.  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++ 不一定很難

就像所有其他技能一樣,練習(xí)對(duì)于充分利用 STL 至關(guān)重要。這些容器和算法可以幫助您節(jié)省大量時(shí)間并且易于使用。從練習(xí)上面顯示的示例開始,您最終也會(huì)開始在自己的項(xiàng)目中使用它。

但是,如果這是您第一次學(xué)習(xí) C++,請(qǐng)先學(xué)習(xí)基礎(chǔ)知識(shí),然后再繼續(xù)了解 STL。


網(wǎng)頁(yè)標(biāo)題:C++進(jìn)階教程:C++標(biāo)準(zhǔn)模板庫(kù)初學(xué)者指南
當(dāng)前網(wǎng)址:http://uogjgqi.cn/article/dhipdpg.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流