# 1. Vectors Introduction

C++中有很多很好用的容器类，比如vector:

1. The vector has size 0 when we instantiate it.
2. We use ‘resize’ to change the size of the vector.
``````// constructing vectors
#include <iostream>
#include <vector>  //Need to include the vector library!

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//Changing the size of vectorInts to 6
vectorInts.resize(6);
std::cout<<"\n\nvectorInts now has "<<vectorInts.size()<<" elements\n";

return 0;
}
``````
``````vectorInts has 0 elements
vectorInts now has 6 elements
``````

# 4. Vectors and Iterators

vector比array更通用，可以在运行时修改其长度，也可以插入元素到vector。

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

std::cout<<"\n\nAdding four elements to the vector\n";
//assigning the value 3 to 4 elements of the vector
vectorInts.assign(4,3);
std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}

``````
``````vectorInts has 0 elements

Adding four elements to the vector
vectorInts has 4 elements
VectorInts has these elements:
3 3 3 3
``````

# 5. Vectors and Its Prog Ans

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

using namespace std;

void printVector(vector<float> vIn);

void printVector(vector<float> vIn)
{//printing the contents of vIns
//TODO: Complete the function
vector<float>::iterator it;
for (it = vIn.begin(); it != vIn.end(); ++it)
cout<<*it<<" ";
}

``````
``````// Goal: pracitce constructing vectors
// and their iterators
// Call the vector vFloat
// Call the iterator it

#include "main.hpp"

int main ()
{
//TODO: create a vector of floats
vector<float> vFloat;

std::cout<<"vFloat has "<<vFloat.size()<<" elements\n";

//TODO: add elements to the library

std::cout<<"\n\nAdding 10 elements to the vector\n";

//TODO: assign the value 8.8 to 10 elements of the vector
vFloat.assign(10, 8.8);
std::cout<<"vFloat has "<<vFloat.size()<<" elements\n";

//TODO: Complete the Print function in main.hpp
// Call the function here to print out each element of vFloat
printVector(vFloat);

return 0;
}
``````

``````vFloat has 0 elements

Adding 10 elements to the vector
vFloat has 10 elements
8.8 8.8 8.8 8.8 8.8 8.8 8.8 8.8 8.8 8.8
``````

# 7. assign Elements

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

vectorInts.assign(1,23);
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

std::cout<<"\n\nAdding four elements to the vector\n";
//assigning the value 3 to 4 elements of the vector
vectorInts.assign(4,3);
std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}

``````

``````vectorInts has 0 elements
23

Adding four elements to the vector
vectorInts has 4 elements
VectorInts has these elements:
3 3 3 3
``````

# 8. push_back Elements

``````vectorInts.push_back(24);
vectorInts.push_back(25);
vectorInts.push_back(26);
vectorInts.push_back(27);
``````

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

vectorInts.assign(1,23);
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

std::cout<<"\n\nAdding four elements to the vector\n";
//assigning the value 3 to 4 elements of the vector
vectorInts.push_back(24);
vectorInts.push_back(25);
vectorInts.push_back(26);
vectorInts.push_back(27);
std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}
``````
``````vectorInts has 0 elements
23

Adding four elements to the vector
vectorInts has 5 elements
VectorInts has these elements:
23 24 25 26 27
``````

# 9. insert elements

``````it  = vectorInts.begin() + 1;
vectorInts.insert(it, -1); // 在第一个元素后添加-1
``````

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

vectorInts.push_back(0);
vectorInts.push_back(1);
vectorInts.push_back(2);
vectorInts.push_back(3);
vectorInts.push_back(4);
vectorInts.push_back(5);
vectorInts.push_back(6);
vectorInts.push_back(7);

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//insert an element after the first element
it  = vectorInts.begin() + 1;
vectorInts.insert(it, -1);
std::cout<<"\n\nAfter the insert\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//insert an element after the third element
it  = vectorInts.begin();
vectorInts.insert(it + 3, -2);
std::cout<<"\n\nAfter the insert\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//insert an element after the third element
it  = vectorInts.begin();
vectorInts.insert(it + 5, -3);
std::cout<<"\n\nAfter the insert\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}
``````
``````vectorInts has 8 elements
VectorInts has these elements:
0 1 2 3 4 5 6 7

After the insert
0 -1 1 2 3 4 5 6 7

After the insert
0 -1 1 -2 2 3 4 5 6 7

After the insert
0 -1 1 -2 2 -3 3 4 5 6 7
``````

# 10. emplace Elements

C++11中，针对顺序容器(如vector、deque、list)，新标准引入了三个新成员：` emplace_front、emplace和emplace_back`，这些操作构造而不是拷贝元素。这些操作分别对应`push_front、insert和push_back`，允许我们将元素放置在容器头部、一个指定位置之前或容器尾部。

The difference makes emplace more efficient than insert in special cases.

• `insert:` copies objects into the vector.
• `emplace:` construct them inside of the vector.

``````struct Foo
{
Foo(int n, double x);
};

std::vector<Foo> v;
v.emplace(someIterator, 42, 3.1416);
v.insert(someIterator, Foo(42, 3.1416));
``````

# 11. Add Elements Prog. Quiz

In this programming quiz, use these member functions to complete the functions in the header file.

• assign
• push_back
• insert
• emplace
``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

using namespace std;

void printVector(vector<int> vIn);
void assignFunction(vector<int> vInts, int in);
void pushBackFunction(vector<int> vInts, int in);
void emplaceFunction(vector<int> vInts, int loc, int in);

void printVector(vector<int> vIn)
{//printing the contents of vIns
//TODO: Finish the function
vector<int>::iterator it;
for (it=vIn.begin();it!=vIn.end();++it){
cout << *it;
}
}

void assignFunction(vector<int> vInts, int in)
{
cout<<"\nassigning "<<in<<" and printing the vector\n";
//TODO: Finish the function
int iSize = vInts.size();
vInts.assign(iSize,in);

printVector(vInts);
}
void pushBackFunction(vector<int> vInts, int in)
{
cout<<"\npush back "<<in<<" and printing the vector\n";
//TODO: Finish the function
vInts.push_back(in);
printVector(vInts);
}
void emplaceFunction(vector<int> vInts,  int loc, int in)
{
vector<int>::iterator it;
cout<<"\nemplacing "<<in<<" and printing the vector\n";
//TODO: Finish the function
it = vInts.begin() + loc;
vInts.emplace(it,in);
printVector(vInts);
}
``````
``````// Goal: pracitce constructing vectors
// and their iterators
// Call the vector vFloat
// Call the iterator it

#include "main.hpp"

int main ()
{
vector<int> vInts;
vInts.assign(10, 5);
printVector(vInts);
assignFunction(vInts, 1);
pushBackFunction(vInts, 2);
emplaceFunction(vInts,1, 3);

return 0;
}
``````

``````5 5 5 5 5 5 5 5 5 5
assigning 1 and printing the vector
1 1 1 1 1 1 1 1 1 1
push back 2 and printing the vector
5 5 5 5 5 5 5 5 5 5 2
emplacing 3 and printing the vector
5 3 5 5 5 5 5 5 5 5 5
``````

# 13. clear Vector

We can remove all elements from the vector at the same time using the method ::clear.

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

vectorInts.push_back(0);
vectorInts.push_back(1);
vectorInts.push_back(2);
vectorInts.push_back(3);
vectorInts.push_back(4);
vectorInts.push_back(5);
vectorInts.push_back(6);
vectorInts.push_back(7);

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//clear the vector
vectorInts.clear();
std::cout<<"\nAfter clear, the vector has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}
``````
``````vectorInts has 8 elements
VectorInts has these elements:
0 1 2 3 4 5 6 7
After clear, the vector has these elements:
``````

# 14. erase Vector

1. delete a single element from a vector For example:
``````//erase the 5th element in the vector
vectorInts.erase(vectorInts.begin()+4);
``````
1. delete a range of elements form a vector For example:
``````//erase a range of elements in the vector
vectorInts.erase(vectorInts.begin()+1, vectorInts.begin()+3);
``````

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

vectorInts.push_back(0);
vectorInts.push_back(1);
vectorInts.push_back(2);
vectorInts.push_back(3);
vectorInts.push_back(4);
vectorInts.push_back(5);
vectorInts.push_back(6);
vectorInts.push_back(7);

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//clear the vector
it = vectorInts.begin();
vectorInts.erase(it + 4);
std::cout<<"\nAfter clear, the vector has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

return 0;
}
``````

``````vectorInts has 8 elements
VectorInts has these elements:
0 1 2 3 4 5 6 7
After clear, the vector has these elements:
0 1 2 3 5 6 7
``````

# 15. pop_back

We can remove the last element of a vector using ::pop_back.

``````//pop the last element off the vector
vectorInts.pop_back();
``````

pop_back DOES NOT return a value!，没有返回值。

``````// constructing vectors
// constructing vectors
#include <iostream>
#include <vector>

int main ()
{
//creating a vector of integers
std::vector<int> vectorInts;
//creating an iterator for the vector
std::vector<int>::iterator it;

vectorInts.push_back(0);
vectorInts.push_back(1);
vectorInts.push_back(2);
vectorInts.push_back(3);
vectorInts.push_back(4);
vectorInts.push_back(5);
vectorInts.push_back(6);
vectorInts.push_back(7);

std::cout<<"vectorInts has "<<vectorInts.size()<<" elements\n";

//printing the contents of vectorInts
std::cout<<"VectorInts has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//pop the last element off the vector
vectorInts.pop_back();
std::cout<<"\n\nAfter pop_back(), the vector has these elements:\n";
for (it = vectorInts.begin(); it != vectorInts.end(); ++it)
std::cout<<*it<<" ";

//pop_back does not return the element that was removed.
//For example, the line of code below will return an error.
int a;
//a = vectorInts.pop_back(); //this line does not compile
return 0;
}
``````
``````vectorInts has 8 elements
VectorInts has these elements:
0 1 2 3 4 5 6 7

After pop_back(), the vector has these elements:
0 1 2 3 4 5 6
``````