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中进行查找元素,具体选择哪种方法取决于个人偏好和具体的应用场景。