Click here to Skip to main content
15,885,100 members
Articles / Programming Languages / C++

Practical Guide to STL

Rate me:
Please Sign up or sign in to vote.
4.41/5 (65 votes)
12 Apr 200412 min read 493K   2.2K   117  
An article on practical learning of STL in the MS development environment.
// Program: Map Own Class
// Purpose: To demonstrate a map of classes

// #include "stdafx.h" - include if you use pre compiled headers
#include <string>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

class CStudent
{
public :
	int nStudentID;
	int nAge;
public :
	// Default Constructor - Empty
	CStudent()	{	}
	// Full constructor
	CStudent(int nSID, int nA)	{	nStudentID=nSID; nAge=nA;	}
	// Copy constructor
	CStudent(const CStudent& ob)	{	nStudentID=ob.nStudentID; nAge=ob.nAge;	}
	// Overload =
	void operator = (const CStudent& ob)	{	nStudentID=ob.nStudentID; nAge=ob.nAge;	}
};

int main(int argc, char* argv[])
{
	map <string, CStudent> mapStudent;

	mapStudent["Joe Lennon"] = CStudent(103547, 22);
	mapStudent["Phil McCartney"] = CStudent(100723, 22);
	mapStudent["Raoul Starr"] = CStudent(107350, 24);
	mapStudent["Gordon Hamilton"] = CStudent(102330, 22);

	// Access via the name
	cout << "The Student number for Joe Lennon is " << 
		(mapStudent["Joe Lennon"].nStudentID) << endl;

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
I shuffle tiny static charges on intricate wafers of silicon all day.

Comments and Discussions