Making Complex Arrays

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 , which is simply a struct with two values.

Here’s the basic building block of a map:

vector > stu_gra;string stu;int gra;do{   cin >> stu >> gra;   stu_gra.push_back(pair(stu,gra) );} while (stu != "end");

When you want to retrieve the values stored in the vector (or list) of pairs, you write:

vector > :: 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.

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist