All source code in C/ C++ Ask a C/ C++ Pro Discussion Forum Categories All jobs in C/ C++
STL,code,demonstrates,Vector,Containers,Stack
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 1,240,245. lines
 Jobs: 167. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for C/ C++.
A Simple Linked List
By Maqsood on 2/7


Student Record
By Hasham Rasheed on 2/1


Httpget
By J Layton on 2/1


Click here to see a screenshot of this code!Swap a Nymber Using Pointers
By Jake Rodriguez Pomperada on 1/31

(Screen Shot)

Click here to see a screenshot of this code!Grades Solver Using Pointers
By Jake Rodriguez Pomperada on 1/31

(Screen Shot)

Base64 Encryption
By J Layton on 12/14


Click here to see a screenshot of this code!Address Book
By arifliminto86 on 1/29

(Screen Shot)

Http
By J Layton on 1/29


Combobox ADO
By Eder Rafo on 1/27


Email
By J Layton on 1/27


A Simple C++ tutorial for withdrawl of money from ATM machine
By Tejas Jayantilal Gandhi on 1/27


Click here to see a screenshot of this code!engine3d
By Harry Balzonya on 8/29

(Screen Shot)

Huge Bitmap Generator
By Josh Code on 1/24


quiz system
By Manuprasad K.S. on 1/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

Code examples of Vector Containers, Stacks, Queues, and Lists.

Print
Email
 
VB icon
Submitted on: 5/13/2008 11:48:17 AM
By: Chris C. 
Level: Intermediate
User Rating: Unrated
Compatibility:C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++

Users have accessed this code 3223 times.
 
author picture
(About the author)
 
     This code demonstrates how to use Vector Containers, Stacks, Queues, and Lists, using the STL in C++.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: Code examples of Vector Contain
//     ers, Stacks, Queues, and Lists.
// Description:This code demonstrates ho
//     w to use Vector Containers, Stacks, Queu
//     es, and Lists, using the STL in C++.
// By: Chris C.
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=12144&lngWId=3//for details.//**************************************
//     

// Here we want to include the templated
//     classes included with the STL.
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <list>
// Here we declare our functions..
void VectorExample();
void StackExample();
void QueueExample();
void LinkedListExample();
// We want to use the std namespace to s
//     implify the code.
using namespace std;
// Our entry point function...
int main(int argc, char *argv[])


    {
    	// this variable will hold the user's selection.
    	int nSelection = -1;
    	// Welcome the user, and get input...
    	std::cout << "Welcome! Please select which demonstration you would like displayed:\n";
    	std::cout << "\n[0] Vector Containers\n[1] Stacks\n[2] Queues\n[3] Linked Lists\n[4] Exit\n\n";
    	
    	while((nSelection < 0) || (nSelection > 4))


        	{
        		std::cout << "Selection: ";
        		std::cin >> nSelection;
        		// if the user decides to exit, we return 0.
        		if (nSelection == 4)
        			return 0; 
        	}
        	switch(nSelection)


            	{
            	case 0:
            		// We are going to demonstrate Vector Containers
            		VectorExample();
            		break;
            	case 1:
            		// We are going to demonstrate Stacks
            		StackExample();
            		break;
            	case 2:
            		// We are going to demonstrate Queues
            		QueueExample();
            		break;
            	case 3:
            		// We are going to demonstrate Linked Lists
            		LinkedListExample();
            		break;
            	};
            	std::cout << "\n\nGoodbye!\n\n" << std::endl;
            	return 0;
        }

void VectorExample() { // Create a vector of integers std::vector<int> MyVector; std::cout << "\n\nWelcome to the Vector Containers demonstration...\n"; std::cout << "You will be asked to enter 3 numbers.\n"; for (int i = 0; i < 3; i++) { int nTemp = 0; std::cout << "\nEnter #" << i+1 << ": "; std::cin >> nTemp; // Here we "push" or add an item to the vector. MyVector.push_back(nTemp); } for (int i = 0; i < 3; i++) { std::cout << "\nThe number you entered at #" << i+1 << " was: " << MyVector.at(i); } }
void StackExample() { // Create a stack of integers. std::stack<int> MyStack; std::cout << "\n\nWelcome to the Stacks demonstration...\n"; std::cout << "You will be asked to enter 3 numbers.\n"; for (int i = 0; i < 3; i++) { int nTemp = 0; std::cout << "\nEnter #" << i+1 << ": "; std::cin >> nTemp; // Here we "Push" or add an item to the stack. MyStack.push(nTemp); } // Because a stack is in FILO, we need to go backwords for (int i = 3; i > 0; i--) { std::cout << "\nThe number you entered at #" << i << " was: " << MyStack.top(); // Write here we "pop" or remove the next item in the stack. MyStack.pop(); } }
void QueueExample() { // Create a queue of integers. std::queue<int> MyQueue; std::cout << "\n\nWelcome to the Queues demonstration...\n"; std::cout << "You will be asked to enter 3 numbers.\n"; for (int i = 0; i < 3; i++) { int nTemp = 0; std::cout << "\nEnter #" << i+1 << ": "; std::cin >> nTemp; // Here we "Push" or add an item to the queue. MyQueue.push(nTemp); } for (int i = 0; i < 3; i++) { std::cout << "\nThe number you entered at #" << i+1 << " was: " << MyQueue.front(); // Write here we "pop" or remove the next item in the QUEUE. MyQueue.pop(); } }
void LinkedListExample() { // Create a linked list of integers. std::list<int> MyList; std::cout << "\n\nWelcome to the Linked Lists demonstration...\n"; std::cout << "You will be asked to enter 3 numbers.\n"; for (int i = 0; i < 3; i++) { int nTemp = 0; std::cout << "\nEnter #" << i+1 << ": "; std::cin >> nTemp; // Here we "Push" or add an item to the queue. MyList.push_back(nTemp); } list<int>::iterator nCounter = MyList.begin(); for (int i = 0; i < 3; i++) { std::cout << "\nThe number you entered at #" << i+1 << " was: " << *nCounter; nCounter++; } }


Other 5 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997-2010 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.