一、遍歷vector容器
vector是C++語言中的一個容器,用於存儲一系列相同類型的元素。遍歷vector是使用vector的一個基本操作。遍歷vector最常見的方式是使用迭代器,可以通過begin()和end()函數獲取vector的起始位置和結束位置。下面是一個遍歷vector容器的代碼示例:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; for (auto it = v.begin(); it != v.end(); it++) { cout << *it << " "; } return 0; }
以上代碼中,我們使用了auto關鍵字來定義迭代器,用於遍歷vector容器。通過循環,我們輸出了每個元素的值,輸出結果為:1 2 3 4 5。
二、便利vector
除了使用迭代器來遍歷vector之外,還可以使用for-each語句來遍歷vector。for-each語句可以簡化代碼,並且更易讀。下面是一個使用for-each語句遍歷vector的代碼示例:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; for (auto i : v) { cout << i << " "; } return 0; }
以上代碼中,我們使用for-each語句來便利vector容器,輸出結果與前一個示例相同。
三、遍歷vector中的元素
遍歷vector中的元素是在遍歷vector的基礎上,進一步操作vector中的每個元素。對於vector中的元素,在遍歷的過程中可以使用下標或迭代器來進行操作。下面是一個使用下標遍歷vector並修改元素值的代碼示例:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; for (unsigned int i = 0; i < v.size(); i++) { v[i] *= 2; } for (auto i : v) { cout << i << " "; } return 0; }
以上代碼中,我們使用下標來遍歷vector容器,並將每個元素的值乘以2。通過第二個for-each循環,我們輸出了修改過數值的vector容器,輸出結果為:2 4 6 8 10。
四、遍歷vector刪除符合條件的元素
在遍歷vector容器過程中,如果需要刪除符合條件的元素,需要注意迭代器的失效問題。刪除元素的時候應該使用erase()函數,同時使用正確的迭代器指向,來防止刪除後迭代器失效。下面是一個遍歷vector並刪除其中的偶數元素的代碼示例:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; for (auto it = v.begin(); it != v.end(); ) { if (*it % 2 == 0) { it = v.erase(it); } else { it++; } } for (auto i : v) { cout << i << " "; } return 0; }
以上代碼中,我們使用迭代器遍歷vector容器,並刪除其中的偶數元素。使用erase()函數刪除元素後,迭代器應該指向erase()函數返回的新迭代器,以防止迭代器失效。輸出結果為:1 3 5。
五、vector遍歷
遍歷vector除了使用for循環和for-each語句之外,還可以使用std::for_each()演算法進行遍歷。與for循環和for-each語句不同,for_each()演算法需要傳入一個函數對象用於處理每個元素。下面是一個使用for_each()演算法遍歷vector並進行求和的代碼示例:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; int sum = 0; std::for_each(v.begin(), v.end(), [&](int i) { sum += i; }); cout << "Sum: " << sum << endl; return 0; }
以上代碼中,我們使用for_each()演算法遍歷vector容器,並使用lambda表達式進行求和操作。輸出結果為:Sum: 15。
六、vector遍歷刪除
除了在遍歷容器中刪除元素之外,還可以使用std::remove_if()演算法進行刪除操作。std::remove_if()演算法可以刪除符合特定條件的元素,並將剩餘的元素移到vector的前面。下面是一個使用std::remove_if()演算法遍歷vector並刪除其中偶數元素的代碼示例:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; v.erase(std::remove_if(v.begin(), v.end(), [](int i) { return i % 2 == 0; }), v.end()); for (auto i : v) { cout << i << " "; } return 0; }
以上代碼中,我們使用std::remove_if()演算法遍歷vector容器,刪除其中的偶數元素。最後我們使用遍歷容器的方式輸出vector容器的內容,輸出結果為:1 3 5。
七、vector大小
在遍歷vector容器的過程中,需要知道vector容器中的元素數量,可以使用size()函數獲取vector容器中元素的個數。下面是一個輸出vector容器大小的代碼示例:
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3, 4, 5}; cout << "Vector size: " << v.size() << endl; return 0; }
以上代碼中,我們輸出了vector容器中元素的個數,輸出結果為:Vector size: 5。
八、遍歷vector里的對象
在實際編程中,vector容器並不僅僅只存儲基本數據類型,還可能存儲結構體、類等自定義對象。遍歷vector中的對象與遍歷基本數據類型並沒有太大區別,具體使用方式與前面的示例基本相同。下面是一個存儲自定義結構體的vector容器,並遍歷輸出每個元素的成員變數值的代碼示例:
#include <iostream> #include <vector> #include <string> using namespace std; struct Person { string name; int age; }; int main() { vector<Person> v { {"Alice", 20}, {"Bob", 25}, {"Charlie", 30}, {"David", 35}, {"Ella", 40} }; for (auto it = v.begin(); it != v.end(); it++) { cout << "Name: " << it->name << ", Age: " << it->age << endl; } return 0; }
以上代碼中,我們定義了一個Person結構體,包含name和age兩個成員變數。我們將多個Person對象存儲在vector容器中,並使用迭代器遍歷vector容器,輸出每個元素的成員變數值。
原創文章,作者:ZBTZ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/136991.html