int num = 42; | float pi = 3.14; | char letter = 'A'; | int arr[5] = {1, 2, 3, 4, 5};
Structures (struct) | Classes (class) | Enums (enum) |
---|---|---|
Structures allow you to group multiple data types into a single entity. struct Person {
std::string name;
int age;
};
|
Classes are similar to structures but encapsulate data and functions (methods) that operate on that data.
class Rectangle {
public:
int length;
int width;
int area() {
return length * width;
}
};
|
Enums allow you to define a set of named integer constants. enum Color {
RED,
GREEN,
BLUE
};
|
Some of the key components of the STL include:
- Containers: The STL provides a range of containers, such as vector, list, map, set, and stack, which can be used to store and manipulate data.
- Algorithms: The STL provides a range of algorithms, such as sort, find, and binary_search, which can be used to manipulate data stored in containers.
- Iterators: Iterators are objects that provide a way to traverse the elements of a container. The STL provides a range of iterators, such as forward_iterator, bidirectional_iterator, and random_access_iterator, that can be used with different types of containers.
- Function Objects: Function objects, also known as functors, are objects that can be used as function arguments to algorithms. They provide a way to pass a function to an algorithm, allowing you to customize its behavior.
- Adapters: Adapters are components that modify the behavior of other components in the STL. For example, the reverse_iterator adapter can be used to reverse the order of elements in a container.
Vectors | Lists | Sets and Maps |
---|---|---|
Vectors are dynamic arrays that can grow or shrink in size. #include <vector>
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6); // Add an element
|
Lists are doubly-linked lists, allowing efficient insertion and removal of elements anywhere in the list.
#include <list>
std::list<std::string> names = {"Alice", "Bob", "Charlie"};
names.push_back("David");
names.pop_front(); // Remove the first element
|
Sets store unique elements, and maps store key-value pairs.
#include <set>
#include <map>
std::set<int> uniqueNumbers = {1, 2, 3, 4, 5};
std::map<std::string, int> scores = {{"Alice", 90}, {"Bob", 85}};
|
Stacks and Queues | Deques | Arrays |
Stacks follow the Last-In-First-Out (LIFO) principle, while queues follow the First-In-First-Out (FIFO) principle. #include <stack>
#include <queue>
std::stack<int> s;
s.push(1); // Push an element onto the stack
std::queue<int> q;
q.push(1); // Enqueue an element
|
Deques (Double-ended queues) allow efficient insertion and deletion at both ends.
#include <deque>
std::deque<int> dq;
dq.push_back(1); // Add an element to the back
dq.push_front(2); // Add an element to the front
|
STL arrays are fixed-size arrays.
#include <array>
std::array<int, 3> arr = {1, 2, 3};
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
// create a vector to store int
vector<int> vec;
int i;
// display the original size of vec
cout << "vector size = " << vec.size() << endl;
// push 5 values into the vector
for(i = 0; i < 5; i++) {
vec.push_back(i);
}
// display extended size of vec
cout << "extended vector size = " << vec.size() << endl;
// access 5 values from the vector
for(i = 0; i < 5; i++) {
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
// use iterator to access the values
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout << "value of v = " << *v << endl;
v++;
}
return 0;
}
std::sort | std::find |
---|---|
int main () {
array <int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
cout << "Before sorting: ";
Print(s);
std :: sort(s.begin (), s.end ());
cout << "After sorting: ";
Print(s);
return 0;
}
|
int main () {
const int n1 = 3;
std::vector <int> v{0, 1, 2, 3, 4};
auto result1 = std :: find(v.begin(), v.end(), n1);
if (result1 != std :: end(v)) {
cout<< "v contains: "<<n1<<endl;
} else {
cout<< "v does not contain: "<< n1<<endl;
}
}
|
std::fill | std::count |
int main () {
std::vector <int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::fill(v.begin(), v.end(), -1);
Print(v);
}
|
int main () {
std:: vector <int> v{1, 2, 3, 4, 4, 3, 7, 8, 9, 10};
const int n1 = 3;
const int n2 = 5;
int num_items1 = std::count(v.begin(), v.end(), n1);
int num_items2 = std::count(v.begin(), v.end(), n2);
cout<< n1<< " count: " << num_items1<< endl;
cout<< n2<< " count: " << num_items2<< endl;
return 0;
}
|
std::count_if | std::for_each |
inline bool div_by_3(int i) { return i % 3 == 0; }
int main () {
std::vector <int> v{1, 2, 3, 3, 4, 3, 7, 8, 9, 10};
int n3 = std::count_if(v.begin(), v.end(), div_by_3);
cout<< "# divisible by 3: " <<n3<<endl;
}
|
int main () {
std :: vector <int> nums {3, 4, 2, 8, 15, 267};
// lambda expression , lecture_9
auto print = [](const int& n) { cout << " " << n; };
cout << "Numbers:";
std::for_each(nums.cbegin(), nums.cend(), print);
cout << endl;
return 0;
}
|
std::all_off | std::rotate |
inline bool even(int i) { return i % 2 == 0; };
int main () {
std:: vector <int> v(10, 2);
std:: partial_sum (v.cbegin(), v.cend(), v.begin());
Print(v);
bool all_even = all_of(v.cbegin(), v.cend(), even);
if (all_even ) {
cout << "All numbers are even" << endl;
}
}
|
int main () {
std :: vector <int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "before rotate: ";
Print(v);
std :: rotate(v.begin(), v.begin() + 2, v.end());
cout << "after rotate: ";
Print(v);
}
|
std::transform | std::accumulate |
auto UpperCase (char c) { return std::toupper(c); }
int main () {
const std :: string s("hello");
std :: string S{s};
std :: transform (s.begin(),
s.end(),
S.begin(),
UpperCase );
cout << s << endl;
cout << S << endl;
}
|
int main () {
std::vector <int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = std :: accumulate (v.begin(), v.end(), 0);
int product = std :: accumulate (v.begin(),
v.end (), 1, std :: multiplies());
cout<< "Sum : " <<sum << endl;
cout<< "Product: " <<product << endl;
}
|
std::max | std::min_element |
int main () {
using std :: max;
cout<< "max(1, 9999) : "<< max(1, 9999) <<endl;
cout<< "max('a', 'b'): "<< max('a', 'b') <<endl;
}
|
int main () {
std :: vector <int> v{3, 1, 4, 1, 0, 5, 9};
auto result = std::min_element (v.begin(), v.end());
auto min_location = std::distance(v.begin(), result);
cout<<"min at: "<< min_location << endl;
}
|
std::minmax_element | std::clamp |
int main () {
using std :: minmax_element ;
auto v = {3, 9, 1, 4, 2, 5, 9};
auto [min , max] = minmax_element(begin(v), end(v));
cout << "min = "<< *min << endl;
cout << "max = "<< *max << endl;
} |
int main () {
// value should be between [kMin ,kMax]
const double kMax = 1.0F;
const double kMin = 0.0F;
cout<<std ::clamp (0.5 , kMin , kMax) << endl;
cout<<std ::clamp (1.1 , kMin , kMax) << endl;
cout<<std ::clamp (0.1 , kMin , kMax) << endl;
cout<<std ::clamp (-2.1, kMin , kMax) << endl;
}
|
std::sample | |
int main () {
std :: string in = "C++ is cool", out;
auto rnd_dev = std::mt19937{ random_device {}() };
const int kNLetters = 5;
std :: sample(in.begin (),
in.end (),
std :: back_inserter (out),
kNLetters ,
rnd_dev);
cout<<"from : " <<in << endl;
cout<<"sample: " <<out << endl;
} |
resources: The C++ Standard Template Library (STL), cplusplus/stl