0% found this document useful (0 votes)
3 views

cplus

The document provides an overview of the C++ std::vector, detailing its dynamic array characteristics, element access methods, and manipulation functions such as inserting, deleting, and initializing elements. It includes examples of accessing elements via index, iterators, and member functions, as well as methods for deleting elements by index, value, or condition. Additionally, it presents two programming tasks related to finding specific elements in a vector based on given criteria.

Uploaded by

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

cplus

The document provides an overview of the C++ std::vector, detailing its dynamic array characteristics, element access methods, and manipulation functions such as inserting, deleting, and initializing elements. It includes examples of accessing elements via index, iterators, and member functions, as well as methods for deleting elements by index, value, or condition. Additionally, it presents two programming tasks related to finding specific elements in a vector based on given criteria.

Uploaded by

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

C++

std::vector
std::vector

• A vector is a dynamic array with automatically handled storage.

• The elements in a vector can be accessed just as efficiently as those in an array

• Vectors can dynamically change in size


Accessing Elements

Index-based access:
std::vector<int> v{ 1, 2, 3 };

// using []
int a = v[1]; // a is 2
v[1] = 4; // v now contains { 1, 4, 3 }

// using at()
int b = v.at(2); // b is 3
v.at(2) = 5; // v now contains { 1, 4, 5 }
int c = v.at(3); // throws std::out_of_range
exception
at() method performs bounds checking and can throw exceptions, it is slower than [].
Accessing Elements

The front() and back() member functions allow easy reference access to the first and last element of the vector

std::vector<int> v{ 4, 5, 6 };

int a = v.front(); // a is 4, v.front() is equivalent to v[0]

v.front() = 3; // v now contains {3, 5, 6}

int b = v.back(); // b is 6, v.back() is equivalent to v[v.size() -


1]

v.back() = 7; // v now contains {3, 5, 7}

Note: empty() member function (which checks if the container is empty) before calling front() or back()
Accessing Elements

Iterators:
std::vector<int> v{ 4, 5, 6 };
auto it = v.begin();
int i = *it; // i is 4
++it;
i = *it; // i is 5
*it = 6; // v contains { 4, 6, 6 }
auto it = v.end();
++it;
it == v.end(); // false, it points to the element at position 2 (with
value 6)
++it;
it == v.end(); // true
Initializing a std::vector

std::vector<int> v{ 1, 2, 3 }; // v becomes {1, 2,


3}

// Different from std::vector<int> v(3, 6)

std::vector<int> v{ 3, 6 }; // v becomes {3, 6}

// Different from std::vector<int> v{3, 6} in


C++11

std::vector<int> v(3, 6); // v becomes {6, 6, 6}


std::vector<int> v(4); // v becomes {0, 0, 0, 0}
Deleting elements

std::vector<int> v{ 1, 2, 3 };
Deleting the last element:
v.pop_back(); // v becomes {1, 2}

Deleting all elements: std::vector<int> v{ 1, 2, 3 };


v.clear(); // v becomes an empty vector

Deleting element by index: std::vector<int> v{ 1, 2, 3, 4, 5, 6 };


v.erase(v.begin() + 3); // v becomes {1, 2, 3, 5, 6}

std::vector<int> v{ 1, 2, 3, 4, 5, 6 };
Deleting all elements in a range:
v.erase(v.begin() + 1, v.begin() + 5); // v becomes
{1, 6}
Deleting elements

Deleting elements by value:


std::vector<int> v{ 1, 1, 2, 2, 3, 3 };
int value_to_remove = 2;
v.erase(std::remove(v.begin(), v.end(), value_to_remove), v.end()); // v becomes
{1, 1, 3, 3}

Deleting elements by condition:


bool _predicate(const int& element) {
return (element > 3); // This will cause all elements to be deleted that are larger
than 3
}
...
std::vector<int> v{ 1, 2, 3, 4, 5, 6 };
v.erase(std::remove_if(v.begin(), v.end(), _predicate), v.end()); // v becomes {1, 2
3}
Inserting elements

struct Point {
double x, y;
Point(double x, double y) : x(x), y(y) {}
};

Appending an element at the end of a vector: std::vector<Point> v;


Point p(10.0, 2.0);
v.push_back(p); // p is copied into the vector

std::vector<Point> v;
v.emplace_back(10.0, 2.0);
Inserting elements

std::vector<int> v{ 1, 2, 3 };
v.insert(v.begin(), 9); // v now contains {9, 1, 2, 3}

Appending an element at any position a vector:

std::vector<int> v{ 1, 2, 3 };
v.emplace(v.begin()+1, 9); // v now contains {1, 9,
2, 3}

std::vector<int> v(4); // contains: 0, 0, 0, 0


Inserting another vector at any position of the vector: std::vector<int> v2(2, 10); // contains: 10, 10
v.insert(v.begin()+2, v2.begin(), v2.end()); //
contains: 0, 0, 10, 10, 0, 0
Program
1) Write a C++ program that returns the elements in a vector that are strictly smaller than their
adjacent left and right neighbours.
Example: 1 2 5 0 3 1 7
elements that are smaller than its adjacent neighbours: 0 1

2) Write a C++ program that returns the element in a vector that is the first unique element
Example: 1 2 5 0 3 2 1 7 0 1
element that is the first unique: 5

You might also like