您好,登錄后才能下訂單哦!
C++ 標(biāo)準(zhǔn)模板庫(kù)(STL)提供了一系列高效的數(shù)據(jù)結(jié)構(gòu)和算法,使得開發(fā)人員能夠更輕松地處理數(shù)據(jù)
#include<iostream>
#include<vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
for (const auto &element : vec) {
std::cout<< element << " ";
}
return 0;
}
#include<iostream>
#include<vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
#include<iostream>
#include<vector>
void print(const std::vector<int> &vec) {
for (auto it = vec.cbegin(); it != vec.cend(); ++it) {
std::cout << *it << " ";
}
}
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
print(vec);
return 0;
}
#include<iostream>
#include<vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
std::cout << *it << " ";
}
return 0;
}
#include<iostream>
#include<vector>
void print_reversed(const std::vector<int> &vec) {
for (auto it = vec.crbegin(); it != vec.crend(); ++it) {
std::cout << *it << " ";
}
}
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
print_reversed(vec);
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
void print_element(int element) {
std::cout<< element << " ";
}
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::for_each(vec.begin(), vec.end(), print_element);
return 0;
}
這些技巧可以幫助你在 C++ STL 中更有效地遍歷和迭代容器。請(qǐng)根據(jù)實(shí)際需求選擇合適的方法。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。