0% found this document useful (0 votes)
209 views2 pages

Learn C++ - Vectors Cheatsheet - Codecademy

Vectors in C++ allow for dynamic lists that can grow and shrink in size. They store elements of the same type and are created using std::vector<type> and the vector header. Common functions for vectors include push_back() to add elements, pop_back() to remove elements, size() to return the number of elements, and accessing elements by index with name[index].

Uploaded by

Vatsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views2 pages

Learn C++ - Vectors Cheatsheet - Codecademy

Vectors in C++ allow for dynamic lists that can grow and shrink in size. They store elements of the same type and are created using std::vector<type> and the vector header. Common functions for vectors include push_back() to add elements, pop_back() to remove elements, size() to return the number of elements, and accessing elements by index with name[index].

Uploaded by

Vatsal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Cheatsheets / Learn C++

Vectors
Vectors
In C++, a vector is a dynamic list of items, that can shrink
and grow in size. It is created using #include <iostream>
std::vector<type> name; and it can only store #include <vector>
values of the same type.
To use vectors, it is necessary to #include the int main() {
vector library.
std::vector<int> grades(3);

grades[0] = 90;
grades[1] = 86;
grades[2] = 98;

Vector Type
During the creation of a C++ vector, the data type of its
elements must be specified. Once the vector is created,
the type cannot be changed.

.push_back() & .pop_back()


The following functions can be used to add and remove
an element in a vector: std::vector<std::string> wishlist;


.push_back() to add an element to the “end” wishlist.push_back("Oculus");
of a vector wishlist.push_back("Telecaster");

.pop_back() to remove an element from the
“end” of a vector wishlist.pop_back();

std::cout << wishlist.size();


// Prints: 1

/
.size() Function
The .size() function can be used to return the
number of elements in a vector, like name.size() .
std::vector<std::string> employees;

employees.push_back("michael");
employees.push_back("jim");
employees.push_back("pam");
employees.push_back("dwight");

std::cout << employees.size();


// Prints: 4

Index
An index refers to an element’s position within an
ordered list, like a vector or an array. The first element std::vector<double> order = {3.99, 12.99,
has an index of 0. 2.49};
A specific element in a vector or an array can be
accessed using its index, like name[index] . // What's the first element?
std::cout << order[0];

// What's the last element?


std::cout << order[2];

You might also like