In this tutorial, I will show how to store class objects in vector just like making a linked list using the STL. A conventional linked list is a structure of data that is linked together using the struct or class keywords.
Overview of Linked List.
An array is like a tupperware, they are containers but they are fixed in size. If you choose a big container it would be a waste of storage area on the other side, if you choose a small container chances are it would spill and that would be a big mess. Before the advent of the STL containers, programmers have found ways to solve the problem and that is creating a lists.
There are tree kind of Lists.
1.)Singly List
2.)Doubly List
3.)Trees
Why Use STL?
When the STL has been standardized, the use of home-cooked containers are not anymore popular in real world problems, although these are good to learn how it works specially manipulating computer memory by using a lot of pointers. If your code has new operations, delete operations, and pointer arithmetic all over the place, you are going to mess up somewhere and get leaks, stray pointers, etc. This is true independently of how conscientious you are with your allocations: eventually the complexity of the code will overcome the time and effort you can afford. It follows that successful techniques rely on hiding allocation and deallocation inside more manageable types. Good examples are the standard containers. -Bjarne Stroustrup
Store class objects in vector
Let's go with the codes. In this example I will make Friend class to create an objects which has data such as name, age and height and store these objects in vector. The idea is similar in creating a linked list, but I will not deal with linked lists anymore for the reason that it has plenty of tutorials here on DIC or if you search in google.
class Friend
{
public:
Friend();
~Friend(){}
void setName(string friendName){ name = friendName;}
void setAge(int friendAge) { age = friendAge; }
void setHeight(int friendHeight) { height = friendHeight; }
void printFriendInfo();
private:
string name;
int age;
float height;
};
Friend::Friend()
{
age = 0;
height = 0.0;
}
void Friend:: printFriendInfo()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Height : " << height << endl <<endl;
}
A friend class is declared and has three member functions which are inlined. Each function has one parameter and is set to the corresponding variables. The last is the printFriendInfo() is used to print member data.
A constructor was made and is setting the member variables age and height to 0.
The main function.
int main()
{
// Create a vector of Friend objects
vector<Friend> list;
string Name;
int Age;
float Height;
Friend *f1;
for (int n=0; n<3; n++)
{
cout <<"Enter name :"<<endl;
getline(cin, Name);
cout <<"Enter age :"<<endl;
cin >> Age;
cout <<"Enter height :"<<endl;
cin>> Height;
f1 = new Friend;
f1->setName(Name);
f1->setAge(Age);
f1->setHeight(Height);
list.push_back(*f1);
}
// Now setup an iterator loop through the vector
vector<Friend>::iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
// For each friend, print out their info
it->printFriendInfo();
}
return 0;
}
In the main function, first line is the declaration of vector Friends(empty vector). Then there are three variables were made. The for loop gives three inputs from the user, after getting age, name and height new Friend object is created and put it in vector. The loop terminates after 3 inputs. Iterator is created, to walk through the lists and it will display each object's data.
It is easy, right? Well its your turn now modifying this codes with the use of list container.
Full source codes;
/****************************************
Store class objects in vector
By: nirvanarupali
Compiled in Borland C++
*********************************************/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Friend
{
public:
Friend();
~Friend(){}
void setName(string friendName){ name = friendName;}
void setAge(int friendAge) { age = friendAge; }
void setHeight(int friendHeight) { height = friendHeight; }
void printFriendInfo();
private:
string name;
int age;
float height;
};
//implementations
Friend::Friend()
{
age = 0;
height = 0.0;
}
//printing
void Friend:: printFriendInfo()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Height : " << height << endl <<endl;
}
int main()
{
// Create a vector of Friend objects
vector<Friend> list;
string Name;
int Age;
float Height;
Friend *f1;
for (int n=0; n<3; n++)
{
cout <<"Enter name :"<<endl;
getline(cin, Name);
cout <<"Enter age :"<<endl;
cin >> Age;
cout <<"Enter height :"<<endl;
cin>> Height;
f1 = new Friend;
f1->setName(Name);
f1->setAge(Age);
f1->setHeight(Height);
list.push_back(*f1);
cin.get(); //clear buffer
}
// Now setup an iterator loop through the vector
vector<Friend>::iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
// For each friend, print out their info
it->printFriendInfo();
}
return 0;
}





MultiQuote




|