std namespace.std::set, std::priority_queue,
or std::vector. Today we'll look at the latter#include <vector>
std::vector<int> numbers = {1, 2, 3};
int as its template argument,
meaning it can only store type intat() or [] |
returns the value at a specific index |
front() |
returns the first item |
back() |
returns the last item |
size() |
returns the number of elements |
clear() |
removes all items |
insert() |
inserts an item at a specific index |
push_back() |
adds a given element to the end |
pop_back() |
removes the last element |
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> trainers = {"Evan 1", "Evan 2", "Jason", "Andrew"};
cout << "There are: " << trainers.size() << " trainers" << endl;
trainers.push_back("Dallas");
trainers.push_back("Woodward");
cout << "Actually, there are: " << trainers.size() << " trainers" << endl;
}
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main() {
vector<string> trainers = {"Evan 1", "Evan 2", "Jason", "Andrew"};
trainers.push_back("Dallas");
trainers.push_back("Woodward");
//Can't forget about the OG trainer. He goes first
trainers.insert(trainers.begin(), "Matt");
trainers.insert(trainers.end(), "Sahit");
//This part is a metaphor for something
cout << "There are " << trainers.size() << " trainers" << endl;
cout << "The 1st trainer is " << trainers.at(0) << endl;
cout << "The last trainer is " << trainers.at(trainers.size() -1) << endl;
}
begin() |
starts at the first item and moves forwards when incremented |
end() |
starts at the last item and moves forwards when incremented |
rbegin() |
starts at the last item and moves backwards when incremented |
rend() |
starts at the first item and moves backwards when incremented |
container.begin()
* |
gets the value at the current index |
++ |
increments the iterator forwards |
-- |
decrements the iterator backwards |
vector<int> vec = {66,89,0,60,17,90,8};
vector<int>::iterator it = vec.begin();
vector<int> vec = {66,89,0,60,17,90,8};
vector<int>::iterator it = vec.begin();
it++;
vector<int> vec = {66,89,0,60,17,90,8};
vector<int>::iterator it = vec.begin();
it++;
it--;
vector<int> vec = {66,89,0,60,17,90,8};
vector<int>::reverse_iterator it = vec.rbegin();
it += 3;
vector<int> vec = {66,89,0,60,17,90,8};
vector<int>::reverse_iterator it = vec.rbegin();
it += 3;
sort() |
sorts a container in increasing order |
nth_element() |
finds the nth smallest element |
fill() |
fills a container with copies of a given element |
transform() |
manipulates each element using a function |
reverse() |
Reverses the order of the elements |
nth_element examplestd::nth_element(first, nth, last);first is an iterator to the beginninglast is an iterator to the endnth is an iterator to the element you would want if the container were sorted#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<int> v{2, 1, 5, 4, 3};
//this changes v to {1, 2, 3, 4, 5}
std::sort(v.begin(), v.end());
//this changes v to {5, 4, 3, 2, 1}
std::reverse(v.begin(), v.end());
}
count() |
counts the number of items in a container that match a given item |
find() |
returns an iterator to the first element that matches a given item |
accumulate() |
sums all elements in a container |
median_line