C++ Vector

less than 1 minute read

Define a new vector:

std::vector<int> my_vector;
std::vector<int> my_vector(10); // With inital allocation

Get data from vector:

int value = my_vector[index];
int value = my_vector.at(index);

Pushing and popping (LIFO):

my_vector.push_back(next_value);
my_vector.pop_back(); // Note: does not return anything

Check if the vector has items:

if (my_vector.empty()) { // ...

Example

cpp.sh/8ntss

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

int main() {
	std::vector<std::string> strings;

	strings.push_back("Hello Hoani");
	strings.push_back("Hello Emma");
	strings.push_back("Hello Indie");

	for (std::string str: strings) {
		std::cout << str << std::endl;
	}

	std::cin.get();
}