c++ vector 常用方法


 

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

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

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

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

 

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

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

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 
 5 int main()
 6 {
 7     std::vector<int> v = { 4, 7, 5, 2, 6, 9 };
 8     int key = 6;
 9 
10     if (std::count(v.begin(), v.end(), key))
11         std::cout << "Element found";
12     else
13         std::cout << "Element not found";
14 
15     return 0;
16 }

 結果顯示:Element found

 

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

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

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

 結果: 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

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

結果顯示: 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

 1 vector<int> row_y = { 502, 263, 684, 324, 979 };  2 
 3 // 最大值索引和最大值
 4 int row_y_max_index = max_element(row_y.begin(), row_y.end()) - row_y.begin();  5 cout << "row_y_max_index = " << row_y_max_index << endl;  6 int row_y_max_value = *max_element(row_y.begin(), row_y.end());  7 cout << "row_y_max_value = " << row_y_max_value << endl;  8 
 9 // 最小值索引和最小值
10 int row_y_min_index = min_element(row_y.begin(), row_y.end()) - row_y.begin(); 11 cout << "row_y_min_index = " << row_y_min_index << endl; 12 int row_y_min_value = *min_element(row_y.begin(), row_y.end()); 13 cout << "row_y_min_value = " << row_y_min_value << endl;

 結果返回:

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

 

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

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

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

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

 

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

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

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

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

 

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

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

若想要刪除第2個索引值和到第5個索引值,則可以使用下以語句: 

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

結果: 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/

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

結果: x = {0  150  220  80}

 

10. c++ 統計 vector 某個元素出現的次數  (C++ count the number of occurrences of an element in vector)

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

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

結果:n = 3

備注:如果想要把出現的數字和次數轉換成map形式(如下圖所示),可以看前本博客的幾篇文章:c++ 統計 vector 每個元素出現的次數  (C++ count the number of occurrences of each element in vector)(2020.3.13)

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM