Stack, deque, and queue comparison

Source: Internet
Author: User

Stack stack, no iterator, supports the push () method. After going forward, go first, top () returns the top element, and pop () removes the top element.

Deque dual-end queue, supports iterator, has push_back () method, and is similar to vector, has more pop_front than vector, push_front Method

Queue queue, first-in-first-out, does not support iterators, there is a push () method, pop () removes the first element, Front () returns the first element

The Code is as follows:

# Include <iostream>
# Include <stack>
# Include <string>
Using namespace STD;
Int main (){
Stack <int> S;
For (INT I = 1; I <= 10; ++ I ){
S. Push (I );
}
For (Int J = 0; j <10; ++ J ){
Cout <S. Top () <"";
S. Pop ();
}
Cout <Endl;
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <string>
# Include <deque>
Using namespace STD;
Int main (){
Deque <int> q;
For (INT I = 0; I <10; ++ I ){
Q. push_back (I );
}
Cout <q. Front () <Endl;
For (deque <int >:: iterator iter = Q. Begin (); iter! = Q. End (); ++ ITER ){
Cout <* ITER <"";
}
Cout <Endl;
Cout <q. Back () <Endl;
System ("pause ");
Return 0;
}

# Include <iostream>
# Include <queue>
# Include <string>
Using namespace STD;
Int main (){
Queue <int> q;
For (INT I = 0; I <10; ++ I ){
Q. Push (I );
}
For (INT I = 0; I <10; ++ I ){
Cout <q. Front () <"";
Q. Pop ();
}
Cout <Endl;
System ("pause ");
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.