C++ Vector Example

Vectors in C ++ are an alternative to the standard dynamic array for which the memory is allocated manually, using the new operator.

Language developers recommend using vectors instead of the manual memory allocation for the array. This helps to avoid memory leaks and facilitates the work of programmers.

Example of creating a vector

#include <iostream>
#include <vector>

int main()
	// vector with 10 int elements
	std::vector<int> v1(10);

	// vector with unknown number of float elements
	std::vector<float> v2;

	// vector with 10 int numbers with the value of 0
	std::vector<int> v3(10, 0);

	return 0;

Operations with vector elements

Let’s create a vector which will contain the arbitrary number names of students.

#include <iostream>
#include <vector>
#include <string>

int main()

	// string vector
	std::vector<std::string> students;

	// input buffer
	std::string buffer = "";

	std::cout << "Input the names of students\n"
		<< "When you are done input the empty string" << std::endl; do { std::getline(std::cin, buffer); if (buffer.size() > 0) {
			// pushing elements into the vector
	} while (buffer != "");

	// save the size of the vector 
	unsigned int vector_size = students.size();

	// output the vector to the screen
	std::cout << "Your vector: " << std::endl;
	for (int i = 0; i < vector_size; i++) {
		std::cout << students[i] << std::endl;

	return 0;

The result of the program:

Vector methods:

To add a new item to the end of the vector, use the push_back() method. The number of elements is determined by the size() method. To access the elements of a vector, you can use square brackets [], as well as for ordinary arrays.

pop_back () – remove the last element

clear () – remove all elements of a vector

empty () – check for emptiness

C++ Assignments Assistance Online

You can use our C vector example to deal with your own assignment. Our C++ sample allows you to handle your homework quickly and easily. When you get an assignment and don’t know how to deal with it on your own, we suggest you to use AssignmentShark.com C++ homework help. It doesn’t matter where you are situated, as we help students from all over the world.

The ordering process is simple on our site. Fill in the order form with your requirements and set the deadline. We have reasonable prices, so any student can afford using our service. Every order is done with a unique approach and according to the customer’s instructions. We do everything possible to satisfy our customers’ needs.

Everything that is included in your assignment is checked several times in order to provide you with accurate work. We are available 24/7 for our customers’ convenience. We have a big team of experts who are knowledgeable in different spheres of study. Using our service will result in a high grade. If you read through our page with testimonials, you will find out that our customers remain satisfied with our service. It is important to have somebody who can help with your assignment. We can convince you that we are the best option for you.

Leave a Reply

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

Customer testimonials

Submit your instructions to the experts without charge.