dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Intermediate
May 24, 2006

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


Making Complex Arrays

Sometimes you need a vector or list of associations. An association is a pair of values that has some link in the problem model. For example, a student and grade are associated with each other.

Suppose you want to maintain the grade of each student. You need a list of all the students together with their grades. This can be done by using <pair>, which is simply a struct with two values.

Here's the basic building block of a map:

vector<pair<string,int> > stu_gra;
string stu;
int gra;
do
{
   cin >> stu >> gra;
   stu_gra.push_back(pair<string,gra>(stu,gra) );
} while (stu != "end");
When you want to retrieve the values stored in the vector (or list) of pairs, you write:
vector<pair<string,int> > :: iterator i = stu_gra.begin();
while(i != stu_gra.end())
{
   cout << "Student : " << i->first << ", Grade : " << i->second << endl;
   i++;
}
The first and second members are used to access the respective elements of pair.

Sunil Kidambi
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date