假期在家里闲着无聊,今天复习了一下queue的用法,其实和昨天做的vector的用法是差不多的,不过实在无事可做。写写简单的代码也可以让我至少觉着充实一点。
#include <deque> #include <iostream> #include <iterator> using namespace std; int main() { string ary[] = {"Alex" ,"Mary" ,"TianBei"}; //define a queue deque<string> q; q.assign(ary ,ary + 3); deque<string>::iterator It = q.begin(); while(It != q.end()) { cout<<*It++<<" "; } cout<<endl; cout<<"==========================="<<endl; //Modify the element. q.at(0) = "ChenZixin"; q.back() = "HuaShuo"; //delete two elements ahead q.erase(q.begin() ,q.begin() + 2); copy(q.begin() ,q.end() ,ostream_iterator<string>(cout ," ")); cout<<endl; cout<<"==========================="<<endl; //delete the specified element q.assign(ary ,ary + 3); It = q.begin(); for( ;It != q.end() { if(*It == "Mary") { It = q.erase(It); } else { It++; } } //delete the second element q.erase(q.begin()+1 ,q.end()); //insert the three elements to the ahead of the queue q.insert(q.begin() ,ary ,ary + 3); copy(q.begin() ,q.end() ,ostream_iterator<string>(cout ," ")); cout<<endl<<"max_size of queue : "<<q.max_size()<<endl; cout<<"==========================="<<endl; It = q.begin(); while(!q.empty()) { copy(q.begin() ,q.end() ,ostream_iterator<string>(cout ," ")); cout<<endl; //delete the last element q.pop_back(); } cout<<"==========================="<<endl; q.assign(ary , ary + 3); It = q.begin(); while(!q.empty()) { copy(q.begin() ,q.end() ,ostream_iterator<string>(cout ," ")); cout<<endl; //delete the last element q.pop_front(); } cout<<"==========================="<<endl; q.clear(); q.push_front("china is a great country"); q.push_front("she is a beautiful girl"); cout<<q.front()<<endl; cout<<"==========================="<<endl; q.clear(); q.push_back("china is a great country"); q.push_back("she is a beautiful girl"); cout<<q.front()<<endl; cout<<"==========================="<<endl; q.resize(7,"I love you"); copy(q.begin() ,q.end() ,ostream_iterator<string>(cout ,"\n")); cout<<"==========================="<<endl; cout<<"queue size :"<<q.size()<<endl; return 0 ; }