# Vector and List - Part 1

◀ Basic Sequence Container Properties▶ Vector and List - Part 2
Amazon Now let’s spend some time learning more about vector and list, two of the most common sequence container types and they are worth extra exploring. Once you master them you’ll be able to use them more effectively!

The primary differences between vector and list are the following:
• List performs better in inserting, removing, moving elements at any position within the container. Vector performs equally only in inserting and removing elements at the end of the container.
• Vector provides random access to its elements by their position. With list one needs to iterate from a known position to the desired position.
• List requires extra memory to keep track of the linking information of the elements in the container.
• List supports several useful functions that vector does not support such as unique() and merge().
In essence vector is easier to use than list because vector supports random element access.

Imagine if you want to examine the value of the 10th element. With vector you use [] operator but with list you need to acquire an iterator first; then iterate from the first element one by one until you hit the desired element.

In fact vector may be the simplest STL container class and should suffice for a small programming project unless needs for using list are explicit.
Because vector and list have many functions in common I will cover them both in this section. You need to include <vector> in the header to use vector, and <list> in the header to use list.

Followed are three tables showing how to use vector and list. C represents a container type, including its identifier, such as vector<string> and list<int>; a represents an object of type C; n is an integer; c is a value of the identifier; p is an iterator. For example, in the statement
`vector<string> concert;`
vector<string> is C, concert is a, “A String” is c (c is any value of string).

The following table shows expressions applied to both vector and list:

 Expression Explanation a.front() returns the first element a.back() returns the last element a.push_back(c) adds c to the end a.pop_back() removes the element from the end
The following table shows expressions applied to vector but not list:
 Expression Explanation a[n] returns the element at position n a.at(n) returns the element at position n
The following table shows list’s functions which vector doesn’t have:

 Function Explanation void merge(list & a) merges list a with the invoking list to form a sorted list, stored in the invoking list. Both lists must be sorted, or the resulting list is not necessarily sorted. a is left empty. void splice(iterator p, list & a) inserts before p the contents of a, and a is left empty void sort() sorts the list using operator < void unique() if consecutive identical elements exist, they are reduced to a single element void remove(const C & c) removes all occurrences of c from the invoking list void reverse() reverses the order of the elements in the invoking list
Let’s see how vector works by looking at the following program.

```#include <iostream>
using namespace std;
#include <vector>
#include <string>

struct record {
string name;
string author;
};
vector<record> vr;
void remove();
void empty();
void show();

int main(){
char input;
cout << "********** Welcome to database **********\n";
while(true) {
cin >> input;
cin.get();
switch(input){
break;
case 'b': remove();
break;
case 'c': empty();
break;
case 'd': show();
break;
case 'e': cout << "Bye.\n";
return 0;
break;
default:  cout << endl;
}
cout<<endl;
}
return 0;
}

cout << "b. Remove a book.\n";
cout << "c. Empty database.\n";
cout << "d. Show the number of books currently in database.\n";
cout << "e. Quit.\n\n";
cout << "Please enter a choice: ";
}

record temp;
string name, author;
cout << "Please enter the name of the book: ";
getline(cin, name);
cout << "Please enter the name of the author: ";
getline(cin, author);
temp.author = author;
vr.push_back(temp);
cout << "The book has been added.\n";
}

void remove() {
string name;
vector<record>::iterator vri;
cout << "Please enter the name of the book: ";
getline(cin, name);
for(vri = vr.begin(); vri != vr.end(); vri++) {
if((*vri).name == name) {
vr.erase(vri);
cout << "The book has been removed.\n";
return;
}
}
}

void empty() {
vr.clear();
cout << "All records are cleared.\n";
}

void show() {
cout << "The number of books in current database is " << vr.size() << ".\n";
}```
This example covers only several common functions. Given the prototypes and descriptions of the functions in the aforementioned tables you should be able to experiment with the rest on your own. Vector is especially useful when you want to store many objects somewhere but you do not know how many in advance.

Next we'll look at more useful functions you can use with vector and list!
◀ Basic Sequence Container Properties▶ Vector and List - Part 2

fShare
Questions? Let me know!