C++ for algorithms
speedrun
MTUCI ICPC
Зачем нужно олимпиадное программирование?
-
Развития общего мышления
-
Ускорения работы программ
-
Прохождение собеседований
-
Соревнования и олимпиады
-
Наслаждение от решения задач
Почему C++
-
Скорость
-
Распространенность
-
Относительная низкоуровневость
-
Удобство (да, удобно)

Основы синтаксиса C++
#include <iostream>
using namespace std;
int main() {
cout<<”Hello World!”;
return 0;
}
Типы данных в C++
-
Целочисленные (int32_t, int64_t) проще: int, long long
-
Дробные (double, Decimal)
-
Символ (char)
-
Строка string
-
Контейнеры: vector, set, map, unordered_set/map
-
Специализированные структуры данных (queue, deque, pair, stack)
Спойлер a+b
#include <iostream>
using namespace std;
int main() {
int a,b;
cin>>a>>b;
cout<<a+b;
return 0;
}
Цикл for
vector <int> a = {1,2,3,4,5};
for (int i=0;i<a.size();i++){
cout<<a[i]<<' ';
}
cout<<'\n';
for (int i:a){
cout<<i<<' ';
}
Цикл while
bool condition;
while (condition){
//do_smth
}
do {
//do_smth
} while (condition)
Условия
bool condition, condition2;
if (condition){
//do_smth
}
else if(condition2){
//do another things
}
else{
//do other stuff
}
char a;
switch (a) {
case ('a'):
cout<<"we got a";
case('b'):
cout<<"we got b";
default:
cout<<"Unexpected char";
}
Работа с массивами
#include "iostream"
#include "vector"
using namespace std;
int main(){
vector<int> a(10);
for (size_t i=0;i<a.size();++i){
cin>>a[i];
}
for (size_t i=10; i<15;++i){
int t;
cin>>t;
a.push_back(t);
}
return 0;
}
Set&Map
#include "iostream"
#include "string"
#include "unordered_map"
using namespace std;
int main(){
unordered_map <string, int> a;
int n;
cin>>n;
string country;
int population;
for (size_t i=0;i<n;++i){
cin>>country>>population;
a.emplace(country, population);
a.insert(make_pair(country,population));
}
return 0;
}
#include "iostream"
#include "string"
#include "unordered_set"
using namespace std;
int main(){
unordered_set <string> a;
int n;
cin>>n;
string name;
for (size_t i=0;i<n;++i){
cin>>name;
a.insert(name);
}
return 0;
}
Кратко про указатели и итераторы
#include "iostream"
using namespace std;
int main(){
int a=5;
int b=a;
int& c = a;
b+=1;
cout<<a<<'\n'; //-> 5
c+=1;
cout<<a<<'\n'; //-> 6
return 0;
}
#include <vector>
#include "iostream"
using namespace std;
int main(){
vector<int> a({1,2,3,4,5});
auto i = a.begin()+1;
cout<<"Value of i it: "<<*(i)<<'\n';
//-> Value of i it: 2
cout<<"Index of i using it: "<<i-a.begin()<<'\n';
//-> Index of i using it: 1
cout<<"Value of end-1: "<<*(a.end()-1)<<'\n';
//-> Value of end-1: 5
cout<<"Value of next i: "<<*next(i)<<'\n';
//-> Value of next i: 3
cout<<"Value of reverse begin iterator + 1: "<<*next(a.rbegin()+1)<<'\n';
//-> Value of reverse begin iterator + 1: 3
return 0;
}
Синтаксис функций
type_out func_name(input_type name_var){
//do smth
return result;
}
Полезные функции из STL
-
lower_bound
-
sort
-
make_heap
-
и многое, многое другое
Полезные библиотеки
-
iostream
-
iomanip
-
algorithm
-
vector, string e.t.c
-
cmath
Лайфхаки
Для подключения всей стандартной библиотеки разом можно написать:
#include <bits/stdc++.h>
Для ускорения программы без каких-либо усилий пропишите в теле main:
ios::sync_with_stdio(0);
cin.tie(0);
C++
By Fleming Kris
C++
- 221