#include #include using namespace std;

int main() { deque d1 = { 'aa', 'bb' }; // 定义deque容器对象d1,实例化为string类型 deque d2(d1); // 定义deque容器对象d2,用d1初始化d2 d2.push_front('/'); // 在队列d2的头部压入数据 d2.push_back('/'); // 在队列d2的尾部压入数据 cout << "Contents of deque are" << endl; deque::iterator it; // 创建队列对象迭代器 for (it = d2.begin(); it != d2.end(); it++) // 利用迭代器依次访问元素 { cout << *it; } return 0; }

该程序运用了deque容器、迭代器的知识点。在编程时需要注意正确使用容器的各种操作,如push_front、push_back等,以及正确使用迭代器访问容器中的元素。同时需要注意类型匹配问题,如实例化deque容器为string类型,需要确保push_front和push_back的数据也是string类型。

C++ deque容器和迭代器使用示例

原文地址: https://www.cveoy.top/t/topic/ok5M 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录