Vector

# Introduction

• A vector is a collection of objects of a single type, each of which has an associated integer index.
• Vector declaration: vector<type> variable_name (optional_number_of_elements);
• Vectors can be resized during the execution of the program to accommodate extra elements as needed.

Vectors are good at:

• Accessing individual elements by their position index (constant time).
• Iterating over the elements in any order (linear time).
• Add and remove elements from its end (constant amortized time).

# Examples

Example 1: Ask the user to give marks for 5 students.

```#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<double> student_marks (5);

for (int i = 0; i < 5; i++)
{
cout << "Enter marks for student #" << i+1 << ": ";
cin >> student_marks[i];
}

return 0;
}
```

Example 2: Ask the user to define number of students. Use of resizable vectors.

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

int main()
{
vector<double> student_marks;     // no size specified: vector contains zero elements

int num_students;

cout << "Number of students in this group: ";
cin >> num_students;

student_marks.resize (num_students);

for (int i = 0; i < num_students; i++)  // valid subscripts for a vector with num_students elements are 0 to num_students-1
{
cout << "Enter marks for student #" << i+1 << ": ";
cin >> student_marks[i];
}

return 0;
}
```

Example 3: Using the pushback() method.

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

int main()
{
vector<double> student_marks;
int number;

cout << "Do you want to enter numbers (y/n)? ";

{
cout << "Enter value: ";
cin >> number;

// Now that we have the number from the user,
// append it at the end of the vector

student_marks.push_back(number);

cout << "Do you want to enter more numbers (y/n)? ";
}

// Now print all the values;  notice that we didn't need to
// count how many elements were entered:  we can always use
// the size() method to ask student_marks how many are there!

for (int i = 0; i < student_marks.size(); i++)
{
cout << "Student #" << i+1 << '\t' << student_marks[i];
}
// '\t' is the code for the TAB character

return 0;
}
```

# Accessing elements of a vector with Iterators

Example 4: The following fragment of code illustrates the use of iterators to print all the elements of a vector.

```// values has been declared as:  vector<int> values;
for (vector<int>::iterator i = values.begin(); i != values.end(); ++i)
{
cout << *i << endl;
}
```

# Examples

Example 5: General example that demonstrates most methods.

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

void print_vector(vector<int> sm);

int main()
{
int i = 0;
int input = 0;
vector<int> student_marks; // vector that holds student marks
vector<int>::iterator iter;

cout << "Please enter marks for students (enter 0 to end):\n";

//push_back(): Adds a new element at the end of the vector
do
{
cout << "Enter mark for student #" << i+1 << ": ";
cin >> input;
if(input != 0)
student_marks.push_back(input);
i++;
}while(input);
print_vector(student_marks);

//capacity(): Returns the size of the allocated storage space
cout << "Capacity of student_marks: " << student_marks.capacity() << endl;

//size() Returns the number of elements in the vector container
cout << "Size of student_marks: " << student_marks.size() << endl;

//insert() The vector is extended by inserting new elements before the element at position
cout << "insert mark 20 at position 2" << endl;
student_marks.insert(student_marks.begin()+2,20);
print_vector(student_marks);

//front(): Returns a reference to the first element
cout << "Marks of first student: " << student_marks.front() << endl;

// at(): Returns a reference to the element at position n in the vector
cout << "Marks of student #2: " << student_marks.at(2) << endl;

// back(): returns a reference to the last element in the vector
cout << "Marks of last student:  " << student_marks.back() << endl;

//pop_back(): Removes the last element
cout << "Removing the last element" << endl;
student_marks.pop_back();
print_vector(student_marks);

// erase(): Removes from the vector container
// erase the 3rd element
cout << "Erasing the 3rd element" << endl;
student_marks.erase (student_marks.begin()+2);
print_vector(student_marks);

//  assign() Assigns new content to the vector object,
//dropping all the elements contained in the vector before the call
//and replacing them by those specified by the parameters
cout << "Assign value 2 to all students" << endl;
student_marks.assign(10, 2);
print_vector(student_marks);
}

void print_vector(vector<int> sm)
{

// begin(): Returns an iterator referring to the first element in the vector container.
// end(): Returns an iterator referring to the past-the-end element in the vector
for(vector<int>::iterator iter = sm.begin(); iter != sm.end(); ++iter)
{
cout << *iter << " " ;
}
cout << endl;

}
```