vector
Initialization
#include <iostream>
#include <vector>
template <class T>
void display(T it, T end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
//initialize vector
std::vector<int> a = {1, 2, 3, 4};//C++11
display(a.begin(), a.end());//1 2 3 4
int array[] = {1, 2, 3, 4, 5};
std::vector<int> g(array, array+sizeof(array)/sizeof(int));
display(g.begin(), g.end());//1 2 3 4 5
std::vector<int> b;
for(int i = 0; i < 10; i++)
b.push_back(i);
display(b.begin(), b.end());//0 1 2 3 4 5 6 7 8 9
std::vector<int> c(10);
display(c.begin(), c.end());//0 0 0 0 0 0 0 0 0 0
std::vector<int> d(10, 100);//100 100 100 100 100 100 100 100 100 100
display(d.begin(), d.end());
std::vector<int> e(a.begin(), a.end());
display(e.begin(), e.end());//1 2 3 4
std::vector<int> f(a);
display(f.begin(), f.end());//1 2 3 4
//operator =
a = g;
display(a.begin(), a.end());//1 2 3 4 5
return 0;
}
Tranverse
#include <iostream>
#include <vector>
template <class T>
void display(T it, T end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
template <class T>
void display2(T it, T end)
{
while(it != end)
{
std::cout<<*it<<" ";
++it;
}
std::cout<<std::endl;
}
template <class T>
void display3(T c)
{
for(auto e : c)
std::cout<<e<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
std::vector<int> v = {1, 2, 3, 4};
display(v.begin(), v.end());//1 2 3 4
display2(v.begin(), v.end());//1 2 3 4
display3(v);//1 2 3 4
return 0;
}
Retrieve Vector
#include <iostream>
#include <vector>
template <class T>
void display(T it, T end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
//initialize vector
std::vector<int> a = {1, 2, 3, 4};//C++11
display(a.begin(), a.end());
//[]
std::cout<<a[1]<<std::endl;
//at
std::cout<<a.at(1)<<std::endl;
//front
std::cout<<a.front()<<std::endl;
//back
std::cout<<a.back()<<std::endl;
//data
int *ptr = a.data();
std::cout<<*ptr<<std::endl;
//begin
std::cout<<a.begin()[2];
std::cout<<*(a.begin()+2)<<std::endl;
//advance
auto it = a.begin();
std::advance(it, 1);
std::cout<<*it<<std::endl;
return 0;
}
Insert and Delete
#include <iostream>
#include <vector>
#include "Rectangle.h"
template <class T>
void display(T it, T end)
{
for(; it != end; ++it)
std::cout<<*it<<" ";
std::cout<<std::endl;
}
int main(int argc, char *argv[])
{
//initialize vector
std::vector<int> a = {1, 2, 3, 4};//C++11
//push_back
a.push_back(10);
//insert
a.insert(a.begin(), 20);//insert new element before the element at the specified position
std::vector<int> b(2, 100);
a.insert(a.begin(), b.begin(), b.end());
//erase
a.erase(a.begin()+2);
a.erase(a.begin(), a.begin()+2);
//clear
a.clear();
display(a.begin(), a.end());
//insert and emplace
std::vector<Rectangle> c;
c.push_back(Rectangle(2, 4));
c.insert(c.begin(), Rectangle(3, 5));//insert takes a reference to an object
c.emplace(c.begin(), 4, 6);//emplace takes the arguments to construct an object
for(int i = 0; i < c.size(); i++)
std::cout<<c[i].toString()<<std::endl;
return 0;
}
Capacity
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> a = {1, 2, 3, 4};//C++11
a.clear();
std::cout<<"Size: "<<a.size()<<std::endl;//0
std::cout<<"Capacity: "<<a.capacity()<<std::endl;//4
std::cout<<"Max_size: "<<a.max_size()<<std::endl;//4611686018427387903
if(a.empty())
std::cout<<"Empty ..."<<std::endl;//Empty ...
a.shrink_to_fit();//C++11
std::cout<<"Capacity: "<<a.capacity()<<std::endl;//0
return 0;
}
Operators
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3, 4};
std::vector<int> d = {1, 2, 3, 4};
if (c == d)
std::cout<<"Equals to ..."<<std::endl;
std::vector<int> e = {2, 3, 4, 5};
if(c < e)
std::cout<<"Less than ..."<<std::endl;
return 0;
}
Pointer in a Vector
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
class Rectangle
{
private:
double width;
double length;
public:
//accessor
double getWidth() const;
double getLength() const;
double getArea() const;
//mutator
void setWidth(double w);
void setLength(double l);
//destructor
~Rectangle()
{
std::cout<<"Call Destructor ..."<<std::endl;
}
};
#endif
//Rectangle.cpp
#include "Rectangle.h"
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getLength() const
{
return length;
}
double Rectangle::getArea() const
{
return width*length;
}
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
//main.cpp
#include <iostream>
#include <vector>
#include "Rectangle.h"
int main(int argc, char *argv[])
{
const int size = 10;
std::vector<Rectangle*> container;
//initialize vector
for(int i = 0 ; i < size; i++)
{
Rectangle *temp = new Rectangle();
temp->setWidth(i+1);
temp->setLength(2*i+1);
container.push_back(temp);
}
for(int i = 0; i < size; i++)
std::cout<<container[i]->getArea()<<std::endl;
//release memory
for(int i = 0; i < size; i++)
delete container[i];
//purge vector
container.clear();
return 0;
}
Memory
#include <iostream>
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> c = {1, 2, 3};
std::cout<<"Address: "<<&c[0]<<std::endl;
c.resize(100);
std::cout<<"Address of resizing: "<<&c[0]<<std::endl;
std::vector<int> d = {1};
for(int i = 0; i < 5; i++)
{
d.push_back(i);
std::cout<<"Address while the capacity changed: "<<d.capacity()<<" "<<&d[0]<<std::endl;
}
int array[] = {1, 2, 3, 4, 5};
d.insert(d.begin(), array, array+5);
for(int i = 0; i < d.size(); i++)
std::cout<<"Address: "<<d.capacity()<<" "<<&d[i]<<std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <cstring>
int main(int argc, char *argv[])
{
int array[] = {10, 20, 30, 40};
std::vector<int> c = {1, 2, 3};
c.resize(c.size()+4);
memcpy(c.data()+3, &array[0], 4*sizeof(int));
for(int i = 0; i < c.size(); i++)
std::cout<<c[i]<<std::endl;//1 2 3 10 20 30 40
return 0;
}
- vector is always contiguous since C++03
- The memory address may be changed as the vector's capacity is changed
- Copy array to vector
Reference