C++的deque(雙端隊列)提供了多種查找方法,其中最常用的方法是使用迭代器進行查找,也可以使用成員函數如find()或者使用標準庫算法進行查找。
#include <deque>
#include <iostream>
int main() {
std::deque<int> myDeque = {1, 2, 3, 4, 5};
for (auto it = myDeque.begin(); it != myDeque.end(); ++it) {
if (*it == 3) {
std::cout << "Found element 3 at position " << std::distance(myDeque.begin(), it) << std::endl;
break;
}
}
return 0;
}
#include <deque>
#include <iostream>
int main() {
std::deque<int> myDeque = {1, 2, 3, 4, 5};
auto it = std::find(myDeque.begin(), myDeque.end(), 3);
if (it != myDeque.end()) {
std::cout << "Found element 3 at position " << std::distance(myDeque.begin(), it) << std::endl;
} else {
std::cout << "Element 3 not found in deque" << std::endl;
}
return 0;
}
#include <deque>
#include <algorithm>
#include <iostream>
int main() {
std::deque<int> myDeque = {1, 2, 3, 4, 5};
auto it = std::find(myDeque.begin(), myDeque.end(), 3);
if (it != myDeque.end()) {
std::cout << "Found element 3 at position " << std::distance(myDeque.begin(), it) << std::endl;
} else {
std::cout << "Element 3 not found in deque" << std::endl;
}
return 0;
}
這些方法都可以用來在deque中進行查找元素,具體選擇哪種方法取決于個人偏好和具體的應用場景。