最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過代碼實(shí)例解析c++ vector常用方法

 更新時(shí)間:2020年07月24日 09:14:57   作者:ttweixiao9999  
這篇文章主要介紹了通過代碼實(shí)例解析c++ vector常用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1. c++ vector 每個(gè)元素加上一個(gè)特定值 (c++ vector add a constant value for each element)

https://stackoverflow.com/questions/4461446/stl-way-to-add-a-constant-value-to-a-stdvector

vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
//transform可以將函數(shù)應(yīng)用到序列的元素上,bind2nd通過綁定其中一個(gè)參數(shù)把二元函數(shù)轉(zhuǎn)換成一元函數(shù)
transform(x.begin(), x.end(), x.begin(), bind2nd(plus<int>(), 1));
//顯示x的值
copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));

結(jié)果: x = {1 31 81 101 121 151 191 221 251}

2. c++判斷vector中是否存在某個(gè)元素(c++ judge whether an element exists in the vector)

https://www.techiedelight.com/check-vector-contains-given-element-cpp/

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

int main()
{
  std::vector<int> v = { 4, 7, 5, 2, 6, 9 };
  int key = 6;

  if (std::count(v.begin(), v.end(), key))
    std::cout << "Element found";
  else
    std::cout << "Element not found";

  return 0;
}

結(jié)果顯示:Element found

3. c++ vector<int> 生成指定個(gè)數(shù)的順序列表 (c++ generate a sequential vector<int> of special numbers)

https://stackoverflow.com/questions/17694579/use-stdfill-to-populate-vector-with-increasing-numbers

 std::vector<int> seq(10);
 // 定義在 numeric 頭文件中的 iota() 函數(shù)模板會用連續(xù)的 T 類型值填充序列
 std::iota(seq.begin(), seq.end(), 0);

結(jié)果: seq = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

4. c++ 一條語句打印vector信息(c++ print out vector by one statement).

https://stackoverflow.com/questions/10750057/how-to-print-out-the-contents-of-a-vector

 vector<int> x = {1, 2, 3, 4};
 //istream_iterator用于從輸入流中讀取連續(xù)的元素
 copy(x.begin(), x.end(), ostream_iterator<int>(cout, " "));

結(jié)果顯示: 1 2 3 4

5. c++ 得到vector<int>中元素的最大值和最小值以及最大值和最小值的索引位置 (c++ get the maximum and minimum values of the elements in vector<int> and the index positions )

https://riptutorial.com/cplusplus/example/11151/find-max-and-min-element-and-respective-index-in-a-vector

vector<int> row_y = { 502, 263, 684, 324, 979 };

// 最大值索引和最大值
int row_y_max_index = max_element(row_y.begin(), row_y.end()) - row_y.begin();
cout << "row_y_max_index = " << row_y_max_index << endl;
int row_y_max_value = *max_element(row_y.begin(), row_y.end());
cout << "row_y_max_value = " << row_y_max_value << endl;

// 最小值索引和最小值
int row_y_min_index = min_element(row_y.begin(), row_y.end()) - row_y.begin();
cout << "row_y_min_index = " << row_y_min_index << endl;
int row_y_min_value = *min_element(row_y.begin(), row_y.end());
cout << "row_y_min_value = " << row_y_min_value << endl;

結(jié)果返回:

row_y_max_index = 4
row_y_max_value = 979
row_y_min_index = 1
row_y_min_value = 263

6. c++ vector 相加兩個(gè)vector (c++ append a vector to vector)

https://stackoverflow.com/questions/2551775/appending-a-vector-to-a-vector

 vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
 vector<int> y = {100};
 y.insert(y.end(), x.begin(), x.end());

結(jié)果:y = {100, 0, 30, 80, 100, 120, 150, 190, 220, 250}

7. c++ 復(fù)制vector(c++ copy vector)

https://www.geeksforgeeks.org/ways-copy-vector-c/

vector<int> x = {0, 30, 80, 100, 120, 150, 190, 220, 250};
 vector<int> y;
 y.assign(x.begin(), x.end());

結(jié)果:y = {0, 30, 80, 100, 120, 150, 190, 220, 250}

8. c++ vector 根據(jù)給定索引刪除元素(c++ vector delete element based on a given index)

https://iq.opengenus.org/ways-to-remove-elements-from-vector-cpp/

若想要?jiǎng)h除第2個(gè)索引值和到第5個(gè)索引值,則可以使用下以語句:

 vector<int> x = {0, 30, 80, 150, 120, 150, 30, 220, 80};
 //remove(x.begin(), x.end(), 80);
 x.erase(x.begin() + 2, x.begin() + 5 + 1);

結(jié)果: x = {0, 30, 30, 220, 80}

9. c++ 刪除vector所有指定元素(c++ delete all specified elements in the vector)

https://www.techiedelight.com/erase-elements-vector-cpp/

 vector<int> x = {0, 30, 150, 30, 220, 80};
 //vector中的remove的作用是將等于value的元素放到vector的尾部,但并不減少vector的size
 //vector中erase的作用是刪除掉某個(gè)位置position或一段區(qū)域(begin, end)中的元素,減少其size
 x.erase(remove(x.begin(), x.end(), 30), x.end());

結(jié)果: x = {0 150 220 80}

10. c++ 統(tǒng)計(jì) vector 某個(gè)元素出現(xiàn)的次數(shù) (C++ count the number of occurrences of an element in vector)

https://www.geeksforgeeks.org/std-count-cpp-stl/

 vector<int> x = { 0, 3, 5, 6, 3, 2, 3 };
 int n = count(x.begin(), x.end(), 3);

結(jié)果:n = 3

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言數(shù)組元素的循環(huán)移位方法

    C語言數(shù)組元素的循環(huán)移位方法

    今天小編就為大家分享一篇C語言數(shù)組元素的循環(huán)移位方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 一文讓你不再害怕指針之C指針詳解(經(jīng)典,非常詳細(xì))

    一文讓你不再害怕指針之C指針詳解(經(jīng)典,非常詳細(xì))

    這篇文章主要給大家介紹了C指針的相關(guān)資料,文中介紹的很經(jīng)典,非常詳細(xì),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C指針具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • C/C++實(shí)現(xiàn)發(fā)送與接收HTTP/S請求的示例代碼

    C/C++實(shí)現(xiàn)發(fā)送與接收HTTP/S請求的示例代碼

    HTTP(Hypertext Transfer Protocol)是一種用于傳輸超文本的協(xié)議,它是一種無狀態(tài)的、應(yīng)用層的協(xié)議,用于在計(jì)算機(jī)之間傳輸超文本文檔,通常在 Web 瀏覽器和 Web 服務(wù)器之間進(jìn)行數(shù)據(jù)通信,本文給大家介紹了C/C++發(fā)送與接收HTTP/S請求,需要的朋友可以參考下
    2023-11-11
  • Qt實(shí)現(xiàn)編輯框失去焦點(diǎn)隱藏功能

    Qt實(shí)現(xiàn)編輯框失去焦點(diǎn)隱藏功能

    這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)的一個(gè)簡單的編輯框操作——主窗口失去焦點(diǎn)隱藏功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-10-10
  • C++中的throw關(guān)鍵字詳解

    C++中的throw關(guān)鍵字詳解

    throw關(guān)鍵字是在C語言中用來拋出異常的關(guān)鍵字,它通常與try和catch一起使用,用于在程序中發(fā)生錯(cuò)誤時(shí)進(jìn)行異常處理,當(dāng)遇到無法處理的錯(cuò)誤情況時(shí),我們可以使用throw關(guān)鍵字主動拋出異常,所以本文給大家詳細(xì)的介紹一下C++中的throw關(guān)鍵字,需要的朋友可以參考下
    2023-09-09
  • C++基礎(chǔ)知識實(shí)例解析(一)

    C++基礎(chǔ)知識實(shí)例解析(一)

    這篇文章主要對C++基礎(chǔ)知識實(shí)例解析,通過四個(gè)簡短的案例,鞏固大家的基礎(chǔ)知識,需要的朋友可以參考下
    2015-08-08
  • C++中簡單的文本文件輸入/輸出示例詳解

    C++中簡單的文本文件輸入/輸出示例詳解

    C++程序把輸入和輸出看作字節(jié)流,輸入時(shí)程序從輸入流中抽取字節(jié),輸出時(shí)程序?qū)⒆止?jié)插入到輸出流中,下面這篇文章主要給大家介紹了關(guān)于C++中簡單的文本文件輸入/輸出的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • VisualStudio2019配置OpenCV4.5.0的方法示例

    VisualStudio2019配置OpenCV4.5.0的方法示例

    這篇文章主要介紹了VisualStudio2019配置OpenCV4.5.0的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C++實(shí)現(xiàn)簡單掃雷游戲

    C++實(shí)現(xiàn)簡單掃雷游戲

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 使用VSCode和VS2017編譯調(diào)試STM32程序的實(shí)現(xiàn)

    使用VSCode和VS2017編譯調(diào)試STM32程序的實(shí)現(xiàn)

    這篇文章主要介紹了使用VSCode和VS2017編譯調(diào)試STM32程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評論

北安市| 吴江市| 枞阳县| 揭阳市| 彭州市| 商丘市| 晋江市| 收藏| 图们市| 临颍县| 方山县| 遵化市| 内丘县| 云安县| 武川县| 黔西县| 南丰县| 类乌齐县| 德江县| 大城县| 德庆县| 闸北区| 大竹县| 磐安县| 柯坪县| 苏州市| 江油市| 石林| 九江县| 海原县| 乌拉特后旗| 靖宇县| 会东县| 遵化市| 启东市| 桂林市| 马龙县| 巴马| 漠河县| 泰顺县| 惠东县|