//插入元素//#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; d.push_back(1);//尾部插入 d.push_back(2); d.push_back(3); d.push_front(10);//d.insert(d.begin()+1, 10);头部插入 d.push_front(20);//d.insert(d.begin()+2, 20); cout<<d[0]<<" "<<d[1]<<" "<<d[2]<<endl; return 0; }
//遍历//#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; d.push_back(1); d.push_back(2); d.push_back(3); for(int i = 0; i < d.size(); i ++) cout<<d[i]<<" "; cout<<endl; deque<int>::iterator it; for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; deque<int>::reverse_iterator rit; for(rit = d.rbegin(); rit != d.rend(); rit ++) cout<<*rit<<" ";//反向遍历 cout<<endl; return 0; }
//删除//#include<iostream> #include<deque> using namespace std; int main() { deque<int> d; for(int i = 1; i < 6; i ++) d.push_back(i); d.pop_front(); d.pop_front(); deque<int>::iterator it; for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.pop_back(); for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.erase(d.begin()+1); for(it = d.begin(); it != d.end(); it ++) cout<<*it<<" "; cout<<endl; d.clear(); cout<<d.size()<<endl; return 0; }
时间: 2024-10-08 10:16:12