Vectors are sequence containers representing arrays that can change in size.  They use contiguous storage locations for their elements so can be accessed in the same way and as efficiently as arrays, but unlike arrays their size can change dynamically, with their storage being handled automatically by the container.

Create a vector

#include <iostream>
#include <vector>

	vector<string> MyVector1;

	MyVector1.push_back("Hello");			//push_back = Add a new item to the end
	MyVector1.push_back("World");

	int Index;	
	for (Index = 0; Index < MyVector1.size(); Index++)
		std::cout << "Next vector element: " << MyVector1[Index] << std::endl;

Length of a vector

	 = MyVector1.size();

Delete item at index

	MyVector1.erase(MyVector1.begin()+2);		//Delete vector entry at index 2

Delete all items

	MyVector1.clear();

Insert item at index

	MyVector1.insert((MyVector1.begin() + 0), "Hello");		//UInsert new item at index 0
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *