Você está na página 1de 408

stamatis@uoc.

gr

http://creativecommons.
org/licenses/by-nc-sa/4.0/deed.el

https://www.materials.uoc.gr/el/undergrad/courses/ETY215/notes.pdf
1
advance()
next()
prev()
distance()
iterator_traits<>
accumulate()
inner_product()
partial_sum()
adjacent_difference()

min_element()
max_element()
minmax_element()

copy()
move()
copy_backward()
move_backward()

rotate()
rotate_copy()

replace()
replace_copy()

remove()
remove_copy()
unique()
unique_copy()

reverse()
reverse_copy()

random_shuffle()
shuffle()

partition()
stable_partition()
is_partitioned()
partition_point()
partition_copy()

sort()
stable_sort()
nth_element()
partial_sort()
partial_sort_copy()
is_sorted()
is_sorted_until()

lexicographical_compare()
next_permutation()
prev_permutation()
is_permutation()

find()
find_first_of()
search()
find_end()
adjacent_find()
search_n()
binary_search()
upper_bound()
lower_bound()
equal_range()

merge()
inplace_merge()
set_union()
set_intersection()
set_difference()
set_symmetric_difference()
includes()

make_heap()
is_heap()
is_heap_until()
pop_heap()
push_heap()
sort_heap()
all_of()
none_of()
any_of()
count()
equal()
mismatch()

iota()
for_each()
swap_ranges()
transform()
fill()
generate()
http://www.dmst.aueb.gr/dds/faq/academic.html#oo
#include <iostream>

/*
main:
Den pairnei orismata.
Zhta ena pragmatiko kai typwnei to tetragwno tou.
Epistrefei 0.
*/
int main()
{
std::cout << u8"Δώσε πραγματικό αριθμό: "; // Mhnyma sthn othoni

double a; // Dhlwsh pragmatikhs metavlhths.


std::cin >> a; // Eisagwgi timhs apo plhktrologio

double b; // Dhlwsh allhs metablhths


b = a * a;
std::cout << u8"Το τετράγωνο είναι: "; // Mhnyma sthn othoni
std::cout << b << '\n';
// Ektypwsi apotelesmatos kai allagh grammhs

return 0; // Epistrofh me epityxia.


}

#include

#include <iostream>

<iostream>

#include
//

/* */

/* */

#if 0
.....
#endif

int main() {...}


main int

return 0;
0 0 main()

main() main()

double a;

double

double b;

"

u8

u8"Το τετράγωνο είναι"

main()
'a' '1'

'\n'

u8

std::cin std::cout

std::cout << u8"Δώσε␣πραγματικό␣αριθμό:␣";

std::cout << b;

std::cin >> a;

<<

std::cout
>> std::cin

<iostream>
\n

std::cout << b << '\n';


std::cout
<<
std::cin std::cout <iostream>
std::cerr

<<

b = a * a;

*
+= == << >>
/* //

//

#include <iostream>
main(){std:cout << 'Hello␣World!\n'}
int k;

decltype(a)

decltype

int i;
decltype(i) j;
std::array<>

=
0

auto

auto
std::initializer_list<>

auto i=1, j=5;

=
auto

alignas alignof and and_eq asm


auto bitand bitor bool break
case catch char char16_t char32_t
class compl const constexpr const_cast
continue decltype default delete do
double dynamic_cast else enum explicit
extern false float for friend
goto if inline int long
mutable namespace new noexcept not
not_eq nullptr operator or or_eq
private protected public register reinterpret_cast
return short signed sizeof static
static_assert static_cast struct switch template
this thread_local throw true try
typedef typeid typename union unsigned
using virtual void volatile wchar_t
while xor xor_eq

0 9
ena␣lathos␣onoma

timi, value12, ena_onoma_me_megalo_mikos, sqrt, New

double b, c;
b = 3.2;
c = 5.5;
c = c + 2.0 * b;
b=0
5.5+2.0*3.2 11.9

int a, b;
b = a = 3; //First a = 3; then b = 3;
int c = (b = 4) + a; // First b = 4; then c = 7;

int
short int long int long long int signed
unsigned float
double long double bool
char signed char
unsigned char wchar_t char16_t char32_t
void

<cstdint>
double_t float_t <cmath>

bool

int

int i;
short int int long int
long long int 16 16 32 64 bit
short int
using
int
long int long long int
char
sizeof()
signed unsigned

signed

int ! "
16 bit −215 , 215 − 1 =
[−32768, 32767]

std::numeric_limits<int>::min()

std::numeric_limits<int>::max()

<limits>

#include <limits>
#include <iostream>

int main()
{
std::cout << std::numeric_limits<int>::min() << '\n';
std::cout << std::numeric_limits<int>::max() << '\n';
}

int
<cstddef> std::size_t
using

short int long int long long int short long


long long signed int unsigned int signed
unsigned
3 12 123456

0 x X

0x 0X

0b 0B
0 1

1'234'567'890

long int
long long int 12l long int
9223372036854775805L long long int

long long int

unsigned int
unsigned long int unsigned long long int

unsigned long int unsigned long long int

unsigned long long int

int long int long long int

short int

+ -
float
double
long double

double g;
float
double
long double

double

std::numeric_limits<double>::digits10

std::numeric_limits<double>::min()
std::numeric_limits<double>::max()
<limits>
double
float
double long double

double
double

2−1023 ≈ 10−308 21024 ≈ 10308

0 9

std::numeric_limits<>::min()
0 9

10

±yyy

×10±yyy

double
float long double

2.034 .44 23.

double

double

2E-4 ≡ 0.0002 2.3e2 ≡ 230.0

32.3f 3E-3F

float

1.234L 0.02E-2L 7E3L

long double

12.3456789
12.345′ 678′ 9

+ -
auto pi = 3.14159265358979323846f;
π 21 float

std::cout.precision(21);
21
6

auto a = 3.14159265358979323846f - 3.1415927f;

bool

bool a;
true false

false
true

bool a{true};

bool
bool

true 1
false 0
true 0 false
nullptr true nullptr false

true

char
char c;

char
signed unsigned char
signed char unsigned char

char

char
8 bit

CHAR_BIT <climits>
char

'\0' 0
'0' 48

char c{'a'};

'a' '
"
char

char

'a'
97

char const *
char newline{'\n'};
char bell{'\a'};
char Alpha{'\x61'}; // Alpha = 'a' in ASCII, hex
char alpha{'\141'}; // alpha = 'a' in ASCII, octal

?
"

std::cout << "This\nis\ta\ntest\nShe␣\bsaid:␣\"How␣are␣you\?\"\n";


wchar_t

char16_t char32_t
16 32 bit

void

void * void

enum class

enum class Color {red, green, blue};


Color

Color c{Color::red};
case
switch

0 0
1 2

enum class Color {red, green=5, blue}; // red=0, green=5, blue=6

int k{static_cast<int>(Color::green)};
5

enum class
Color g{static_cast<Color>(6)}; // g is Color::blue

class

enum

constexpr

enum class int


double constexpr pi{3.141592653589793};
auto constexpr maximum = 100; // maximum is int

constexpr

const

constexpr const

goto

#include
#include <iostream>

int main()
{ // begin block A
double x{3.2};
{ // begin block B
int x{5};
std::cout << x; // prints 5
} // end block B
std::cout << x; // prints 3.2
} // end block A

+ -

+ - *

%
int a{5};
int b{3};
int p{a/b}; // Piliko: p ← 1
int y{a%b}; // Ypoloipo: y ← 2

std::pow() <cmath>

xa std::pow(x,a)
#include <cmath>

std::cout << 0.1+0.2-0.3 << '␣' << 0.1-0.3+0.2 << '\n';

=
a = a + b;

a += b;

+= -= *= /= %= =

1
++ --

b = ++a + c;
1
b = a++ + c;

1 1 --

b = --a + c;

a = a - 1;
b = a + c;

b = a-- + c;

b = a + c;
a = a - 1;
++ --

a--b (a--)b
a-(-b)

()

::
.
->

[]
()
{}

static_cast
const const_cast
++ --

sizeof
sizeof()
++ --
~
!
+ -
&
*

.*
->*

*
/
%

+ -

>> <<

< >
<= >=

== !=

&

&&
||

?:

{}
=
*=
/=
%=
+=
-=
<<=
>>=
&=
^=
|=

int double
int double

int bool char


short int int
int

bool char

?:
int a = 3.14; // a is 3
short int b = 12121212121.3; // b = ??

static_cast<>
int sum{2 + 3 + 5};
int N{3};
double mean1{sum / N}; // Wrong value
double mean2{static_cast<double>(sum) / N}; // Correct value

static_cast<>
static_cast

static_cast<>
static_cast<>
double mean2{1.0 * sum / N};

double mean2{(sum + 0.0) / N};

sizeof

sizeof
int a;
std::cout << sizeof(int); //parentheses are necessary
std::cout << sizeof(a);
std::cout << sizeof a;

sizeof

sizeof std::size_t

enum class
~
0 1
compl
<< >>

<<
0 >> unsigned
=

char
~ ~expr
<< expr1 << expr2
>> expr1 >> expr2
& expr1 & expr2
^ expr1 ^ expr2
| expr1 | expr2
<<= expr1 <<= expr2
>>= expr1 >>= expr2
&= expr1 &= expr2
^= expr1 ^= expr2
|= expr1 |= expr2

& ^ |

bitand xor bitor


= &= ^= |=

and_eq xor_eq or_eq

std::bitset<>
std::vector<> bool std::vector<bool>

bool
,

auto a = -1;
auto b = 1;
std::cout << a, b << '\n';
std::cout << (a, b) << '\n';

double sqrt(double x)

std::sqrt()
<cmath>

y=std::sqrt(x);

#include <iostream> // for std::cin, std::cout


#include <cmath> // for std::sqrt

int main()
{
double x;
std::cin >> x;
auto y = std::sqrt(x);

'\n'
10
std::cout << y << '\n';
}

<complex>
#include <complex>

<complex>
std::complex_literals
i
std::complex<double> 0
i if il
1i
0.0 + 1.0i
using namespace std::complex_literals;

double
std::complex<double> z; // z = 0.0 + 0.0 i
double
float long double
0 + 0i

std::array<>

std::complex<double> z{3.4,2.8}; // z = 3.4 + 2.8i

std::complex<double> z(3.4,2.8);
0
std::complex<double> z{3.41}; // z = 3.41 + 0.0i

std::complex<double> z(3.41);

std::complex<double> z = 3.41;

0
std::complex<double> z{1.2i}; // z = 0.0 + 1.2i

std::complex<double> z1{3.41}; // z1 = 3.41 + 0.0i


std::complex<double> z2{z1}; // z2 = z1;
auto z3 = z2; // z3 = z2;

+ - * / += -= *= /=

std::abs()

<complex>

std::abs()
z = α + iβ
std::complex<double>
std::vector<double> double
<complex>
√ #
std::abs(z) zz⋆ → α2 + β 2

std::polar()

0 std::polar(r,t)
z = reit std::polar(r)
z = rei0 ≡ r(1 + 0i) = r

std::complex<double> z1{std::polar(2.0,0.75)}; // z1 = 2 exp(0.75i)


auto z2 = std::polar(2.0); // z2 = 2 exp(0i) = 2.0 + 0.0i

std::norm()
z = α + iβ

std::norm(z) zz⋆ → α2 + β 2

std::arg()
z = α + iβ

std::arg(z) → tan−1 (β/α)

std::conj()
z = α + iβ

std::conj(z) → α − iβ

std::proj()

std::real()
z = α + iβ

std::real(z) → α

real() std::complex<>
z.real()

std::imag()
z = α + iβ

std::imag(z) → β

imag() std::complex<>
z z.imag()
real()
imag()
std::complex<double> z{3.0,1.5}; // z = 3.0 + 1.5i
z.real(4.2); // z = 4.2 + 1.5i
z.imag(-0.9); // z = 4.2 − 0.9i

<<

std::complex<double> z{3.2,1.5};
std::cout << z;

(3.2,1.5)

>>

std::complex<double> z;
std::cin >> z;

char
std::string <string>
std::string
std::string s;
std::string

std::string s{};
std::string

std::string s{'a','b','c','d'}; // s <- "abcd"

std::string s{u8"Δώσε αριθμό:"}; // s <- "Δώσε αριθμό:"

std::string s{"Give␣number", 6}; // s <- "Give n"

u8

char

string
std::string s1{"Giver␣number:"};
std::string s2{s1}; // s2 <- "Give number:"

string
std::string s1{"Give␣number:"};
std::string s2{std::move(s1)};
// s2 <- "Give number:", s1 undefined

string
std::string s1{"Give␣number:"};
// copy all chars from position 3
std::string s2{s1,3}; // s2 <- "e number:"

string
std::string s1{"Give␣number:"};
// copy 5 bytes from position 3
std::string s2{s1,3,5}; // s2 <- "e num"

int n{6};
std::string s(n, '*'); // s <- "******"

= string
string

std::string s{"in"}; // s <- "in"


s = "out"; // s <- "out"
+ string string
string
std::string s1{"Give"};
std::string s2{"number:"};
std::string s3 = s1 + '␣' + s2; // s3 <- "Give number:"
= string
string
std::string s{"Give"};
s+= '␣'; // s <- "Give "
s+="number:"; // s <- "Give number:"
[]
string
0
std::string s{"Give␣number:"};
std::cout << s[0] << '\n'; // 'G'
std::cout << s[10] << '\n'; // 'r'
>> <<
std::cin std::cout string
std::string

std::string std::wstring
std::u16string std::u32string
wchar_t char16_t char32_t

using

using

using

std::complex<double>
complex

using complex = std::complex<double>;

complex z;

using

using integer = int;


integer a; // Correct: a is int
long integer b; // Error: b is not long int
int
long int
using

double

double float
real

using real = double;


float

using real = float;


using

using typedef

typedef

using
typedef
using

namespace
namespace onoma {
...
double a;
...
}

namespace
namespace
onoma::a namespace

namespace

namespace

n1::n2::a

cin cout complex<>


std::

using namespace std;

std::
#include <iostream>
#include <complex>

int main()
{
using namespace std; // "std::" not needed below
complex<double> a{2.4,3.7};
cout << a << '\n';
}
using namespace std;
namespace

using std::cout;
cout
#include <iostream>
#include <complex>

int main()
{
using std::cout;
std::complex a{2.4,3.7}; // std:: necessary
cout << a << '\n'; // std:: not necessary
}

namespace {
...
}

namespace
#include

namespace

main()

int a;

int & r{a};


&

int a;
int & r{a};
a = 3; // r = 3
r = 2; // a = 2
int b{a}; // b = 2
int c{r--}; // c = 2, a = 1

const constexpr
const
int constexpr a{5};
int & p{a}; // Error
int const & q{a}; // Correct. Value of a cannot change through q.

std::numeric_limits<double>::digits10
<limits>

#include <limits>
#include <iostream>

int main()
{
auto const & digits = std::numeric_limits<double>::digits10;
std::cout << digits << '\n';
}

int const & p{4};


int a{3};
int const & q{a};
int const & r{2*a};

int a{3};
int b{4};
int c{a+b};
a+b

int a{3};
int b{4};
int c{a+b};
int && d{a-b};
d = 6;
&&

a-b

std::move() <utility>

auto c = a;
a = b;
b = c;

auto c = std::move(a);
a = std::move(b);
b = std::move(c);
auto x = y;
auto z = std::move(y);

x = y;
z = std::move(y);

std::move()

&

int a{3};
&a
int
int * p{&a};

*
*p
*p
double r{5.0};
double * q{&r};
*q = 3.0; // r becomes 3.0

int a{4};
int * p;
p = &a;
int * q{p};

*q *p

void

int a{5};
int * p{&a};
void * t{p};
void int
*
*
void static_cast<>

int * v{static_cast<int*>(t)};
*v = 4;
4
void *
static_cast<>

*
nullptr

nullptr

int a;
int * p1{&a};
int const * p2{&a};
int * const p3{&a};
int const * const p4{&a};
int * & p5{p1};
int const * & p6{p2};
int * const & p7{p3};
int const * const & p8{p4};

*p2

*p3
double x{1.2};
double * const p{&x};
double y{0.1};
p = &y; // error
double const * q{&x};
*q -= 0.2; // error
int const a{2};
int * r{&a}; // error

*p
++ --

sizeof
sizeof(T)
p+2
sizeof(T)
+= -=
sizeof(T)

sizeof(T)
int a{4};
int *p{&a};
int *q{&a+10};
auto k = q - p; // int k = 10;
void

std::ptrdiff_t <cstddef>
void * void

<cstdlib>

std::rand() <cstdlib>

std::rand()
RAND_MAX RAND_MAX
<cstdlib>

#include <cstdlib>

int main()
{
auto r = std::rand(); // r is random in [0,RAND_MAX]
... // use r
}
std::rand()

std::rand() std::srand() <cstdlib>


unsigned int

std::time() <ctime> nullptr

std::time()
1 1970
std::rand()

std::srand(std::time(nullptr));
[m, n] m n

= αr + β α
β

int x=m + std::round(static_cast<double>(n-m)/RAND_MAX*r);

[m, n]

int x{m + r % (n-m+1)};

std::rand()
static_cast<double>(r) / RAND_MAX [0, 1]

[a, b]
double x{a + static_cast<double>(std::rand()) / RAND_MAX * (b-a)};

<random>
<random>

[5, 20]

std::default_random_engine
std::uniform_int_distribution<int>
d(e)

#include <random>

int main()
{
std::default_random_engine e{};
std::uniform_int_distribution<int> d{5, 20};

auto r = d(e); // random


... // use r
}

std::random_device

#include <random>

int main()
{
std::random_device rd{};
std::default_random_engine e{rd};
std::uniform_int_distribution<int> d{5, 20};

auto r = d(e); // random


... // use r
}

std::uniform_int_distribution<int> <random>

[a, b)
std::uniform_real_distribution<double>
std::uniform_real_distribution<double> d{a,b};

b b>a

std::normal_distribution<double>

std::normal_distribution<double> d{m,s};
#include <iostream>

int main()
{
std::cout << u8"Δώσε δυο ακέραιους\n";
int a, b;
std::cin >> a >> b;
int c{a + b};
std::cout << u8"Το άθροισμά τους είναι: " << c << '\n';
}

4 · 103 10−2 3/2

x
y z

x2 + y 2 + z 2
x2 /y + z
2.45(x + 1.5) + 3.1(y + 0.4) + 5.2 − z/2
(12.8x + 5y)/(11.3y + 4z)
x2/3 + y 2/3 + z 2/3

x = 1.5 y = 2.5 z = 3.5


20.75 4.4 19.79 0.7502958579881658 5.457604592453865
std::cout.precision(16);
16
Γ = 2πR
E= πR2

a b c a2 +b2 = c2

m n m>n
a= m2 − n2 b = 2mn c = m2 + n2

10

200000

30 360
2034938471
2099

r1 19
r2 4
r3 7
ra = 19r1 + 16
r4 ra 30
rb = 2(r2 + 2r3 + 3r4 )
r5 rb 7
rc = r4 + r 5
rc

rc

5^2

int m{217};
int n{813};

m ^= n;
n ^= m;
m ^= n;

80 cm
cm
km m cm

1253
100 240 cm 1 km 2 m 40 cm

π = cos−1 (−1) ,
π = 8 tan−1 (1/3) + 4 tan−1 (1/7) ,
√ √
4 sin(π/12) = 6− 2,
tan(π/2) = ∞ .

π
∞ = 1/0
π 3.14159265358979323846264338 . . .

15
std::cout.precision(16); 16

q1 q2
(x1 , y1 , z1 ) (x2 , y2 , z2 )

q1 q2 x2 − x 1 q1 q2 y2 − y1 q 1 q 2 z 2 − z1
Fx = , Fy = , Fz = ,
4πϵ0 d3/2 4πϵ0 d3/2 4πϵ0 d3/2

d = (x2 − x1 )2 + (y2 − y1 )2 + (z2 − z1 )2 ϵ0


8.854 × 10−12 π ≈ 3.14159
if switch

x>10 a!=b

> >=
< <=
== !=

true false 3>2 true


2!=1+1 false
==
!= != not_eq

!
&&
||

not and or
&& ||

!
!

!(4 > 3) false


!(4 < 3) true

&& true true


false
(4 > 3) && (3.0 > 2.0) true
(4 < 3) && (3.0 > 2.0) false
||
true true
false
(4 > 3) || (3.0 < 2.0) true
(4 < 3) || (3.0 < 2.0) false
&& ||

&& ||
||

&&
|| &&

bool
bool a{3==k};
auto b = ( i > 0 && i < max );

(i < 0) || (i > max)

true

(i >= 0) && (i < max)

false
i < max
if

else

if (val > max)


max = val;
else {
max = 1000.0;
++i;
}
else

if
if
else else if

if (i == 0)
if (val > max)
max = val;
else
max = 10;
max = 10;
(i == 0) (val > max) (i == 0)

if (i == 0) {
if (val > max)
max = val;
}
else
max = 10;

if

bool

if
else
if (int j = 3) {
max = 10 + j;
}

3 true
if

if (j = 3) {
max = 10 + j;
}
3 true

if (j == 3) {
max = 10 + j;
}

?:

val = (condition ? value1 : value2);

if (condition) {
val = value1;
} else {
val = value2;
}

(k == 5 ? a : b) = 3;
5
a=3;
b=3;

= += -= *=
/= ?: =

a = b ? c : d a = (b ? c : d)
a ? b : c = d a ? b : (c = d)
true

switch
if
switch (i) {
case value1:
...
case value2:
...
...
case valueN:
...
default:
...
}
bool
enum class

enum class

case
case default break
goto return throw
switch

default
switch
case default

#include <iostream>

int main()
{
double a, b, res;
char c;
std::cin >> a >> b;
std::cin >> c;

switch (c) {
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
std::cerr << "wrong␣character\n";
return -1;
}
std::cout << "the␣result␣is␣" << res << '\n';
}
case
0 1 2
4 5
switch (i) {
case 0:
case 1:
case 2:
....
break;
case 4:
case 5:
....
break;
}
2 0

ax = b
a, b
a, b a ̸= 0
a=0 b=0 a=0 b ̸= 0

ax2 + bx + c = 0 a, b, c
abc

2015

48000 20000
22 4400 10000
29 2900 10000
37 3700 8000
45 3600 14600
d 1, 2, 3, . . . , 31 m 1, 2, . . . , 12 y
1 2
m 12 1 y

a 13(m + 1) 5
j, k
100
b j 4
c k 4
h a b c d k
j

h 7 0
1

31
30
28
29
4

400 4
100 400
16
1923 1 1923
1923 15

switch
1
2 3 4 5

std::cout << 1 << '\n';


std::cout << 2 << '\n';
std::cout << 3 << '\n';
std::cout << 4 << '\n';
std::cout << 5 << '\n';

500

int i{1};
std::cout << i << '\n';
i = 2;
std::cout << i << '\n';
i = 3;
std::cout << i << '\n';
i = 4;
std::cout << i << '\n';
i = 5;
std::cout << i << '\n';

1
for
range for while do while for while do while

break
for

for (int i{1}; i <= 5; ++i) {


std::cout << i << '\n';
}

500
for

while do while
int i{1};
while (i <= 5) {
std::cout << i << '\n';
++i;
}

int i{1};
do {
std::cout << i << '\n';
++i;
} while (i <= 5);

for

goto
for

for

for

break return goto


throw
2

for

for

for
for (int i{1}; i <= 5; ++i) {
std::cout << i << '\n';
}
1
5
for

1 9

int s{1 + 3 + 5 + 7 + 9};


for

int s{0};
s += 1;
s += 3;
s += 5;
s += 7;
s += 9;

int s{0};
int i{1};

s += i;
i += 2;

s += i;
i += 2;

s += i;
i += 2;

s += i;
i += 2;

s += i;
for

int s{0};
for (int i{1}; i<=9; i+=2) {
s += i;
}

i+=2;

int i;
for
int s{0};
int i{1};
for ( ; i<=9; i+=2) {
s += i;
}
for 11

for

for

while do while

100 1 100

int s{0};
for (int i{1}; i<= 100; ++i) {
s += i;
}

[0, 1000]

int s{0};
for (int i{0}; i<= 1000; i+=2) {
s += i;
}
99 97 95 3 1

for (int i{99}; i >= 1; i-=2) {


std::cout << i << '␣';
}

0.1 0.2 0.3


double s{0.0};
for (int i{1}; i <= 3; ++i) {
s += 0.1 * i;
}

double s{0.0};
for (double x{0.0}; x <= 0.3; x += 0.1) {
s += x;
}

2 3
[5, 108]
int k{0};
for (int i{5}; i <= 108; ++i) {
if (i%2 == 0 || i%3 == 0) {
++k;
}
}

0
0 1 1 2 3 5 8 13

−2

c=a+b;

int a{0};
int b{1};
std::cout << a << '\n' << b << '\n';
for (int k{2}; k <= n; ++k) {
int c{a+b};
std::cout << c << '\n';
a = b;
b = c;
}

j
$
ak ,
k=i

k i j
ak k

i j
double s{0.0};
for (int k{i}; k <=j; ++k) {
s += ak;
}

ak ak
s
for
0

j
%
ak ,
k=i

double p{1.0};
for (int k{i}; k <=j; ++k) {
p *= ak;

p
1

for

for (auto x : a) {
.... // use value of x
}

auto range for

for
range for

4 5 8 −6
for (auto x : {4,5,8,-6}) {
std::cout << x << '\n';
}

for (auto & x : a) {


std::cin >> x;
}

range for

begin() end() cbegin() cend(),

while

while

while

break return goto throw


1

0
int i;
std::cin >> i;

int s{0};
while (i != 0) {
s += i;
std::cin >> i;
}

do while while

do
while

do while

break return goto throw

int i;
do {
std::cout << u8"Δώσε θετικό ακέραιο: ";
std::cin >> i;
std::cout << '\n';
} while (i <= 0);
break switch
for range for while do while

int i;
while (true) {
std::cout << u8"Δώσε θετικό ακέραιο: ";
std::cin >> i;
std::cout << '\n';
if (i > 0) {
break;
}
std::cerr << "Έδωσες αρνητικό\n";
}
// ... use i

true
do {...} while (true);
for (;;) {...}

continue for
for while do while

10

for (int i{0}; i < 10; ++i) {


double x;
std::cin >> x;
if (x < 0.0) {
continue;
}
std::cout << "Η τετραγωνική ρίζα είναι "
<< std::sqrt(x) << '\n';
}

labelname : statement;

`:'

goto
goto labelname;
goto

goto

catch

goto
goto

switch

goto break
goto
0.0 0.1 0.2
0.3 0.4 2.5

20 2 20 21 219

n! = 1 × 2 × 3 × . . . × n, 0! = 1.

12



⎨ 1 × 3 × 5 × · · · × (n − 2) × n n
n!! = 2 × 4 × 6 × · · · × (n − 2) × n n

⎩ 1 n 0

15

53 [−3.5, 6.5]

n [a, b]
h x1 = a x2 = a + h x3 = a + 2h xn =
a + (n − 1)h xn ≡ b h = (b − a)/(n − 1)

30
f (x) = x(x2 + 5 sin(x)) [−5, 5]

1000
0.5

15

2011 10815197
0.53
15000000
ab

a b

a<b
b=0 a
b > 0
b a b

135 680

6
1 2 3 6 1+2+3+6 = 12 = 2×6

4
444

3
3

fi+2 = fi+1 + fi , i ≥ 0, f0 = 0, f1 = 1 .
0, 1, 1, 2, 3, 5, 8, . . .
n

0 ≤ n < 31

http://en.wikipedia.org/wiki/Euclidean_algorithm
http://oeis.org/A000045
1

18
1930

8/2/2013

17000
26/8/2059
13000 2/7/2015

28/11/1979

1/1/1970 00 00 00
109
231 − 1

9/9/2001 01 46 40 19/1/2038 03 14 07
1950
ex sin x cos x
∞ n
$ ∞
$ ∞
$
x (−1)n x2n+1 (−1)n x2n
ex = , sin x = , cos x = .
n=0
n! n=0
(2n + 1)! n=0
(2n)!

10−10
a
2a = n(n + 1) + m(m + 1) + k(k + 1)
nmk
a
nmk
nmk
n≤m≤k
16 [0, 1, 5] [0, 3, 4] [2, 2, 4] 104 [2, 4, 13]
111 [1, 10, 10] [0, 9, 11]


%
π 4n2
= .
2 n=1 4n2 − 1

104
π/2
http://mathworld.wolfram.com/LagrangesFour-SquareTheorem.html

* + ,-
$ 12 9 π2
cos # =−
n=1
n2 nπ + (nπ + 3)(nπ − 3) e3

10−7


$ 1
ln(5/4) =
k=1
k5k

10−11

π

$ (−1)k
π=3 .
k=0
(k + 1/2)3k+1/2

10−8

π
= lim fn ,
4 n→∞

.
n / 02
1$ k
fn = 1− .
n k=1 n
n fn π/4
fn n = 106 π/4

2
0

p p + 6 p + 12

10000

120
[−100, 100]

<random>


$ k(5k + 3)(2k − 1)!k!
π =3+2 .
k=1
2k−1 (3k + 2)!

π
10−6
10−6

65728

65728 = 123 + 403 = 313 + 333 .

64232

64232 = 173 + 393 = 263 + 363 .

k k = i3 + j 3
i, j i, j, k i≤j<k
1729 = 13 + 123 = 93 + 103
http://oeis.org/A047948
https://en.wikipedia.org/wiki/Taxicab_number
3 = 1 + 2 × 12
5 = 3 + 2 × 12
9 = 1 + 2 × 22 = 7 + 2 × 12
15 = 7 + 2 × 22
27 = 19 + 2 × 22
=

n
1 n 3142

4
[6, 10000]

529 925
910 19

409 + 904 = 1313

100

12 2×6
3×4 2×2×3

1
12 223
13 13

1 b
f (x) dx ,
a

f (x)
a b
(xi , yi ) a ≤ x ≤ b
0 ≤ y ≤ max{f (x)} xi yi max{f (x)}
f (x) [a, b] y = f (x)
yi ≤ f (xi )

a≤x≤b 0≤y≤
max{f (x)}

1 2
x2 dx .
0

<random>

1 2 5 10 20
50 100 =1 200 =2

300
300
150 60 5

300
std::array<>
<array> std::vector<> <vector>

struct
class

int constexpr po2_0{1};


int constexpr po2_1{2};
int constexpr po2_2{4};
int constexpr po2_3{8};
int constexpr po2_4{16};

std::cout << po2_0 << '\n';


std::cout << po2_1 << '\n';
std::cout << po2_2 << '\n';
std::cout << po2_3 << '\n';
std::cout << po2_4 << '\n';

for (int i{0}; i <=4; ++i) {


std::cout << po2_i << '\n';
}

std::array<> std::vector<>
std::array<>

std::vector<>

std::array<>
std::vector<>

std::array<> <array>

std::array

constexpr

constexpr

std::array<double,7> temper;

<array> #include
int constexpr n{5};
std::array<double,n> a, b;
5

std::array<>

0
std::array<char,4> letter{'a', 'b', 'c', 'd'};
// letter[0] = 'a', letter[1] = 'b', letter[2] = 'c', letter[3] = 'd'

std::array<int,5> prime{2,3,5,7}; // prime[4] = 0


std::array<double,10> a{}; // a[0]=a[1]=...=a[9] = 0.0

std::array<int,5> a{1,2,5,7,8};
std::array<int,5> b{a};
auto c = a;

0 −1

0 1 −1
0
−1
std::array<double,7> temper;
temper[0] temper[1]
temper[6]

temper[4] = 13.6;
temper[5] = 15.0;
temper[6] = 16.5;

std::cin >> temper[0];


std::cin >> temper[1];

std::array<double,7> temp;
... // give values to temp
temper = temp;

fill()

temper.fill(12.6);

12.6

double mo3 { (temper[0]+temper[1]+temper[2])/3.0 };

std::cout << "The␣temperature␣on␣Wednesday␣was␣"


<< temper[3] << "␣deg.␣Celsius\n";
std::array<>

std::array<>
std::array<>

15
double a[15];

int b[] {3,4,9,12};

std::array<>

int a[10];
int * p{&a[3]};
int * q{p + 2}; // q == &a[5]

std::array<> std::vector<>
int a[10];
int * p{&a[0]};
*(p+i) a[i]
p+i == &a[i]

a[i]
*(a+i)

double b[10];
double * p{b};
for (int i{0}; i < 10; ++i) {
*p = 1.0;
++p;
}

double b[10];
for (auto p = b; p != b+10; ++p) {
*p = 1.0;
}

sizeof

double a[13];
int k { sizeof(a) / sizeof(a[0]) }; // k == 13
std::array<> std::vector<> <vector>
std::array<>

#include <vector>
#include <iostream>

int main()
{
int D;
std::cin >> D; // get dimension
std::vector<double> v(D);
...
}

std::vector<>
std::array<>
std::vector

std::vector<>
0

std::vector<>
std::array<> v[0] v[1]
std::vector<>
std::vector<>
std::array<>

std::array<>
std::vector<>

std::malloc() std::calloc() std::realloc()


std::free()
<cstdlib>
malloc()/free()

std::malloc()

void
static_cast<>

std::free()
void *

#include <cstdlib>
#include <cstddef>

int main()
{
std::size_t D;
std::cin >> D; // get dimension
auto p = std::malloc(D * sizeof(double));
double * v{static_cast<double *>(p)};

v[0] = ...
v[1] = ...
...
v[D-1] = ...

std::free(p);
}
std::malloc()
sizeof
double

std::free()

new/delete[]
int main()
{
std::size_t D;
std::cin >> D; // get dimension

double * v{new double[D]};

v[0] = ...
v[1] = ...
...
v[D-1] = ...

delete[] v;
}

2 3
(◦C)
1 5.0
2 7.5
3 6.4

157 19.1
158 21.4

364 4.5
365 7.0
1 365

1 2 ... 14 15 . . . 30 31
1 5.0 7.5 . . . 8.3 9.2 . . . 12.3 11.0
2 4.5 6.5 . . . 7.0 9.0 . . .

6 25.0 27.5 . . . 28.3 29.2 . . . 27.3


7 26.0 27.0 . . . 26.0 31.0 . . . 32.5 33.0

11 5.0 6.5 . . . 7.6 10.0 . . . 11.0


12 5.0 7.5 . . . 8.5 9.5 . . . 12.0 12.5

std::array<> std::array<>
std::array<std::array< >, >

12
31
std::array<std::array<double,31>,12> tempr;
<array>

14
tempr[3][13]
0
(i, j) tempr[i,j]
tempr[i,j] j
std::array<double>

std::array<std::array<int,3>,2> b { 0, 1, 2, 3, 4, 5 };

+ ,
0 1 2
b= .
3 4 5

std::array<>

[1][29] [5][30]
12 31

double tempr[12][31];

tempr[3][13] = 16.5;
tempr[i,j] (0, j)
(i, j)

int b[2][3] = { {0, 1, 2}, {3, 4, 5} };

std::array<>

3
4 0, 1, 2 0, 1, 2, 3

0
(0, 0)

(i, j) i+3∗j
M N
k = i+M ∗j
(i, j) k = i∗N +j
M ×N
M ∗N (i, j)
0 1 2 3

0 (0, 0) (0, 1) (0, 2) (0, 3)


0 3 6 9

1 (1, 0) (1, 1) (1, 2) (1, 3)


1 4 7 10

2 (2, 0) (2, 1) (2, 2) (2, 3)


2 5 8 11

i+M ∗j i∗N +j

M ×N
k
k

std::vector<>

[aij ] ×
D1*D2 aij a[i+D1*j]
a[i*D2+j]

[aijk ] × ×
D1*D2*D3 aijk

a[i+D1*(j+D2*k)]

a[i+D1*(j*D3+k)]

a[(i*D2+j)*D3+k]

a[(i+j*D1)*D3+k]
std::array<> std::vector<>

8 6

std::array<double,8*6> b;

5 2 b[4+8*1]

const constexpr

std::array<int,6> constexpr powers_of_two{1,2,4,8,16,32};

std::array<> std::vector<>
size() a.size()
size()
std::vector<>

size() 0
std::array<double,10> a;
for (std::size_t i{0}; i < a.size(); ++i) {
std::cin >> a[i];
}

for (auto & x : a) {


std::cin >> x;
}

5×8

std::array<std::array<int,8>,5> a;
for (sts::size_t i{0}; i < 5; ++i) {
for (std::size_t j{0}; j < 8; ++j) {
std::cin >> a[i][j];
}
}

std::array<int,5*8> a;
for (std::size_t i{0}; i < 5; ++i) {
for (std::size_t j{0}; j < 8; ++j) {
std::cin >> a[i+5*j];
}
}

a[i+D1*j]

std::array<int,5*8> a;
... // give values to a
for (std::size_t k{0}; k < a.size(); ++k) {
std::cout << a[k] << '\n';
}

std::array<int,5*8> a;
... // give values to a
for (auto const & x : a) {
std::cout << x << '\n';
}

a[i*D2+j] a[i+D1*j]
std::string
struct

struct {

};

struct

struct ChemicalElement {
double mass;
int Z; // atomic number
std::string name;
std::string symbol;
};

ChemicalElement hydrogen;
""

ChemicalElement hydrogen{1.008, 1, "Hydrogen", "H"};

ChemicalElement hydrogen = {1.008, 1, "Hydrogen", "H"};

ChemicalElement hydrogen{1.008, 1, "Hydrogen", "H"};


ChemicalElement elem{hydrogen};

ChemicalElement elem = hydrogen;


ChemicalElement elem(hydrogen);

.
.
ChemicalElement oxygen;
oxygen.name = "Oxygen";
oxygen.mass = 15.99494;
oxygen.Z = 8;
oxygen.symbol = "O";
std::cout << "The␣mass␣of␣element␣" << oxygen.name
<< "␣is␣" << oxygen.mass << '\n';

* .
(*p).member

p->member
struct
100
i i = 0, . . . , 99 i2 +3i+1

N
j j = 0, . . . , N − 1 sin(πj/N )

0.4

A B
20×30 (i, j) i = 0, . . . , 19 j = 0, . . . , 29 A
(i + j)/3 B(i, j) 2i − j/3

BT B
A, B T

A · BT

a 100 j j=
0, . . . , 99 cos(πj/100)
50 50 a[0] ↔ a[50] a[1] ↔ a[51]
a[49] ↔ a[99]

0 12 ex

ex ≈ x0 /0! + x1 /1! + x2 /2! + · · · + x12 /12! .

x
std::exp() <cmath>

π

$
1 ((2n)!)3 (42n + 5)
= ,
π n=0 (n!)6 163n+1

15
3.1415926535898
AM ×N BN ×P Aij Bij CM ×p

$
N

Cij = Aik Bkj .


k=1
10

1023 3201 100 1

1000
2

2 1000

n
1, 2, 3, . . . , n

n
i

i
n

M ×N M = 20 N = 60 K = 400
1 0 1

'x' 'o'

9×9
std::cin std::cout
std::cerr std::clog

wchar_t
std::wcin std::wcout std::wcerr std::wclog
<iostream>

<fstream>
ifstream ofstream
std::ifstream inpstr{"filename"};
std::ofstream outstr{"filename"};

std::ofstream outstr{"filename", std::ios_base::app};

std::ofstream outstr{"filename", std::ios_base::trunc};

std::cin
std::cout
double a{10.0};
outstr << a;

char c;
inpstr >> c;

str.close();
str.open("filename");

<sstream>
std::istringstream std::ostringstream
ostringstream
"filename_3.dat"
#include <sstream>

int main()
{
std::ostringstream os;
os << "filename_";
os << 3;
os << ".dat";
// os contains the string "filename_3.dat"
}
str()
ostringstream
std::cout << os.str(); // prints: filename_3.dat

std::ostringstream

std::ofstream outstr{os.str()};
std::istringstream

#include <sstream>

int main()
{
std::istringstream is{"5␣6␣7␣a"};
int i,j,k;
is >> i; // i = 5
is >> j; // j = 6
is >> k; // k = 7
char ch;
is >> ch; // ch = 'a'
}

<<
std::cout << 'a';
std::cerr << "Wrong␣value␣of␣b\n";
std::cin >>

double a;
int b;
std::cin >> a;
std::cin >> b;

<< >>

std::cout << u8"Το άθροισμα του ";


std::cout << a;
std::cout << u8" και του ";
std::cout << b;
std::cout << u8" είναι: ";
std::cout << a+b;
std::cout << '\n';

std::cout << u8"Το άθροισμα του " << a << u8" και του " << b
<< u8" είναι: " << a+b << '\n';

std::cin >> a >> b;


std::cin >> a, b

bool

std::cout << (3==2);


0 true false
std::boolalpha
<ios>
std::cout << std::boolalpha << (3==2);

std::noboolalpha
bool
false

while
int i;
while (inp >> i) {
...
}
inp >> i

true while

get()
EOF eof()
true

while (is.get() != '\n') {}

seekg() seekp()

is.seekg(0);

std::boolalpha std::noboolalpha
bool <ios>

std::noskipws std::skipws

std::skipws
std::noshowpos std::showpos +

std::noshowpos
std::noshowpoint std::showpoint

std::setprecision()
std::noshowpoint
std::scientific d.ddddddedd
std::fixed dddd.dd
std::defaultfloat

std::left std::right
std::setw()

std::right
<iomanip>
std::setprecision()
std::fixed std::scientific

std::defaultfloat std::setprecision()

std::setw()

0
std::setfill()
std::setw()

std

#include <ios>
#include <iomanip>
#include <iostream>

int main()
{
double b{3.25};
std::cout << b << '\n';
std::cout << std::showpoint << b << '\n';
std::cout << std::noshowpoint; // reset
double a{256.123456789987};
std::cout << "default\t" << a << '\n';
std::cout << "scientific\t" << std::scientific << a << '\n';
std::cout << "fixed\t" << std::fixed << a << '\n';
std::cout << "with␣9␣digits\t" << std::setprecision(9)
<< a << '\n';
}

setprecision() setw() setfill()


precision()
width() fill()
#include <iostream>

int main()
{
std::cout.precision(9);
std::cout << 256.123456789987 << '\n'; // 256.123457
std::cout << std::fixed;
std::cout << 256.123456789987 << '\n'; // 256.123456790
}
0◦ 359.9◦ 0.1◦ 5

x0
(0, 1)
x1
x2 x100
x1 = |(100 ln(x0 )) mod 1| ,
x2 = |(100 ln(x1 )) mod 1| ,

x100 = |(100 ln(x99 )) mod 1| .


a mod 1 a ln(x)
x1 x0
x2 x1
http://bit.
ly/2f4Obpy 126
0
10 0
(0, 1] (1, 2] (9, 10]
http://tinyurl.com/ints201406 1300

2014
[0, 1)
1.0
9.9 0.1 3

i
j
(i, j)

n×n

P (i, 1) = P (1, j) = 1 i j
P (i, j) = P (i − 1, j) + P (i, j − 1) i, j > 1

n=7
2
3
1

1
n

2
100000
1

10000
[−20, 20]

http://tinyurl.com/ints201406 1300

m 7 999999
p q m = p + 2q m p q

r(θ) = 4 cos2 (θ/2) .


1
70 θi i = 1, . . . , 70 0◦ 359◦

θi
r(θi ) (θi , r(θi ))

n 0 ≤ n < 106
137n

http://bit.ly/2bDuQxB http://bit.ly/2bIIhtv
1200 1600

http://tinyurl.com/q8cuydn 4996

http://tinyurl.com/q8cuydn 4996

0 1
0
1
70
512 × 512

1
0

K
256
255

0 K

70

(i, j)
(i, j)
p (i, j)

(i ± p, j ± p) (i, j)

p
http://fla.st/
1KxdatB

K 255

255
K 0 0 K 255
255 0 0
0 K 0 0 0 0 K K K
K K 0
0 K

70

512 × 768

M ×N
K

512 × 512 1000

http://www.math.com/students/wonders/life/life.html
animate
*.ppbm
128 × 128

(64, 64)

90◦

90◦

12000
100

http://mathworld.wolfram.com/LangtonsAnt.html
main()

main()
f (x) = x2 + 5x − 2 .

x2 + 5x − 2
f
x

f (x) f
x x
x2 + 5x − 2
x
f x
y = f (2.5)
y = 2.52 + 5 × 2.5 − 2
x x
auto

->

auto

decltype(auto)

void
return
return

return

void

int a[] int *a

return

main() return
return 0;
return
void
return;

void return
void f(int a)
{
// ...
return;
}

void g(int b)
{
return f(b);
}

return

EXIT_SUCCESS EXIT_FAILURE <cstdlib>


auto

->

#include

read()
#include <iostream>
#include <fstream>
#include <string>

//declarations
double func(double a, double b); // The definition is elsewhere.
int read(double & a, std::string const & fname);
void print(char c);

// definition
int read(double & a, std::string const & fname)
{
std::ifstream file{fname};
file >> a;
return 0; // All ok
}

// definition
void print(char c)
{
std::cout << c << '\n';
}

int main()
{
double x{3.2};
double y{3.4};
double z{func(x,y)}; // Calls func with double, double.

int i{3};
double t{func(x,i)}; // Calls func with double, int.
// int is promoted to double.

print('a'); // Calls a void function.

double r;
read(r, "input.dat");
// Calls function and ignores returned value.
}
#include <iostream>

void add3(double x);

int main()
{
double z{2.0};
add3(z); // z = ???
std::cout << z << '\n'; // z is 2.0
}

void add3(double x)
{
x+=3.0;
}
add3()

2.0 5.0

2.0

void add3(double & x) { x+=3.0; }

void add3(double * x) { *x+=3.0; }

add3(z)

add3(&z)
*x

const

void print(double const a[], int N)


{
for (int i{0}; i < N; ++i){
std::cout << a[i] << '\n';
}
}

void print(std::vector<double> const & a)


{
for (auto const & x : a) {
std::cout << x << '\n';
}
}

const
2
1 × 2 × · · · × (n − 1) × n = (n − 1)! × n , n > 0 ,
n! =
1, n=0.

−1 =1

int factorial(int n)
{
int result;
if (n > 0) {
result = n * factorial(n-1);
}
if (n == 0) {
result = 1;
}
return result;
}

factorial()

factorial(n)→factorial(n-1) →factorial(n-2)→ . . .

?:
int factorial(int n)
{
return (n > 0 ? n * factorial(n-1) : 1);
}

H0 (x) = 1 ,
H1 (x) = 2x ,
H2 (x) = 4x2 − 2 ,
H3 (x) = 8x3 − 12x ,

Hn (x)

Hn (x) = 2xHn−1 (x) − 2(n − 1)Hn−2 (x) , n≥2.

H0 (x) = 1
H1 (x) = 2x
H1 (x)
H2 (x) H3 (x)

Hn (x) x

double hermite(int n, double x)


{
double h;

if (n == 0) {
h = 1.0;
}

if (n == 1) {
h = 2.0 * x;
}
if (n > 1) {
h = 2.0 * (x * hermite(n-1,x) - (n-1) * hermite(n-2,x));
}

return h;
}

const

void f(double x);

void f(double const x);

void f(double * x);


void f(double * const x);
void f(double * const x)
void f(double const * x)

void f(double x); // argument cannot change

void f(double & x); // argument can change

void f(double const & x);//argument cannot change


*xp
void f(double * xp);
// argument cannot change, *xp can change

*xp
void f(double const * xp);
// argument cannot change, *xp cannot change

*xp
void f(double * & xp);
// argument can change, *xp can change

void f(double a[]);


// argument cannot change, a[i] can change

void f(double const a[]);


// argument cannot change, a[i] cannot change

int func(double a, double b = 5.0);


func()

5.0
f (x)
(x, y)
#include <iostream>

double f(double x);

int plot(double low, double high)


{
double const step{(high - low) / 100};
for (double x{low}; x < high; x+=step) {
std::cout << x << '␣' << f(x) << '\n';
}

return 0;
}
plot()
f(x) f(x)
plot()

*
()

double f(double x); // declaration of f(x)

double (*fptr)(double x); // declaration of a pointer

fptr = f; // assignment
double x{1.2};
auto y = f(x);
auto z = fptr(x); // or z = (*fptr)(x);
// y == z

int plot(double low, double high, double (*f)(double x))


{
double const step{(high - low) / 100};
for (double x{low}; x < high; x+=step) {
std::cout << x << '␣' << f(x) << '\n';
}

return 0;
}

f(x) f (x)
(*f)(x)

double mysin(double x);


double mycos(double x);
double mytan(double x);

int plot(double low, double high, double (*f)(double x));

int main()
{
plot(1.0, 5.0, mysin); // plot of mysin
plot(1.0, 5.0, mycos); // plot of mycos
plot(1.0, 5.0, mytan); // plot of mytan
}

int plot(double low, double high, double (&f)(double x))


{
double const step{(high - low) / 100};
for (double x{low}; x < high; x+=step) {
std::cout << x << '␣' << f(x) << '\n';
}
return 0;
}
double mysin(double x);
double mycos(double x);
double mytan(double x);
int plot(double low, double high, double (&f)(double x));

int main()
{
plot(1.0, 5.0, mysin); // plot of mysin
plot(1.0, 5.0, mycos); // plot of mycos
plot(1.0, 5.0, mytan); // plot of mytan
}
using

int func(double a, double b); // target function


using rtype = int (&)(double x, double y);
using ptype = int (*)(double x, double y);
rtype gr{func}; // reference
ptype gp{func}; // declaration with assignment

<functional>
std::function<> <>

int plot(double low, double high, std::function<double (double)> f)


{
double const step{(high - low) / 100};
for (double x{low}; x < high; x+=step) {
std::cout << x << '␣' << f(x) << '\n';
}

return 0;
}

plot()
double mysin(double x);
double mycos(double x);
double mytan(double x);
int plot(double low, double high, std::function<double (double)> f);

int main()
{
plot(1.0, 5.0, mysin); // plot of mysin
plot(1.0, 5.0, mycos); // plot of mycos
plot(1.0, 5.0, mytan); // plot of mytan
}
std::function<>

.h

#include "name.h"

"
<>

<

<
double
// utilities.h
double min(double a, double b); // declaration
double max(double a, double b); // declaration

// utilities.cpp
#include "utilities.h"
// Not necessary but good practice

// definitions
double min(double a, double b)
{
return a<b ? a : b;
}

double max(double a, double b)


{
return a>b ? a : b;
}

#include <iostream>
#include "utilities.h"

int main()
{
double a, b;
std::cout << "Give␣two␣real␣numbers\n";
std::cin >> a >> b;
std::cout << "Max␣is␣" << max(a,b) << '\n';
std::cout << "Min␣is␣" << min(a,b) << '\n';
}

main()
main()
int main() {.....}

int main(void) {.....}


main()

int main(int argc, char* argv[]) {........}

int main(int argc, char** argv) {........}

1 main() 0

char
argv[0]
argv[1] argv[2]
argv[argc] 0

./a.out 12 input.dat output.dat 4.5


main() 5

argv[0] = "./a.out";
argv[1] = "12";
argv[2] = "input.dat";
argv[3] = "output.dat";
argv[4] = "4.5";
argv[5] = 0;
1 4
main()
<cstdlib>
int atoi(char const * p); // C-string to int
long atol(char const * p); // C-string to long int
double atof(char const * p); // C-string to double
strtol() strtod()

#include <cstdlib>
#include <fstream>

int main(int argc, char *argv[])


{
int n{std::atoi(argv[1])};
// n gets the value of the first argument

std::ifstream filein{argv[2]};
// open input file. Name is given by argv[2].

std::ofstream fileout{argv[3]};
// open output file. Name is given by argv[3].

double x{std::atof(argv[4])};
// x gets the value of the fourth argument
....
}

double float long double

bool char short int int float double

int double double long double void*


int float

void swap(int & a, int & b)


{
int const temp{b};
b = a;
a = temp;
}

void swap(float & a, float & b)


{
float const temp{b};
b = a;
a = temp;
}

void swap(double & a, double & b)


{
double const temp{b};
b = a;
a = temp;
}

std::swap() <utility>
...

template <typename T>


void swap(T & a, T & b)
{
T const temp{b};
b = a;
a = temp;
}
template <typename T>

template <typename X, typename Y = double, typename Z = int>


...
<>

double a{2.0};
double b{3.0};
swap<double>(a,b);

swap(a,b)

enum class
bool mult(int a, int b)
{
return !(a%b);
}
mult(a,b)

template<int b>
bool mult(int a)
{
return !(a%b);
}
mult<b>(a)

swap()

swap()
template<>
void swap(int & x, int & y)
{
if (&x != &y) {
x^=y;
y^=x;
x^=y;
}
}
swap<int>(a,b)

typename T

template<typename T1, typename T2>


void f(T1 a, T2 b)
{
...
}

template<typename T1>
void f(T1 a, int b)
{
...
}

template<typename T2>
void f(double a, T2 b)
{
...
}

constexpr

constexpr
constexpr
goto try
constexpr
constexpr
int max(int a, int b)
{
return a > b ? a : b;
}

constexpr
constexpr
int factorial(int n)
{
return (n > 0 ? n * factorial(n-1) : 1);
}

constexpr

constexpr

inline

inline
int max(int a, int b)
{
return a > b ? a : b;
}

inline main()
constexpr
inline

static

void func(double a)
{
static int howmany{0};
// .....
++howmany;
}

func()

static

std::abort()
<cmath>
float double long double

double <cmath>
std

<cstdlib>
std

int abs(int x)

long int long long int

long int labs(long int x)

long int abs(long int x);

long long int llabs(long long int x);

long long int abs(long long int x);

div_t div(int n, int d)

std::div_t
quot rem long int long long int
std::ldiv_t std::lldiv_t

ldiv_t ldiv(long int n, long int d)

ldiv_t div(long int n, long int d);

lldiv_t lldiv(long long int n, long long int d)

double
lldiv_t div(long long int n, long long int d);

<cinttypes>
intmax_t imaxabs(intmax_t x);
imaxdiv_t imaxdiv(intmax_t n, intmax_t d);
std::intmax_t <cstdint>
std::imaxdiv_t std::intmax_t quot
rem
<cmath>

double cos(double x) rad


double sin(double x) rad
double tan(double x) rad
double acos(double x) [−1, 1]
[0, π] rad
double asin(double x) [−1, 1]
[−π/2, π/2] rad
double atan(double x) [−π/2, π/2]
rad
double atan2(double x, double y) tan−1 (x/y)

[−π, π] rad

double cosh(double x)
double sinh(double x)
double tanh(double x)
double acosh(double x) ≥ 1

double asinh(double x)
double atanh(double x) (−1, 1)

double pow(double x, double a) xa a>0 x=0


x<0
double sqrt(double x)
double cbrt(double x)
double hypot(double x, double y) x2 + y 2
#
<cmath>
double exp(double x) ex
double exp2(double x) 2x
double expm1(double x) 1 ex − 1
exp(x)-1
double log(double x) ln x
double log2(double x) log2 x
double log10(double x) log10 x
double log1p(double x) 1+x
log(1+x)
double modf(double x, double* p) *p
double frexp(double d, int* p) [0.5, 1) d = x 2y *p
double ldexp(double d, int i) d 2i
double ceil(double x)
double floor(double x)
double trunc(double x)
double round(double x) 0.5
long int lround(double x) 0.5
long long int llround(double x) 0.5
<cmath>

double fmod(double x, double y) 0

double remainder(double x, double y) 0.5

double remquo(double x, double y, int* q) std::remainder(x,y)

*q
2
double erf(double x) erf(x) = √2π 0 e−t dt
double erfc(double x) erfc(x) = 1 − erf(x)
3x

double tgamma(double x) Γ(x) Γ(x) = 0


tx−1 e−t dt
double lgamma(double x)
3∞

Γ(x)

double fma(double x, double y, double z) x·y+z

x · y + z

float
long double
double copysign(double x, double y)
double fmin(double x, double y)
double fmax(double x, double y)
double fdim(double x, double y)
double fabs(double x)
double abs(double x)
2 /2
x e−x

x = 0.3 1.4 5.6

3! 5! 7!

(3.2, 5.6, 8.1)

89 261 1511

std::vector<>

std::vector<>

std::vector<>

std::vector<>
4
i a i bi a b

[a, b]

n
n xi , y i 1 ≤ x i ≤ 1 1 ≤ yi ≤ 1

x2 + y 2 = 1 2
n n = 103 n = 104
n= 109 n
2
2
http://tinyurl.com/114rndint

5744 6789 2774


(x, y, z)
(r, θ, φ)
5 / 5 0
−1
r= x2 + y2 + z2 , θ = cos z/ x2 + y2 + z2 , φ = tan−1 (y/x) .

(3.5, 2.5, −1.0) (0.0, 1.5, −2.0)


(x, y)
(r, θ)
5
r= x2 + y 2 , θ = tan−1 (y/x) .

x = r cos θ , y = r sin θ .

4 x y r θ

r(θ) = esin θ − 2 cos(4θ) + sin5 [(2θ − π)/24]


ri = r(θi ) θi 0◦ 2◦ 4◦ 358◦
(xi , yi )
(ri , θi )

π
N
N
4 $ 1
p(N ) = / 02 .
N k=1 k− 12
1+ N

limN →∞ p(N ) = π
π
N = 1, 2, 10, 50, 100, 500
N
π e(N ) = |p(N )−π|
Nmin e(Nmin ) < 10−6

K 1024 = 210
2

K = d9 29 + d8 28 + · · · d1 21 + d0 20 .

d9 d8 d1 d0
K

10
d0 d9

81 833 173

k
k +1 2
10000

ex

ex ≈ x0 /0! + x1 /1! + x2 /2! + · · · + x12 /12! .

ex x = 0.5 1.2 4.1

sin x ≈ x1 /1! − x3 /3! + x5 /5! − x7 /7! + x9 /9! − x11 /11! .

35◦

ex sin x
cos x
∞ n
$ ∞
$ ∞
$
x (−1)n x2n+1 (−1)n x2n
ex = , sin x = , cos x = .
n=0
n! n=0
(2n + 1)! n=0
(2n)!
10−10

http://bit.
ly/2f4Obpy 126

7.6 3.2 9.1

std::vector<>

200 http://tinyurl.com/q8cuydn

2 + 3i 5.7 − 9i

C F F =
9/5 C + 32

22 ◦C
0 −273.15 ◦C
6000 ◦C
100 ◦ F

ax2 +
bx + c = 0
a b c

http://tinyurl.com/114matrixA http://tinyurl.com/114matrixB

(0, 0) (0, 1) (1, 0) (1, 1)

⎛ ⎞
2.3 − i 1 − 7i 5.8 −2.9 − 3.7i
⎜ −4.9i i 9 − 0.3i −2 + 0.72i ⎟
⎜ ⎟
⎜ ⎟
⎝ 8.2 + 4i −0.8 + i 0.2 + 5i 9 − 3i ⎠
2.3i −7.1 + 9i 0.9 −4i

x(t) = A cos(ωt) + B sin(ωt)


A = 3 cm B = 2 cm ω = 12 Hz

t
x(t)

t x(t) 4
t = 0.0 0.5 1.0 100.0 s
2
(n − 1)! × n , n > 0 ,
n! =
1, n=0.

n
n
15

2
4096 65534 1855932
2

10

19791 4774

1000000
57960
2 6
0
http://tinyurl.com/ints201411 3590

2, 8, 8, 3, 5, 5, 5, 8, 8, 1, 6, 7, 7, 7
7

⃗r = (x, y, z) θ

⃗r
< <
<1 0 0 <
< <
< <
Rx (θ) = <0 cos θ − sin θ <,
< <
<0 sin θ cos θ <

⎛ ⎞ < <⎛ ⎞
x′ <1 0 0 < x
< <
⎜ ′ ⎟ < <⎜ ⎟
⎝ y ⎠ = <0 cos θ − sin θ <⎝ y ⎠ .
< <
z′ <0 sin θ cos θ < z

ŷ ẑ
< <
<cos θ 0 − sin θ <
< <
< <
Ry (θ) = < 0 1 0 <
< <
< sin θ 0 cos θ <

< <
<cos θ − sin θ 0 <
< <
< <
Rz (θ) = < sin θ cos θ 0 <.
< <
< 0 0 1 <

θ
x = 0.5 y = −0.3 z =
1.2 30◦
ŷ 35◦ x̂
58◦ ẑ

Hn (x) n 0 1

H0 (x) = 1
H1 (x) = 2x
H2 (x) = 4x2 − 2
=

Hn (x) = 2xHn−1 (x) − 2(n − 1)Hn−2 (x) , n≥2.

n
x
Hn (x)
yn (x)
n 0, 1, . . .

y0 (x) = 1
y1 (x) = x + 1
y2 (x) = 3x2 + 3x + 1
=

yn (x) = (2n − 1)xyn−1 (x) + yn−2 (x) n≥2,


2 ′
x yn (x) = (nx − 1)yn (x) + yn−1 (x) n≥1,
y0′ (x) = 0.
n
x
yn (x)
yn (x)
x ̸= 0

m V = kx2 /2

. +√ ,1/4
1 km 2 /2
ψn (y) = n
√ Hn (y)e−y ,
2 n! π h̄
5√
y=x km/h̄

ψψ ∗

nx m = k = h̄ = 1

n = 5 60 x [−6, 6]
x x ψψ ∗

Pℓ (x) x ∈ [−1, 1] ℓ 0 1
P0 (x) = 1 P1 (x) = x

ℓPℓ (x) = (2ℓ − 1)xPℓ−1 (x) − (ℓ − 1)Pℓ−2 (x) .


x
Pℓ (x)

http://tinyurl.com/114rndint
N

1000

xi+1 = |(100 ln(xi )) mod 1| ,

x0 = 0.1 a mod 1 a
xi [0, 1)

A N ×N
N
$
det A = (−1)i+j aij det A=ij ,
i=1

j 1 A i j
aij A=ij A
i j
x

ψnℓm (r, θ, φ) = Rnℓ (r)Yℓm (θ, φ) .

.
2ℓ + 1 (ℓ − m)! m
Yℓm (θ, φ) = P (cos θ)eimφ .
4π (ℓ + m)! ℓ

Pℓm (x)

ℓ=m

Pℓm (x) = (−1)m 1 × 3 × 5 × · · · × (2m − 1) (1 − x2 )


m/2
,
http://mathworld.wolfram.com/CliffRandomNumberGenerator.html

http://mathworld.wolfram.com/DeterminantExpansionbyMinors.html

http://mathworld.wolfram.com/CramersRule.html
ℓ=m+1
Pℓm (x) = x(2m + 1)Pm
m
(x) ,

(ℓ − m)Pℓm (x) = x(2ℓ − 1)Pℓ−1


m m
(x) − (l + m − 1)Pℓ−2 (x) .

θ φ [0, π] [0, 2π)

Pℓm (x)
Yℓm (θ, φ)
50 × 100 θ−φ
Yℓm (θ, φ)
sin θ cos φ sin θ sin φ
∗ (θ, φ)
cos θ Yℓm (θ, φ)Yℓm x y z ψψ ∗
ℓ=2 m=0 d

f (x)
x̄ f (x) f (x̄) = 0

x0 x1 x2

f (x2 ) − f (x0 )
w0 =
x2 − x0
f (x2 ) − f (x1 )
w1 =
x2 − x1
w1 − w0
a = ,
x1 − x0
b = w0 + a(x2 − x0 ) ,
c = f (x2 ) .

2c
x3 = x2 − ,
d
d √ √
b+ b2 − 4ac b − b2 − 4ac
x0 ← x1 x1 ← x2 x2 ← x3

xn q A B C D

f (x) = x3 − x + 1

Γ(z)
> ?z
∞ n 1+ 1
1 % n
Γ(z) = .
z n=1 n + z

/ 0 / 0
1 5 3
Γ Γ = π.
2 2 4

1 10−12

n Jn (x)


$ / 02m+n
(−1)m x
Jn (x) = .
m=0
m!(m + n)! 2

J0 (x) J1 (x)
J2 (x) 150 xi [0, 20]

xi J0 (xi ) J1 (xi ) J2 (xi )

10−12
a
x0
x0 = 1
x3i + 2a
xi+1 = xi
2x3i + a
x1 , x 2 , . . .
x30 + 2a
x1 = x0 ,
2x30 + a
x3 + 2a
x2 = x1 13 ,
2x1 + a

x1 , x 2 , . . . 3
a
xk
|x3k − a| ≤ ε ε 10−12

20.0 20.1 20.2 30.0

20.0 20.1 20.2 30.0


12

3×3 N ×N
N
M ×N 7 6

N N ×N N >3
N

N ×N
0

column[i]

0
1

1
N N

9×9
3×3 1−9

2 3

1 9
x=0 y=0
(x′ , y ′ )
x′ = a · x + b · y + e
y′ = c · x + d · y + f
abcdef

x′ → x y′ → y
(x′ , y ′ )
(x, y)

4
7 6
a, b, c, d, e, f p
4
pi 1
r [0, 1)

0 ≤ r < p1
p 1 ≤ r < p1 + p 2
1000

0 0 0 0.16 0 0 0.01
0.85 0.04 −0.04 0.85 0 1.6 0.85
0.2 −0.26 0.23 0.22 0 1.6 0.07
−0.15 0.28 0.26 0.24 0 0.44 0.07
0 0 0 0.25 0 −0.4 0.02
0.95 0.005 −0.005 0.93 −0.002 0.5 0.84
0.035 −0.2 0.16 0.04 −0.09 0.02 0.07
−0.04 0.2 0.16 0.04 0.083 0.12 0.07

10 × 10

p(z)
z0 p′ (z0 ) ̸= 0
p(z) p(z)

p(zi )
zi+1 = zi − , i = 0, 1, 2, . . . .
p′ (zi )
z0
z1 z2
p(z)
|p(zi )| ≤ ϵ ϵ

p(z) = z 3 − 1 a=
1 b = ei2π/3 c = e−i2π/3
z = 0 + i0

a (255, 0, 0)
b (0, 255, 0)
c (0, 0, 255) 0 + i0
(255, 255, 255)

N = 512
[−1, 1] xi i = 0, . . . , N − 1
M = 512
[−1, 1] yj j = 0, . . . , M − 1
z = xi + iyj

(i, j)
static_assert()
assert() errno

static_assert()
static_assert
int 32767
std::numeric_limits<int>::max()
int
1000000
static_assert(std::numeric_limits<int>::max() > 1000000,
"int␣is␣not␣sufficient");

<cassert>
assert()

assert

assert()
assert()
NDEBUG
<cassert> #define NDEBUG

assert()
0 false

assert(N<10);
(N<10)
std::
math_errhandling
MATH_ERRNO errno
MATH_ERREXCEPT

(MATH_ERRNO | MATH_ERREXCEPT)
math_errhandling MATH_ERRNO MATH_ERREXCEPT
<cmath>

#pragma STDC FENV_ACCESS on

#pragma STDC FENV_ACCESS off

errno <cerrno> EDOM


FE_INVALID <cfenv>
NAN
NAN std::isnan()
<cmath>

errno
ERANGE FE_OVERFLOW
HUGE_VAL HUGE_VALF HUGE_VALL
double float long double
errno ERANGE
FE_DIVBYZERO ±INFINITY
INFINITY std::isinf() <cmath>

errno
ERANGE FE_UNDERFLOW
0.0 0
std::isnormal() <cmath>
0 errno

std::feclearexcept(FE_ALL_EXCEPT);
errno
std::fetestexcept() <cfenv>

true/false

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <limits>
#include <iostream>

#pragma STDC FENV_ACCESS on

int main()
{
if (MATH_ERRNO != 1 && MATH_ERREXCEPT != 2) {
std::cerr << "not␣supported\n";
return -1;
}

errno = 0; // clear error. No error code is 0.


std::feclearexcept(FE_ALL_EXCEPT); // clear exceptions

std::sqrt(-1.0);
// errno becomes EDOM, FE_INVALID is raised

if (errno == EDOM || std::fetestexcept(FE_INVALID)) {


std::cerr << "argument␣out␣of␣domain␣of␣function.\n";
}

errno = 0;
std::feclearexcept(FE_ALL_EXCEPT);

std::pow(std::numeric_limits<double>::max(), 2);
// errno becomes ERANGE, FE_OVERFLOW is raised
if (errno == ERANGE || std::fetestexcept(FE_OVERFLOW)) {
std::cerr << "Math␣result␣not␣representable.\n";
}

errno = 0;
std::feclearexcept(FE_ALL_EXCEPT);

auto y = 1.0/0.0;
// errno becomes ERANGE, FE_DIVBYZERO is raised

if (errno == ERANGE || std::fetestexcept(FE_DIVBYZERO)) {


std::cerr << "Division␣by␣zero.\n";
}
}

std::strerror() <cstring>
errno char *

#include <cerrno>
#include <cmath>
#include <cstring>
#include <iostream>

int main()
{
errno = 0; // clear error
std::cerr << std::strerror(errno) << '\n';

std::sqrt(-1.0); // errno becomes EDOM.


std::cerr << std::strerror(errno) << '\n';
}
std::set<>
std::map<>
std::vector<> std::array<> std::deque<>
std::map<>
std::array<> std::vector<>
<utility> std::pair<T1,T2>

≡int ≡double

std::pair<int, double> p1; // p1 = (0, 0.0)


std::pair<int, double> p2{3, 2.0}; // p2 = (3, 2.0)
std::pair<>

std::pair<int, double> p{3, 2.0};


std::cout << "first␣element␣is␣" << p.first << '␣'
<< "second␣element␣is␣" << p.second <<'\n';
std::get()
0 1
std::pair<int, double> p{3, 2.0};
std::cout << "first␣element␣is␣" << std::get<0>(p) << '␣'
<< "second␣element␣is␣" << std::get<1>(p) << '\n';

std::get<0>(p) = 5; // p = (5,2.0)
std::get()

std::pair<int, double> p{3, 2.0};


std::cout << "first␣element␣is␣" << std::get<int>(p) << '␣'
<< "second␣element␣is␣" << std::get<double>(p) << '\n';
std::pair<>
std::make_pair()
template <typename T1, typename T2>
std::pair<T1, T2>
make_pair(T1 const & f, T2 const & s);
<utility>
std::pair<int, double> p; // p.first = 0, p.second = 0.0
p = std::make_pair(4, 3.0);//p.first = 4, p.second = 3.0
p=q; q.first
p.first q.second p.second

std::tuple<T1,T2,T3,...> std::pair<>
<tuple>
n n = 0, 1, . . .
≡int ≡double ≡int

std::tuple<int, double, int> t1; // {0, 0.0, 0}


std::tuple<int, double, int> t2{3,4.1,-2};
std::make_tuple()
std::tuple<int, double, int> t;
t = std::make_tuple(9,1.2,3);
std::get()
std::tuple<>
0 n−1 n

std::tuple<int, double, int> t{3,4.1,-2};


std::cout << std::get<0>(t) << '␣'
<< std::get<1>(t) << '␣'
<< std::get<2>(t) << '\n';
std::get()

std::tuple<int, double, int> t{3,4.1,-2};


std::cout << std::get<double>(t) << '\n'; // 4.1

std::pair<>
std::tuple<> std::pair<>

<algorithm>
std::min() std::max()
std::minmax()

< std::minmax()
std::pair<>
<
auto a = std::min(3.1,5.5); // a = 3.1
auto b = std::max({12,3,5,17,9});// b = 3
auto c = std::minmax({3,2,9,-1});// c.first = -1, c.second = 9

auto a = std::min<double>(3,4.5); 3.0


<

auto c = std::min(std::abs(a), std::abs(b));

std::min()

bool absless(double a, double b)


{
return std::abs(a) < std::abs(b);
}
std::min()
auto c = std::min(a,b,absless);

<utility>
std::swap()

double a{3.1};
double b{4.2};
std::swap(a,b); // a = 4.2, b = 3.1
std::swap<>
std::complex<>
std::swap()
int a[100];
int b[100];
...
a[15] = 9;
b[15] = 4;
...
std::swap(a,b); // a[15] = 4, b[15] = 9

()

<functional>
std

std::plus<T>
std::plus<int> a;

auto b = a(3,5);
int 8
std::plus<int>{}
std::plus<int>
double() double{} 0.0
()
operator()
()
negate<T>{} −
plus<T>{} +
minus<T>{} −
multiplies<T>{} ∗
divides<T>{} /
modulus<T>{} %
equal_to<T>{} ==
not_equal_to<T>{} !=
less<T>{} <
greater<T>{} >
less_equal<T>{} <=
greater_equal<T>{} >=
logical_not<T>{} !
logical_and<T>{} &&
logical_or<T>{} ||
bit_and<T>{} &
bit_or<T>{} |
bit_xor<T>{} ^

[]

[&a,b]

&
=

mutable

noexcept

->type

return void return

return

[](int x, int y) -> int {


return (std::abs(x) < std::abs(y) ? x : y);
}
auto && fun = [](int x, int y) -> int {
return (std::abs(x) < std::abs(y) ? x : y);
};
;

auto x = fun(-5,3); // x = 3

auto

auto && g = [](auto x, auto y) {return x+y;};

std::cout << g(2,3) << '␣' << g(2.4,3) << '␣'


<< g(2.4,3.4) <<'\n';

5 5.4 5.8

std <functional>

std::bind()
std::bind()
std::minus<int> a;

auto && b = std::bind(a, 10, 2);


std::cout << b() << '\n'; // 10-2 -> 8

auto && c = std::bind(a, std::placeholders::_1, 10);


std::cout << c(3) << '\n'; // 3-10 -> -7

auto && d = std::bind(a, 10, std::placeholders::_1);


std::cout << d(3) << '\n'; // 10-3 -> 7

auto && e = std::bind(a, std::placeholders::_2,


std::placeholders::_1);
std::cout << e(3,5) << '\n'; // 5-3 -> 2
std::bind()

std::bind()

std::placeholders

()
10

10
()

()

std::bind()

std::placeholders::_1

std::placeholders::_2 std::placeholders::_3
using cntr = std::array<int,10>;
cntr a, b;
auto && sizea = std::bind(&cntr::size, a);
std::cout << "the␣size␣of␣a␣is␣" << sizea() << '\n';
auto && size = std::bind(&cntr::size, std::placeholders::_1);
std::cout << "the␣size␣of␣b␣is␣" << size(b) << '\n';

std::mem_fn()

std::mem_fn()
std::bind()
std::mem_fn()

std::array<int,10> a;
auto && size = std::mem_fn(&std::array<int,10>::size);
std::cout << "the␣size␣of␣a␣is␣" << size(a) << '\n';

&
true/false
comp()

comp(a,a)==false

comp(a,b)==true comp(b,c)==true
comp(a,c)==true

comp(a,b)==false comp(b,a)==false
true false

std::bind()

true false

true false

[−2.5, 4.5)

std::mem_fn()

imag() std::complex<>
std::string

&

std::vector<double>
std::vector<double>::iterator it;
c.begin()
c.end()

std::vector<double>
std::vector<double>::iterator b{v.begin()};
std::vector<double>::iterator e{v.end()};

auto

auto b = v.begin();
auto e = v.end();

std::begin() std::end() std::cbegin() std::cend()

auto b = std::begin(v); // b ≡ v.begin()


auto e = std::end(v); // e ≡ v.end()

auto cb = std::cbegin(v); // cb ≡ v.cbegin()


auto ce = std::cend(v); // ce ≡ v.cend()
std::begin() std::end() std::cbegin() std::cend()

<iterator>

std::vector<double>

std::vector<double>::const_iterator it;

cbegin() cend()
begin() end()
iterator const_iterator

const_iterator
std::vector<int> v(100);
std::vector<int>::const_iterator it1{v.begin()};//correct
std::vector<int>::iterator it2{v.cbegin()}; // error

const

std::vector<double> v(100);
auto const it1 = v.begin();
auto const it2 = v.cbegin();

template<typename C, typename T>


void f(C & a)
{
std::vector<T> v;
C b;
...
}
std::vector<>

std::vector<int>

std::vector<T>::iterator itv; // error


C::iterator itb; // error
C::iterator
std::vector<T>::iterator

typename
typename std::vector<T>::iterator itv; // correct
typename C::iterator itb; // correct
*

*it

* end()

->

(*it).member
it->member
*
.

++ --

begin() end()

+= -=

== !=

it1 < it2


3.5

for (auto it = v.begin(); it != v.end(); ++it) {


*it = 3.5;
}

range for
for (auto & x : v) {
x = 3.5;
}
range for

std::vector<double>
for (auto it = v.cbegin(); it != v.cend(); ++it) {
std::cout << *it << '\n';
}

const_iterator std::vector<double>
*it

->

++it it++

auto q = p;
++p;
++q;
bool eq{p==q};
true

istream_iterators

*it = ...
*it
++it it++

ostream_iterators
* ->

std::forward_list<>

std::unordered_set<>

std::unordered_multiset<>

std::unordered_map<>

std::unordered_multimap<>

--

std::list<>

std::set<>

std::multiset<>
std::map<>
std::multimap<>

+= -=

it[n] *(it+n)

std::array<>
std::vector<>
std::deque<>
std::string

T const *
<iterator>

advance()

template<typename InputIterator, typename Distance>


void advance(InputIterator & it, Distance n);

it += n;

++it >0 --it <0

++it

next()

template<typename ForwardIterator>
ForwardIterator next(ForwardIterator it, Dist n = 1);
std::advance()

prev()

template<typename BidirectionalIterator>
BidirectionalIterator prev(BidirectionalIterator it, Dist n = 1);

typename std::iterator_traits<ForwardIterator>::difference_type
std::advance()

distance()

template<typename InputIterator>
Dist distance(InputIterator it1, InputIterator it2);

it2-it1

iterator_traits<>

iterator_traits<>

typename std::iterator_traits<T>::iterator_category

std::output_iterator_tag
std::input_iterator_tag
std::forward_iterator_tag
std::bidirectional_iterator_tag
std::random_access_iterator_tag
typename
std::iterator_traits<BidirectionalIterator>::difference_type
typename std::iterator_traits<InputIterator>::difference_type
typename std::iterator_traits<T>::value_type

typename std::iterator_traits<T>::difference_type

copyodd(a.cbegin(), a.cend(), std::next(b.begin(), 8));

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2)
{
while (beg1 < end1) {
*beg2 = *beg1;
if (beg1 == end1 - 1) {
break;
}
beg1+=2;
++beg2;
}
}
end()

+=
<

#include <iterator>

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2)
{
while (beg1 != end1) {
*beg2 = *beg1;
if (std::next(beg1) == end1) {
break;
}
std::advance(beg1, 2);
++beg2;
}
}

std::advance()
beg1 != end1

beg1 == std::prev(end1)

std::next(beg1) == end1
std::prev()
std::next()

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2)
{
while (beg1 != end1) {
*beg2 = *beg1;
++beg1;
if (beg1 == end1) {
break;
}
++beg1;
++beg2;
}
}

std::random_access_iterator_tag

std::forward_iterator_tag

std::input_iterator_tag
#include <iterator>
template<typename Iterator1, typename Iterator2>
void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2,
std::random_access_iterator_tag name)
{
while (beg1 < end1) {
*beg2 = *beg1;
if (beg1 == end1 - 1) {
break;
}
beg1+=2;
++beg2;
}
}

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2,
std::forward_iterator_tag name)
{
while (beg1 != end1) {
*beg2 = *beg1;
if (std::next(beg1) == end1) {
break;
}
std::advance(beg1, 2);
++beg2;
}
}

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2,
std::input_iterator_tag name)
{
while (beg1 != end1) {
*beg2 = *beg1;
++beg1;
if (beg1 == end1) {
break;
}
++beg1;
++beg2;
}
}
#include <iterator>

template<typename Iterator1, typename Iterator2>


void copyodd(Iterator1 beg1, Iterator1 end1, Iterator2 beg2)
{
typename std::iterator_traits<Iterator1>::iterator_category a;
return copyodd(beg1,end1,beg2,a);
}

copyodd()

a[0] a+5
a[4]
a+5

std::begin() std::end()
<iterator>
std::vector<double>

std::vector<double>::reverse_iterator

std::vector<double>::const_reverse_iterator

rbegin() rend()
reverse_iterator

crbegin() crend()
const_reverse_iterator

++ --

std::vector<double>

for (auto it = v.crbegin(); it != v.crend(); ++it) {


std::cout << *it << '\n';
}
std::vector<double>::const_reverse_iterator
std::cin

std::cin

std::istream_iterator<int> a{std::cin};
std::cin
++ *

std::istream_iterator<int> b{};

std::ifstream in{"data"};
std::istream_iterator<double> beg{in}, end{};
double s{0.0};
for (auto it = beg; it != end; ++it) {
s += *it;
}

std::cout

std::ostream_iterator<double> beg{out, "␣"};

*beg

++
std::istream_iterator<> std::ostream_iterator<>
<iterator>

*a

<iterator> std::insert_iterator<>

std::vector<int> v; // empty vector


v.reserve(10);

std::insert_iterator<std::vector<int>> iend{v,v.end()};
*iend = 1; // v is {1}
++iend;
*iend = 2; // v is {1, 2}
++iend;
*iend = 3; // v is {1, 2, 3}
++iend;

std::insert_iterator<std::vector<int>> ibeg{v,v.begin()};
*ibeg = -1; // v is {-1, 1, 2, 3}
++ibeg;
*ibeg = -2; // v is {-1, -2, 1, 2, 3}
++ibeg;

v.begin()
reserve()

std::inserter() <iterator>

std::vector<int> v;
auto it = std::inserter(v,v.begin());

std::vector<int> c;
it = std::inserter(c, c.end());
std::insert_iterator<> std::inserter()

insert() insert()
std::array<> std::forward_list<>

<iterator>

std::back_insert_iterator<>
std::vector<int> v;
std::back_insert_iterator<std::vector<int>> b{v};

push_back()

std::vector<> std::deque<>
std::list<>
std::back_inserter()

std::vector<int> v;
auto it = std::back_inserter(v);

std::front_insert_iterator<>
std::list<int> c;
std::front_insert_iterator<std::list<int>> b{c};

push_front()
std::deque<> std::list<>
std::forward_list<>
std::front_inserter()

std::list<int> c;
auto it = std::front_inserter(c);

std::move_iterator <iterator>

#include <vector>
#include <iterator>
#include <iostream>

int main()
{
using cntr = std::vector<std::vector<int>>;
cntr a(5, {1,2,3});
cntr b(5);
std::cout << "␣sizes␣of␣a␣before:␣";
for (auto const & x: a) {
std::cout << x.size() << '␣';
}
std::cout << "\b\n";
std::cout << "␣sizes␣of␣b␣before:␣";
for (auto const & x: b) {
std::cout << x.size() << '␣';
}
std::cout << "\b\n";
std::move_iterator<cntr::iterator> beg{a.begin()};
std::move_iterator<cntr::iterator> end{a.end()};
auto bit = b.begin();
for (auto ait = beg; ait != end; ++ait) {
*bit = *ait;
++bit;
}
std::cout << "␣sizes␣of␣a␣after:␣";
for (auto const & x: a) {
std::cout << x.size() << '␣';
}
std::cout << "\b\n";
std::cout << "␣sizes␣of␣b␣after:␣";
for (auto const & x: b) {
std::cout << x.size() << '␣';
}
std::cout << "\b\n";
}
5

std::make_move_iterator()
<iterator>
move_iterator

auto beg = std::make_move_iterator(a.begin());


auto end = std::make_move_iterator(a.end());
4
i αi βi

true false
template<typename Iterator1, typename Iterator2>
Iterator2
uniq(Iterator1 beg1, Iterator1 end1, Iterator2 beg2)
{
}

std::vector<int>
1, 6, 6, 5, 2, 2, 2, 8, 9, 9, 8, 9, 7, 0, 1, 1

1, 6, 5, 2, 8, 9, 8, 9, 7, 0, 1

{2, 2, 3, 4, 4, 2, 5, 6, 7} {9, 8, 7, 6, 4, 2, 5, 4, 3, 2, 1, 2, 4}
6 {2, 3, 4, 5, 6, 7}
array<> vector<> deque<> forward_list<> list<>
std::string

set<> multiset<> map<> multimap<>

unordered_set<>
unordered_multiset<>
unordered_map<>
unordered_multimap<>
std::map<> std::multimap<>
std::unordered_map<> std::unordered_multimap<>

std::array<>

constexpr

std::array<>

<array> array<>

<vector> vector<>

<deque> deque<>

<forward_list> forward_list<>

<list> list<>

<set> set<> multiset<>

<map> map<> multimap<>

<unordered_set> unordered_set<> unordered_multiset<>

<unordered_map> unordered_map<> unordered_multimap<>

std
std::array<>

<T>

cntr<T> c1;

cntr<T> c1;
// fill c1 ....
cntr<T> c2{c1};

cntr<T> c2=c1;
cntr<T> c2(c1);

cntr<T> c1;
// fill c1 ....
cntr<T> c2{std::move(c1)};

cntr<T>

cntr<T> c2=std::move(c1);
cntr<T> c2(std::move(c1));
T v1, v2, v3, ...;
cntr<T> c1{v1, v2, v3, ... };

cntr<T> c1 = {v1, v2, v3, ... };


cntr<T> c1({v1, v2, v3, ... });

cntr<T> c1{beg,end};

double
int int double
std::complex<double>

#include <vector>
#include <set>

std::vector<int> v1{0,2,-2,4,-4,6,-6};
std::vector<int> v2{v1};
std::set<int> s1{v1.cbegin(), v1.cend()};
std::set<int> s2{std::move(s1)};

std::array<>

cntr<T> c1,c2;
...
c1 = c2;

c1 = std::move(c2);

std::move() <utility>

cntr<T> c;
c = {v1,v2,v3, ...};

std::swap() <utility>
cntr<T> c1,c2;
std::swap(c1,c2);

swap()
cntr<T> c1,c2;
c1.swap(c2);

swap()

insert() array<>
forward_list<>
insert_after()

cntr<T> c;
c.insert(pos, elem);

std::move()

insert()

clear()

void

c.clear();

erase()
array<> forward_list<>
c.erase(pos);
c.erase(beg,end);
std::string

const_iterator iterator

const_reverse_iterator reverse_iterator
typedef

value_type

reference

const_reference

pointer

const_pointer

difference_type

size_type

std::vector<double> std::vector<double>::value_type

std::vector<double>::value_type a{v[0]};
double

template<typename C>
void f(C c)
{
typename C::value_type a;
...
}

a.member();

forward_list<>
forward_list<> before_begin()
cbefore_begin()

*
rbegin() rend()

crbegin() crend()
begin()
end()
cbegin()

cend()

rbegin()
rend()
crbegin()

crend()

forward_list<>

array size()

false/true
size()==0

size() max_size() size_type


std::vector<double>

std::vector<double>::size_type n{v.size()};

auto

auto n = v.size();
std::array<>

std::array<>

<array>

array

constexpr

std::array<T,N> a;
std::array<T,N> a1;
// assign values to a1 ...
std::array<T,N> a2{a1};

std::array<T,N> a2 = {a1};
std::array<T,N> a2 = a1;
std::array<T,N> a2(a1);

std::move()

std::array<T,N> a1;
// assign values to a1 ...
std::array<T,N> a2{std::move(a1)};

T v1,v2,..., vN;
std::array<T,N> a{v1, v2, ..., vN};

T{}
0

std::array<T,N> a = {v1, v2, ..., vN};


std::array<T,N> a({v1, v2, ..., vN});
std::array<T,N> a{{v1, v2, ..., vN}};

std::array<int,10> a;
std::array<int,10> b{};
10
0
fill()
std::array<int,10> a;
a.fill(20);

10
20

std::array<int,10> a, b;
a.fill(20);
b = a;

b = std::move(a);

std::array<int,3> a;
a = {-1,-2,-3};

std::swap() <utility>
std::array<int,10> a, b;
a.fill(20);
std::swap(a,b);

swap()
b.swap(a);

swap()
[]
std::array<int,5> a{2,4,6,8,10};
std::cout << a[0] << '\n'; // a[0] = 2
a[3] = a[2]+a[1]; // a[3] = 10

at()
std::array<> a.at[0]
a.at[1] a.at[2]

std::out_of_range

std::array<int,5> a{2,4,6,8,10};
std::cout << a.at(0) << '\n';
std::cout << a.at(10) << '\n';

std::get()

std::array<int,5> a{2,4,6,8,10};
std::cout << std::get<3>(a) << '\n'; // a[3] = 8
std::get<0>(a) = 5; // a[0] = 5

std::get()

front() back()
std::array<int,5> a{2,4,6,8,10};
std::cout << "first␣element␣is␣" << a.front()
<< "␣last␣element␣is␣" << a.back() << '\n';
a.front() = 11; // a[0] = 11
a.back() = 55; // a[4] = 55

*
const_iterator

std::array<int,5> a{2,4,6,8,10};
int * p{&a[1]};
*p=30;
--p; // p ≡ &a[0]
*p=1;

30 1
data()

std::array<int,5> a{2,4,6,8,10};
int * p{a.data()}; // p ≡ &a[0]
p+=2; // p ≡ &a[2]
*p=-1;

−1

std::vector<>

std::malloc() std::realloc() std::free()

std::vector<>

std::vector<>

<vector>

std::vector<>

std::vector<T> v;
std::vector<T> v1;
std::vector<T> v2{v1};
std::vector<T> v3{std::move(v1)};

T a1, a2, a3, ...;


std::vector<T> v{a1, a2, a3, ... };

std::vector<T> v{beg,end};

std::vector<T> v(N);

T{}
0

std::vector<T> v(N, elem);


std::vector<>
size() empty()
max_size()
capacity()

size_type
reserve()
size_type

std::vector<T> v;
v.reserve(N);
push_back()

std::vector<T> v(N);
v[0] v[1] v[N-1]

reserve()

shrink_to_fit()

capacity()
size()
std::vector<T> v1, v2, v3;
...
v2 = v1;
v3 = std::move(v1);

std::vector<T> v;
T a1, a2, a3, ...;
v = {a1,a2,a3, ...};

swap() std::swap()
std::vector<T> v1, v2, v3;
...
v1.swap(v2);
std::swap(v3,v1);

insert()

v.insert(pos, elem);

insert()

v.insert(pos, N, elem);
v.insert(pos, beg, end);
v.insert(pos, {a1, a2, a3, ...});
0 N==0 beg==end

insert()

v.push_back(elem);

v.push_back(elem);
v.insert(v.cend(),elem);

emplace()

std::vector<std::complex<double>> v;
v.emplace(v.cend(), 3.1, 2.1);

3.1 + i2.1

emplace_back()
v.emplace_back(a,b,c,...);
v.emplace(v.cend(), a,b,c,...);

assign()

v.assign(N,elem);
v.assign(beg, end);
v.assign({a1,a2,a3,...});
resize()

v.resize(N);

v.resize(N,elem);

reserve()

std::vector<int> v; // empty vector


v.reserve(4);

v.insert(v.cend(),1); // v is {1}
v.insert(v.cend(),2); // v is {1,2}

auto end = v.cend();

v.insert(end, 3); // v is {1,2,3}


v.insert(end, 4); // v is ? v may be {1,2,4,3}

clear()
v.clear();
erase()

v.erase(pos);
v.erase(beg,end);

resize()

push_back() pop_back()

v.pop_back();

v.erase(v.cend()-1);

vector<> array<>
std::get()

[]

at()
std::vector<> v.at[0]
v.at[1] v.at[2]
[0:v.size())
std::out_of_range

front() back()
*
const_iterator

data()

std::vector<>

#include <iostream>
#include <vector>
#include <cstddef>

int main()
{
std::vector<double> v(9);
// v = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}

// assign some values


for (std::vector<double>::size_type i{0}; i < v.size(); ++i) {
v[i] = 4.0 * i*i;
}

auto v2 = v; // v2 is a copy of v

// append more values to v2


auto siz = v2.size();
for (auto i = siz; i < 2*siz; ++i) {
v2.push_back(4.0*i*i);
}

// print v2
std::cout << "v2␣is\t";
for (auto const & x : v2) {
std::cout << x << '␣';
}
std::cout << '\n';
}
std::vector<bool>

bool std::deque<bool>
bool

std::deque<>

std::deque<>
std::vector<>

✛ ✲

std::deque<>

capacity() reserve()

deque

data()

capacity() reserve() data()


push_front()
pop_front()
emplace_front()
push_back() pop_back() emplace_back()

std::deque<>

<deque>

3
#include <deque>
#include <iostream>

int main()
{
std::deque<int> d{0}; //deque with one element, 0.
//fill with values:
for (int i{1}; i < 100; ++i) {
d.push_back(i);
d.push_front(-i);
}
// d is {-99,-98, ..., -1, 0, 1, ..., 98, 99}

// erase all multiples of 3:


for (auto it = d.begin(); it != d.end(); ) {
if (*it % 3 == 0) {
d.erase(it); // 'it' undefined
it = d.begin(); // 'it' defined; restart.
} else {
++it;
}
}

// print d:
for (auto it = d.cbegin(); it != d.cend(); ++it) {
std::cout << *it << '␣';
}
std::cout << '\n';
}

for (auto it = d.begin(); it != d.end(); ++it) {


if (*it % 3 == 0) {
d.erase(it); // it undefined
}
}

std::list<>

list<> vector<> deque<>


list<>

list<>
vector<> deque<>
✲ ✲ ✲ ✲ ✲ ✲ ✲ ✲
✛ ✲
✛ ✛ ✛ ✛ ✛ ✛ ✛ ✛

list<>



✻ ✻
❄ ❄
✲ ✲ ✲ ✲ ✲ ✲ ✲
✛ ✲
✛ ✛ ✛ ✛ ✛ ✛ ✛

std::list<>

<list>

std::list<>
vector<> deque<>

std::list<T> c;
std::list<T> c1;
std::list<T> c2{c1};
std::list<T> c3{std::move(c1)};

T a1, a2, a3, ...;


std::list<T> c{a1, a2, a3, ... };

std::list<T> c{beg,end};

std::list<T> c(N);

std::list<T> c(N, elem);

list<> size() empty() max_size()

list<>
deque<>
std::list<T> c1, c2, c3;
...
c2 = c1;
c3 = std::move(c1);

std::list<T> c;
T a1, a2, a3, ...;
c = {a1,a2,a3, ...};

swap() std::swap()
std::list<T> c1, c2, c3;
...
c1.swap(c2);
std::swap(c3,c1);

insert()

c.insert(pos, elem);
c.insert(pos, N, elem);
c.insert(pos, beg, end);
c.insert(pos, {a1, a2, a3, ...});

insert()
0 N==0 beg==end

assign()
c.assign(N,elem);
c.assign(beg, end);
c.assign({a1,a2,a3,...});
c.push_front(elem);
c.push_back(elem);

emplace()

emplace_front() emplace_back()

resize()

v.resize(N);

v.resize(N,elem);

splice()
std::list<T> c1, c2;
std::list<T>::const_iterator pos; // iterator to c1
...
c1.splice(pos, c2);

std::list<T> c1, c2;


std::list<T>::const_iterator c1pos; // iterator to c1
std::list<T>::const_iterator c2pos; // iterator to c2
...
c1.splice(c1pos, c2, c2pos);

std::list<T> c1, c2;


std::list<T>::const_iterator c1pos; // iterator to c1
std::list<T>::const_iterator c2beg; // iterator to c2
std::list<T>::const_iterator c2end; // iterator to c2
...
c1.splice(c1pos, c2, c2beg, c2end);

merge()

std::list<T> c1, c2;


...
c1.merge(c2);
std::list<T> c1, c2;
...
c1.merge(c2, comp);

comp()
comp()

true false

clear()
c.clear();

erase()

c.erase(pos);
c.erase(beg,end);

resize()

pop_front() pop_back()

remove()
c.remove(a);
c.remove_if(op);
remove_if()

op() op()
true false remove_if() op()
op()
true

remove() erase()
erase()
remove()

unique()
c.unique();
c.unique(comp);

comp() comp()

unique() comp()
comp() true

front() back()

*
reverse()

sort() N
N log N

c.sort();
c.sort(comp);

<

comp()
comp()
true false

comp()

#include <list>
#include <iostream>

bool lessthan10(double a)
{
return a < 10.0;
}

int main()
{
std::list<double> c; // creates empty list

for (int i{0}; i != 10; ++i) {


c.push_back(2.0 * i);
}
// c: {0.0, 2.0, 4.0, ..., 18.0}

c.remove(16.0);
// any element with value equal to 16.0 is erased.

c.remove_if(lessthan10);
// all elements with value less than 10 are erased.

std::cout << "List␣is␣:\t";

for (auto it = c.cbegin(); it != c.cend(); ++it) {


std::cout << *it << '␣';
}
std::cout << '\n';
}

c.remove_if(lessthan10);

c.remove_if(std::bind(std::less<double>{},
std:placeholders::_1,10.0));
<functional>
lessthan10()

std::forward_list<>

✲ ✲ ✲ ✲ ✲ ✲ ✲ ✲
forward_list<>

before_begin() cbefore_begin()

list<>

_after

before_begin() cbefore_begin()

forward_list<>

back() push_back() pop_back()

end()
size()

std::forward_list<>

<forward_list>

std::forward_list<> std::list<>
std::forward_list<>

std::forward_list<T> c;

std::forward_list<T> c1;
std::forward_list<T> c2{c1};
std::forward_list<T> c3{std::move(c1)};

T a1, a2, a3, ...;


std::forward_list<T> c{a1, a2, a3, ... };

std::forward_list<T> c{beg,end};

std::forward_list<T> c(N);

std::forward_list<T> c(N, elem);


forward_list<> empty() max_size()
size()
forward_list<>

#include <iterator>
#include <forward_list>

std::forward_list<T> c;
...
auto size = std::distance(c.cbegin(), c.cend());

forward_list<>

std::forward_list<T> c1, c2, c3;


...
c2 = c1;
c3 = std::move(c1);

std::forward_list<T> c;
T a1, a2, a3, ...;
c = {a1,a2,a3, ...};

swap() std::swap()
std::forward_list<T> c1, c2, c3;
...
c1.swap(c2);
std::swap(c3,c1);

insert_after()
c.insert_after(pos, elem);
c.insert_after(pos, N, elem);
c.insert_after(pos, beg, end);
c.insert_after(pos, {a1, a2, a3, ...});

insert_after()
0 N==0
beg==end

assign()
c.assign(N,elem);
c.assign(beg, end);
c.assign({a1,a2,a3,...});

c.push_front(elem);

emplace_after()

emplace_front()

resize()
v.resize(N);

v.resize(N,elem);

splice_after()

merge()

std::forward_list<T> c1, c2;


...
c1.merge(c2);

std::forward_list<T> c1, c2;


...
c1.merge(c2, comp);

comp()
clear()

erase_after()

c.erase_after(pos);
c.erase_after(beg,end);

resize()

pop_front()

remove()
c.remove(a);
c.remove_if(op);

remove_if()
op() true

unique()
c.unique();
c.unique(comp);

unique() comp()
comp() true

front()
*

reverse()

sort() N
N log N

c.sort();
c.sort(comp);

<
comp()

O(log N )

O(N )
std::set<>
std::multiset<> std::map<> std::multimap<>

std::set<> std::multiset<>

std::set<> std::multiset<>

<set>

std::set<T> std::multiset<T>

Set c;

Set c1;
Set c2{c1};
Set c3{std::move(c1)};

T a1, a2, a3, ...;


Set c{a1, a2, a3, ... };
Set c{beg,end};

std::less<T>
<functional> <

true/false

std::set<T, std::greater<T>> c;

key_comp()

std::set<> std::multiset<>
size() empty() max_size()

Set c1, c2, c3;


...
c2 = c1;
c3 = std::move(c1);
Set c;
T a1, a2, a3, ...;
c = {a1,a2,a3, ...};

swap() std::swap()
Set c1, c2, c3;
...
c1.swap(c2);
std::swap(c3,c1);

insert()

Set c;
T elem;
...
c.insert(elem);

insert()

Set c;
T elem;
Set::const_iterator pos;
...
c.insert(pos,elem);

c.insert(beg, end);
c.insert({a1, a2, a3, ...});
emplace()

std::set<std::string> c;
c.emplace("one"); // c is { "one" }
c.emplace("two"); // c is { "one", "two" }
c.emplace("three"); // c is { "one", "three", "two" }

emplace_hint()

clear()
c.clear();

erase()
Set c;
Set::value_type elem;
Set::const_iterator pos,beg,end;

c.erase(elem);
c.erase(pos);
c.erase(beg,end);
1

size_type

set<> multiset<>

O(log N ) O(N )

count()

size_type

find()

end()

cend()

lower_bound()

upper_bound()
equal_range()

#include <set>
#include <iostream>
#include <functional>
#include <iterator>

int main()
{
using crit = std::greater<int>;
std::cout << "Set:\n";
std::set<int,crit> c; // define empty set

// insert values in random order.


c = {5, 12, 3, 6, 7, 1, 9};

// print set
std::cout << "Number␣of␣elements:␣" << c.size() << '\n';
for (auto it = c.cbegin(); it != c.cend(); ++it) {
std::cout << *it << '␣';
}
std::cout << '\n';

// remove elements with value 4 and 6 and print information


auto m = c.erase(4);
std::cout << "There␣were␣" << m << "␣elements␣with␣value␣4\n";

m = c.erase(6);
std::cout << "There␣were␣" << m << "␣elements␣with␣value␣6\n";
std::cout << '\n';

std::cout << "Multiset:\n";

// create multiset from c


std::multiset<int, crit> mc{c.cbegin(), c.cend()};

//insert element 12 three times


mc.insert(12);
mc.insert(12);
mc.insert(12);

auto p = mc.equal_range(12);
std::cout <<"First␣12␣is␣in␣position␣"
<< std::distance(mc.cbegin(), p.first) << '\n';
std::cout <<"Last␣12␣is␣in␣position␣"
<< std::distance(mc.cbegin(), p.second)-1 << '\n';

// print multiset
std::cout << "Number␣of␣elements:␣" << mc.size() << '\n';

for (auto const & x: mc) {


std::cout << x << '␣';
}
std::cout << '\n';
}

std::map<> std::multimap<>

std::map<> std::multimap<>

<map>

map<> set<>

multimap<>
multiset<>
std::map<>
std::multimap<>

std::map<K,T> std::multimap<K,T>

Map c;

Map c1;
Map c2{c1};
Map c3{std::move(c1)};

K k1, k2, k3, ...;


T a1, a2, a3, ...;

std::pair<K,T> p1{k1,a1};
std::pair<K,T> p2{k2,a2};
std::pair<K,T> p3{k3,a3};
...

Map c1{ p1, p2, p3, ... };


Map c2{ {k1, a1}, {k2, a2}, {k3, a3}, ... };

Map c{beg,end};
std::less<K> <functional>
<

true/false

key_comp()

std::map<> std::multimap<>
size() empty() max_size()

Map c1, c2, c3;


...
c2 = c1;
c3 = std::move(c1);

Map c;

K k1, k2, k3, ...;


T a1, a2, a3, ...;

std::pair<K,T> p1{k1,a1};
std::pair<K,T> p2{k2,a2};
std::pair<K,T> p3{k3,a3};
...
c = { p1, p2, p3, ... };
swap() std::swap()
Map c1, c2, c3;
...
c1.swap(c2);
std::swap(c3,c1);

c.insert(elem);

std::pair<K,T>

Map c;
std::pair<K,T> elem;
Map:const_iterator pos;

c.insert(pos,elem);

c.insert(beg, end);
c.insert({p1, p2, p3, ...});

emplace()
emplace_hint()

clear()
c.clear();

erase()
Map c;
K key;
Map::const_iterator pos,beg,end;

c.erase(key);
c.erase(pos);
c.erase(beg,end);

size_type
(*it).first it->first
(*it).second it->second

std::map<>
std::set<>
[] at()

at()

c[key]
c.at(key)

c[key] = value;

std::make_pair(key,value)

std::cout << c[key];

c.at(key) std::out_of_range
map multimap

std::set<> std::multiset<>
count(key)
size_type

find(key)

end()
lower_bound(key)

upper_bound(key)

equal_range(key)

std::map<>

#include <iostream>
#include <string>
#include <map>
#include <utility>

int main()
{
using Map = std::map<std::string, int>;

Map birthyear; // Empty map.


// insert a few pairs in different ways:
birthyear.insert(std::make_pair("John", 1940));
birthyear.insert(Map::value_type("Paul", 1942));
birthyear.emplace("George", 1943);

std::cout << "John␣was␣born␣in␣"


<< birthyear["John"] << "\n\n";
// insert new pair
birthyear["Ringo"] = 1941; // wrong value, change it
birthyear.at("Ringo") = 1940;

// print all pairs


for (auto const & x : birthyear) {
std::cout << x.first << "␣was␣born␣in␣"
<< x.second << '\n';
}
}

std::unordered_set<>

std::unordered_multiset<>

std::unordered_map<>

std::unordered_multimap<>

std::unordered_multiset<> std::unordered_multimap<>
std::unordered_set<>
std::unordered_map<>

std::hash<> <functional>
true
false
std::equal_to<T> std::equal_to<K>
==

typedef

std::unordered_set<> std::unordered_multiset<>
<unordered_set> <unordered_map>
std::unordered_map<> std::unordered_multimap<>

std::unordered_set<T>

std::unordered_multiset<T>

std::unordered_map<K,T>

std::unordered_multimap<K,T>

Unord c;
Unord c1;
Unord c2{c1};
Unord c3{std::move(c1)};

Unord::value_type a1, a2, a3, ...;


Unord c{a1, a2, a3, ... };

Unord c{beg,end};

size() empty()
max_size()

Unord c1, c2, c3;


...
c2 = c1;
c3 = std::move(c1);
Unord c;
Unord::value_type p1,p2,p3,...;
...
c = {p1, p2, p3, ... };

swap() std::swap()
Unord c1, c2, c3;
...
c1.swap(c2);
std::swap(c3,c1);

Unord::value_type elem;
...
c.insert(elem);

Unord::value_type elem;
Unord:const_iterator pos;
c.insert(pos,elem);

c.insert(beg, end);
c.insert({p1, p2, p3, ...});
emplace()

emplace_hint()

clear()
c.clear();

erase()
Unord::const_iterator pos,beg,end;
c.erase(pos);
c.erase(beg,end);

erase()

c.erase(elem);
c.erase(key);

size_type

at() []

count()

size_type

find()

end()

cend()

equal_range()

hash_function() key_eq()

bucket_count()

max_bucket_count()
bucket_size()

bucket()

load_factor()

max_load_factor()

rehash()

reserve()
list unique() splice() merge() reverse()
std::list<T>

template<typename container>
void
split(container const & c, container & odd, container & even)
{
...
}

std::list<int> 3 5 −1 9 −7 88 3 −6 −4
split()
1958723584 60945983
<algorithm>
<numeric>
std
true/false

<numeric>

accumulate()

template<typename InputIterator, typename Type>


Type
accumulate(InputIterator beg, InputIterator end, Type value);

template<typename InputIterator, typename Type,


typename BinaryFunctor>
Type
accumulate(InputIterator beg, InputIterator end, Type value,
BinaryFunctor op);

value += a1;
value += a2;
...

op()

value = op(value,a1);
value = op(value,a2);
...

beg==end

auto sum = std::accumulate(v.cbegin(), v.cend(), 0.0};


auto prod = std::accumulate(v.cbegin(), v.cend(), 0.0,
std::multiplies<double>{}};

inner_product()

template<typename InputIterator, typename Type>


Type
inner_product(InputIterator beg1, InputIterator end1,
InputIterator beg2, Type value);

template<typename InputIterator, typename Type,


typename BinaryFunctor>
Type
inner_product(InputIterator beg1, InputIterator end1,
InputIterator beg2, Type value,
BinaryFunctor op1, BinaryFunctor op2);

+
*

value += a1*b1;
value += a2*b2;
...

value = op1(value,op2(a1,b1));
value = op1(value,op2(a2,b2));
...

beg1==end1

auto s = std::inner_product(v.cbegin(), v.cend(), v.cbegin(),


0.0);

partial_sum()

template<typename InputIterator, typename OutputIterator>


OutputIterator
partial_sum(InputIterator beg1, InputIterator end1,
OutputIterator beg2);

template<typename InputIterator, typename OutputIterator,


typename BinaryFunctor>
OutputIterator
partial_sum(InputIterator beg1, InputIterator end1,
OutputIterator beg2, BinaryFunctor op);

op()
beg2+i+1 op(*(beg2+i), *(beg1+i+1))

adjacent_difference()

template<typename InputIterator, typename OutputIterator>


OutputIterator
adjacent_difference(InputIterator beg1, InputIterator end1,
OutputIterator beg2);

template<typename InputIterator, typename OutputIterator,


typename BinaryFunctor>
OutputIterator
adjacent_difference(InputIterator beg1, InputIterator end1,
OutputIterator beg2, BinaryFunctor op);

op() op(*(beg1+i), *(beg1+i+1))


beg2+i+1
std::partial_sum()

< comp()
true false

comp()

min_element()

template<typename ForwardIterator>
ForwardIterator
min_element(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename ΒinaryFunctor>


ForwardIterator
min_element(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);

beg==end

auto && f = [](double x, double y) -> double


{ return (std::abs(x) < std::abs(y) ? x : y); }

auto it = std::min_element(v.cbegin(), v.cend(), f);

*it
max_element()

template<typename ForwardIterator>
ForwardIterator
max_element(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename ΒinaryFunctor>


ForwardIterator
max_element(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);

beg==end

minmax_element()

template<typename ForwardIterator>
std::pair<ForwardIterator,ForwardIterator>
minmax_element(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename ΒinaryFunctor>


std::pair<ForwardIterator,ForwardIterator>
minmax_element(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);
std::pair<>

beg==end

auto && f = [](double x, double y) -> double


{ return (std::abs(x) < std::abs(y) ? x : y); }
auto p = std::minmax_element(v.cbegin(), v.cend(), f);

p.first
p.second
copy()

template<typename InputIterator, typename OutputIterator>


OutputIterator
copy(InputIterator beg1, InputIterator end1, OutputIterator beg2);

template<typename InputIterator, typename OutputIterator,


typename UnaryFunctor>
OutputIterator
copy_if(InputIterator beg1, InputIterator end1,
OutputIterator beg2, UnaryFunctor op);

template<typename InputIterator, typename Size,


typename OutputIterator>
OutputIterator
copy_n(InputIterator beg1, Size n, OutputIterator beg2);

op()

op() true

std::copy()
std::move()

move()

template<typename InputIterator, typename OutputIterator>


OutputIterator
move(InputIterator beg1, InputIterator end1, OutputIterator beg2);
copy_backward()

template<typename BidirectionalIterator1,
typename BidirectionalIterator2>
BidirectionalIterator2
copy_backward(BidirectionalIterator1 beg1,
BidirectionalIterator1 end1,
BidirectionalIterator2 end2);

end1-i end2-i i = 1, 2, . . .

std::move_backward()

move_backward()

template<typename BidirectionalIterator1,
typename BidirectionalIterator2>
BidirectionalIterator2
move_backward(BidirectionalIterator1 beg1,
BidirectionalIterator1 end1,
BidirectionalIterator2 end2);

end1-i end2-i i = 1, 2, . . .
rotate()

template<typename ForwardIterator>
ForwardIterator
rotate(ForwardIterator beg, ForwardIterator mid,
ForwardIterator end);

rotate_copy()

template<typename ForwardIterator, typename OutputIterator>


OutputIterator
rotate_copy(ForwardIterator beg, ForwardIterator mid,
ForwardIterator end, OutputIterator beg2);

replace()

template<typename ForwardIterator, typename Type>


void
replace(ForwardIterator beg, ForwardIterator end,
Type const & oldvalue, Type const & newvalue);

template<typename ForwardIterator, typename Type,


typename UnaryFunctor>
void
replace_if(ForwardIterator beg, ForwardIterator end,
UnaryFunctor op, Type const & newvalue);

==
op() true

using type = decltype(v)::value_type;


type zero{0};
std::less<type> f;
auto && lt0 = std::bind(f, std::placeholders::_1, zero);
std::replace_if(v.begin(), v.end(), lt0, zero);
0

std::less<>

0
std::bind()

auto && lt0 = [zero] (decltype(zero) x) -> bool


{return x < zero;};

replace_copy()

template<typename InputIterator, typename OutputIterator,


typename Type>
OutputIterator
replace_copy(InputIterator beg1, InputIterator end1,
OutputIterator beg2, Type const & oldvalue,
Type const & newvalue);

template<typename InputIterator, typename OutputIterator,


typename UnaryFunctor, typename Type>
OutputIterator
replace_copy_if(InputIterator beg1, InputIterator end1,
OutputIterator beg2, UnaryFunctor op,
Type const & newvalue);
==

op() true

using type = decltype(a)::value_type;


type constexpr c{1};
auto && g = [c] (type const & x) -> bool
{return std::abs(x) >= c;};
std::replace_copy_if(a.cbegin(), a.cend(), b.begin(), g, c);

1 1

remove()

template<typename ForwardIterator, typename Type>


ForwardIterator
remove(ForwardIterator beg, ForwardIterator end, Type const & value);

template<typename ForwardIterator, typename UnaryFunctor>


ForwardIterator
remove_if(ForwardIterator beg, ForwardIterator end, UnaryFunctor op);

==
op()

op() true
erase()
std::list<> std::forward_list<>
remove() std::remove()

using type = decltype(v)::value_type;


auto && ge0 = std::bind(std::greater_equal<type>{},
std::placeholders::_1, type{0});
auto end = std::remove_if(v.begin(), v.end(), ge0);
[v.begin(),end)

std::greater_equal<>

0 std::bind()

remove_copy()

template<typename InputIterator, typename OutputIterator,


typename Type>
OutputIterator
remove_copy(InputIterator beg1, InputIterator end1,
OutputIterator beg2, Type const & value);

template<typename InputIterator, typename OutputIterator,


typename UnaryFunctor>
OutputIterator
remove_copy_if(InputIterator beg1, InputIterator end1,
OutputIterator beg2, UnaryFunctor op);
==
op()

op() true

unique()

template<typename ForwardIterator>
ForwardIterator
unique(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename BinaryFunctor>


ForwardIterator
unique(ForwardIterator beg, ForwardIterator end, BinaryFunctor comp);

==
comp() comp() true false

std::list<> std::forward_list<>
unique() std::unique()

unique_copy()
template<typename InputIterator, typename OutputIterator>
OutputIterator
unique_copy(InputIterator beg1, InputIterator end1,
OutputIterator beg2);

template<typename InputIterator, typename OutputIterator,


typename BinaryFunctor>
OutputIterator
unique_copy(InputIterator beg1, InputIterator end1,
OutputIterator beg2, BinaryFunctor comp);

std::unique()

==
comp()

reverse()

template<typename BidirectionalIterator>
void
reverse(BidirectionalIterator beg, BidirectionalIterator end);

std::list<> std::forward_list<>

reverse_copy()

template<typename BidirectionalIterator>
OutputIterator
reverse_copy(BidirectionalIterator beg1, BidirectionalIterator end1,
OutputIterator beg2);
random_shuffle()

template<typename RandomIterator>
void
random_shuffle(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename RNG>


void
random_shuffle(RandomIterator beg, RandomIterator end, RNG && rng);

rng()

rng()

std::shuffle()

shuffle()

template<typename RandomIterator, typename URNG>


void
shuffle(RandomIterator beg, RandomIterator end, URNG && g);

<random>

op() op()
true false

typename std::iterator_traits<RandomIterator>::difference_type
partition()

template<typename ForwardIterator, typename UnaryFunctor>


ForwardIterator
partition(ForwardIterator beg, ForwardIterator end, UnaryFunctor op);

op() true
op() false

op() false

using type = decltype(v)::value_type;


type zero{0};
auto && gt0 = std::bind(std::greater<type>{},
std::placeholders::_1, zero);
auto mid = std::partition(v.begin(), v.end(), gt0);

std::greater<>

0
std::bind()

stable_partition()

template<typename BidirectionalIterator, typename UnaryFunctor>


BidirectionalIterator
stable_partition(BidirectionalIterator beg,
BidirectionalIterator end, UnaryFunctor op);
std::partition()
op() true
false

is_partitioned()
template<typename InputIterator, typename UnaryFunctor>
bool
is_partitioned(InputIterator beg, InputIterator end,
UnaryFunctor op);

op()
op() true false
true false
beg==end true

partition_point()

template<typename ForwardIterator, typename UnaryFunctor>


ForwardIterator
partition_point(ForwardIterator beg, ForwardIterator end,
UnaryFunctor op);

op()

std::partition(beg,end,op);

op() false

partition_copy()

template<typename InputIterator, typename OutputIterator1,


typename OutputIterator2, typename UnaryFunctor>
std::pair<OutputIterator1,OutputIterator2>
partition_copy(InputIterator beg1, InputIterator end1,
OutputIterator1 beg2, OutputIterator2 beg3,
UnaryFunctor op);

op() true
op() false

std::pair<>
< comp()
true false
comp()

sort()

template<typename RandomIterator>
void
sort(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
sort(RandomIterator beg, RandomIterator end, BinaryFunctor comp);

n log n n

std::list<> std::forward_list<>

stable_sort()

template<typename RandomIterator>
void
stable_sort(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
stable_sort(RandomIterator beg, RandomIterator end,
BinaryFunctor comp);
std::sort()

nth_element()
template<typename RandomIterator>
void
nth_element(RandomIterator beg, RandomIterator nth,
RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
nth_element(RandomIterator beg, RandomIterator nth,
RandomIterator end, BinaryFunctor comp);

*nth

partial_sort()

template<typename RandomIterator>
void
partial_sort(RandomIterator beg, RandomIterator sortEnd,
RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
partial_sort(RandomIterator beg, RandomIterator sortEnd,
RandomIterator end, BinaryFunctor comp);
std::sort()

partial_sort_copy()

template<typename InputIterator, typename RandomIterator>


RandomIterator
partial_sort_copy(InputIterator beg1, InputIterator end1,
RandomIterator beg2, RandomIterator end2);

template<typename InputIterator, typename RandomIterator,


typename BinaryFunctor>
RandomIterator
partial_sort_copy(InputIterator beg1, InputIterator end1,
RandomIterator beg2, RandomIterator end2,
BinaryFunctor comp);
is_sorted()

template<typename ForwardIterator>
bool
is_sorted(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename BinaryFunctor>


bool
is_sorted(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);

true
false

is_sorted_until()

template<typename ForwardIterator>
ForwardIterator
is_sorted_until(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename BinaryFunctor>


ForwardIterator
is_sorted_until(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);

N N!

std::lexicographical_compare()
N!

p 1 p 2 . . . pn

k pk < pk+1

i>k pk < p i

pk pi

pk+1 pn

std::next_permutation()

lexicographical_compare()

template<typename InputIterator1, typename InputIterator2>


bool
lexicographical_compare(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2);

template<typename InputIterator1, typename InputIterator2,


typename BinaryFunctor>
bool
lexicographical_compare(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
BinaryFunctor comp);

true false
==
<
!(a<b) && !(b<a)
comp() !comp(a,b) && !comp(b,a)
next_permutation()

template<typename BidirectionalIterator>
bool
next_permutation(BidirectionalIterator beg,
BidirectionalIterator end);

template<typename BidirectionalIterator, typename BinaryFunctor>


bool
next_permutation(BidirectionalIterator beg,
BidirectionalIterator end, BinaryFunctor comp);

false
true
<
comp()
true false
comp()

prev_permutation()

template<typename BidirectionalIterator>
bool
prev_permutation(BidirectionalIterator beg,
BidirectionalIterator end);

template<typename BidirectionalIterator, typename BinaryFunctor>


bool
prev_permutation(BidirectionalIterator beg,
BidirectionalIterator end, BinaryFunctor comp);

false
true
<
comp()
true false
comp()
is_permutation()

template<typename ForwardIterator1, typename ForwardIterator2>


bool
is_permutation(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2);

template<typename ForwardIterator1, typename ForwardIterator2,


typename BinaryFunctor>
bool
is_permutation(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, BinaryFunctor comp);

template<typename ForwardIterator1, typename ForwardIterator2>


bool
is_permutation(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2);

template<typename ForwardIterator1, typename ForwardIterator2,


typename BinaryFunctor>
bool
is_permutation(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2,
BinaryFunctor comp);

==
comp() comp() true false

<
comp()
comp()
true false
==
comp() comp()
true false

find()

template<typename InputIterator, typename Type>


InputIterator
find(InputIterator beg, InputIterator end, Type const & value);

template<typename InputIterator, typename UnaryFunctor>


InputIterator
find_if(InputIterator beg, InputIterator end, UnaryFunctor op);

template<typename InputIterator, typename UnaryFunctor>


InputIterator
find_if_not(InputIterator beg, InputIterator end, UnaryFunctor op);

==

op()
std::find_if()
op() true
std::find_if_not() op()
false

std::upper_bound() std::lower_bound() std::equal_range()

find()

auto && lta = std::bind(std::less<decltype(a)>{},


std::placeholders::_1, a);
auto it = std::find_if(v.cbegin(), v.cend(), lta);
v.cend()

find_first_of()

template<typename InputIterator, typename ForwardIterator>


InputIterator
find_first_of(InputIterator beg1, InputIterator end1,
ForwardIterator beg2, ForwardIterator end2);

template<typename InputIterator, typename ForwardIterator,


typename BinaryFunctor>
InputIterator
find_first_of(InputIterator beg1, InputIterator end1,
ForwardIterator beg2, ForwardIterator end2,
BinaryFunctor comp);

== comp()

search()

template<typename ForwardIterator1, typename ForwardIterator2>


ForwardIterator1
search(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2);

template<typename ForwardIterator1, typename ForwardIterator2,


typename BinaryFunctor>
ForwardIterator1
search(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2,
BinaryFunctor comp);

== comp()
beg2==end2

find_end()

template<typename ForwardIterator1, typename ForwardIterator2>


ForwardIterator1
find_end(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2);

template<typename ForwardIterator1, typename ForwardIterator2,


typename BinaryFunctor>
ForwardIterator1
find_end(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2, ForwardIterator2 end2,
BinaryFunctor comp);
std::search()

std::search()
==
comp()

beg2==end2

adjacent_find()

template<typename ForwardIterator>
ForwardIterator
adjacent_find(ForwardIterator beg, ForwardIterator end);

template<typename ForwardIterator, typename BinaryFunctor>


ForwardIterator
adjacent_find(ForwardIterator beg, ForwardIterator end,
BinaryFunctor comp);
search_n()

template<typename ForwardIterator, typename Size, typename Type>


ForwardIterator
search_n(ForwardIterator beg, ForwardIterator end, Size n,
Type const & value);

template<typename ForwardIterator, typename Size, typename Type,


typename BinaryFunctor>
ForwardIterator
search_n(ForwardIterator beg, ForwardIterator end,
Size n, Type const & value, BinaryFunctor comp);

binary_search()

template<typename ForwardIterator, typename Type>


bool
binary_search(ForwardIterator beg, ForwardIterator end,
Type const & value);

template<typename ForwardIterator, typename Type,


typename BinaryFunctor>
bool
binary_search(ForwardIterator beg, ForwardIterator end,
Type const & value, BinaryFunctor comp);
true false

< comp()
comp()

upper_bound()

template<typename ForwardIterator, typename Type>


ForwardIterator
upper_bound(ForwardIterator beg, ForwardIterator end,
Type const & value);

template<typename ForwardIterator, typename Type,


typename BinaryFunctor>
ForwardIterator
upper_bound(ForwardIterator beg, ForwardIterator end,
Type const & value, BinaryFunctor comp);

< comp()
comp()

lower_bound()

template<typename ForwardIterator, typename Type>


ForwardIterator
lower_bound(ForwardIterator beg, ForwardIterator end,
Type const & value);

template<typename ForwardIterator, typename Type,


typename BinaryFunctor>
ForwardIterator
lower_bound(ForwardIterator beg, ForwardIterator end,
Type const & value, BinaryFunctor comp);

< comp()
comp()

equal_range()

template<typename ForwardIterator, typename Type>


std::pair<ForwardIterator,ForwardIterator>
equal_range(ForwardIterator beg, ForwardIterator end,
Type const & value);

template<typename ForwardIterator, typename Type,


typename BinaryFunctor>
std::pair<ForwardIterator,ForwardIterator>
equal_range(ForwardIterator beg, ForwardIterator end,
Type const & value, BinaryFunctor comp);

< comp() comp()

==

!(a<b) && !(b<a)

!comp(a,b) && !comp(b,a)

<
comp()
true false
comp()

==
!(a<b) && !(b<a)
!comp(a,b) && !comp(b,a)

merge()

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator>
OutputIterator
merge(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2, OutputIterator beg3);
template<typename InputIterator1, typename InputIterator2,
typename OutputIterator, typename BinaryFunctor>
OutputIterator
merge(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3, BinaryFunctor comp);

std::list<> std::forward_list<>

inplace_merge()

template<typename BidirectionalIterator>
void
inplace_merge(BidirectionalIterator beg, BidirectionalIterator mid,
BidirectionalIterator end);

template<typename BidirectionalIterator, typename BinaryFunctor>


void
inplace_merge(BidirectionalIterator beg, BidirectionalIterator mid,
BidirectionalIterator end, BinaryFunctor comp);

set_union()

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator>
OutputIterator
set_union(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3);

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator, typename BinaryFunctor>
OutputIterator
set_union(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3, BinaryFunctor comp);
std::merge()

1 2 1
2− 1
2> 1

set_intersection()

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator>
OutputIterator
set_intersection(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3);

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator, typename BinaryFunctor>
OutputIterator
set_intersection(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3, BinaryFunctor comp);

1
2 min{ 1 2}

set_difference()

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator>
OutputIterator
set_difference(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3);

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator, typename BinaryFunctor>
OutputIterator
set_difference(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3, BinaryFunctor comp);

1 2
max{ 1− 2 0}

set_symmetric_difference()

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator>
OutputIterator
set_symmetric_difference(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3);

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator, typename BinaryFunctor>
OutputIterator
set_symmetric_difference(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
OutputIterator beg3, BinaryFunctor comp);

1
2 1− 2
1> 2 2− 1
2> 1 1= 2

includes()

template<typename InputIterator1, typename InputIterator2>


bool
includes(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2);
template<typename InputIterator1, typename InputIterator2,
typename BinaryFunctor>
bool
includes(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
BinaryFunctor comp);

<
comp()
true false
comp()

make_heap()

template<typename RandomIterator>
void
make_heap(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
make_heap(RandomIterator beg, RandomIterator end,
BinaryFunctor comp);

is_heap()
template<typename RandomIterator>
bool
is_heap(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


bool
is_heap(RandomIterator beg, RandomIterator end, BinaryFunctor comp);

true false
true

is_heap_until()

template<typename RandomIterator>
RandomIterator
is_heap_until(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


RandomIterator
is_heap_until(RandomIterator beg, RandomIterator end,
BinaryFunctor comp);

pop_heap()

template<typename RandomIterator>
void
pop_heap(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
pop_heap(RandomIterator beg, RandomIterator end, BinaryFunctor comp);

−1 −1
push_heap()

template<typename RandomIterator>
void
push_heap(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
push_heap(RandomIterator beg, RandomIterator end,
BinaryFunctor comp);

−1
−1

sort_heap()

template<typename RandomIterator>
void
sort_heap(RandomIterator beg, RandomIterator end);

template<typename RandomIterator, typename BinaryFunctor>


void
sort_heap(RandomIterator beg, RandomIterator end,
BinaryFunctor comp);

all_of()

template<typename InputIterator, typename UnaryFunctor>


bool
all_of(InputIterator beg, InputIterator end, UnaryFunctor op);
op()
op()
true true
false
none_of()

template<typename InputIterator, typename UnaryFunctor>


bool
none_of(InputIterator beg, InputIterator end, UnaryFunctor op);
op()
op()
false true
false

any_of()

template<typename InputIterator, typename UnaryFunctor>


bool
any_of(InputIterator beg, InputIterator end, UnaryFunctor op);
op()

op()
true true
false

count()

template<typename InputIterator, typename Type>


Diff
count(InputIterator beg, InputIterator end, Type const & value);

template<typename InputIterator, typename UnaryFunctor>


Diff
count_if(InputIterator beg, InputIterator end, UnaryFunctor op);

op() true

typename std::iterator_traits<InputIterator>::difference_type
auto cnt = std::count(v.cbegin(), v.cend(), a);
auto && lta = std::bind(std::less<decltype(a)>{},
std::placeholders::_1, a);
auto cnt2 = std::count_if(v.cbegin(), v.cend(), lta);

std::count_if()
auto && lta = [a] (decltype(a) x) -> bool {return x < a;};

equal()

template<typename InputIterator1, typename InputIterator2>


bool
equal(InputIterator1 beg1, InputIterator1 end1, InputIterator2 beg2);

template<typename InputIterator1, typename InputIterator2,


typename BinaryFunctor>
bool
equal(InputIterator1 beg1, InputIterator1 end1, InputIterator2 beg2,
BinaryFunctor comp);

template<typename InputIterator1, typename InputIterator2>


bool
equal(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2);

template<typename InputIterator1, typename InputIterator2,


typename BinaryFunctor>
bool
equal(InputIterator1 beg1, InputIterator1 end1, InputIterator2 beg2,
InputIterator2 end2, BinaryFunctor comp);
true
false
true

false

==
comp()

auto && f = std::less<decltype(a)::value_type>{};


auto g = std::equal(a.cbegin(), a.cend(), b.cbegin(), f);

mismatch()

template<typename InputIterator1, typename InputIterator2>


std::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2);

template<typename InputIterator1, typename InputIterator2,


typename BinaryFunctor>
std::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, BinaryFunctor comp);

template<typename InputIterator1, typename InputIterator2>


std::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2);

template<typename InputIterator1, typename InputIterator2,


typename BinaryFunctor>
std::pair<InputIterator1, InputIterator2>
mismatch(InputIterator1 beg1, InputIterator1 end1,
InputIterator2 beg2, InputIterator2 end2,
BinaryFunctor comp);
==
comp()
true false

std::pair<>


iota()

template<typename ForwardIterator, typename Type>


void
iota(ForwardIterator beg, ForwardIterator end, Type value);

value+1 value+2
++
<numeric>

std::iota(a.begin(), a.end(), 0);


0
1 2

for_each()

template<typename InputIterator, typename UnaryFunctor>


UnaryFunctor
for_each(InputIterator beg, InputIterator end, UnaryFunctor op);
op()
op()

op()
return std::move(op);

swap_ranges()

template<typename ForwardIterator1, typename ForwardIterator2>


ForwardIterator2
swap_ranges(ForwardIterator1 beg1, ForwardIterator1 end1,
ForwardIterator2 beg2);

beg2+(end1-beg1)

transform()

template<typename InputIterator, typename OutputIterator,


typename UnaryFunctor>
OutputIterator
transform(InputIterator beg1, InputIterator end1,
OutputIterator beg2, UnaryFunctor op);

template<typename InputIterator1, typename InputIterator2,


typename OutputIterator, typename UnaryFunctor>
OutputIterator
transform(InputIterator1 beg1, InputIterator1 end1,
InputIterator1 beg2, OutputIterator beg3,
BinaryFunctor op);
op()

op()

fill()

template<typename ForwardIterator, typename Type>


void
fill(ForwardIterator beg, ForwardIterator end, Type const & value);

template<typename OutputIterator, typename Size, typename Type>


OutputIterator
fill_n(OutputIterator beg, Size n, Type const & value);

beg+n

auto it = std::fill_n(a.begin(), 10, 1);


std::fill(it, a.end(), 2);
10 1
2

generate()

template<typename ForwardIterator, typename Functor>


void
generate(ForwardIterator beg, ForwardIterator end, Functor op);

template<typename OutputIterator, typename Size, typename Functor>


OutputIterator
generate_n(OutputIterator beg, Size n, Functor op);

op()
op()
op()

beg+n
1000000
[−1000000, 1000000]

−1234

0
10000
10000
100

vector deque list set


std::clock() <ctime>

std::equal()

template<typename Iterator>
bool equal(Iterator beg1, Iterator end1, Iterator beg2)
{
.....
}

std::list<>
1, 3, 5, 88, 92, 4, 91
1, 3, 5, 88, 92, 4, 2, 91
equal()
false
http://bit.ly/2bDuQxB http:
//bit.ly/2bIIhtv

std::string

for while do ... while


goto
std::isalnum() std::toupper()
<cctype>
class Student {
...
};

class struct
;

class Student {

private:
std::string name;
std::string department;
int am;
int year;
bool active;
};
private:

#include <string>

class Student {
public:
std::string const & getName() const {return name;}
std::string const & getDept() const {return department;}
int const & getAm() const {return am;}
int const & getYear() const {return year;}
bool const & getActive() const {return active;}

void setActive(bool a) {active = a;}


union
private:
std::string name;
std::string department;
int am;
int year;
bool active;
};
public:

private:
class public:
struct

class

setActive()

const
getName() getDept() getAm() getYear() getActive()

const
const
protected:
Student & s1{s};
Student const & s2{s};

s1.setActive(false); // correct
s2.setActive(false); // error
auto y1 = s1.getYear(); // correct
auto y2 = s2.getYear(); // correct
setYear()
getYear()

const

const

private
public

public

class Student {
public:
std::string const & getName() const;
std::string const & getDept() const;
int const & getAm() const;
int const & getYear() const;
bool const & getActive() const;

void setActive(bool a);


private:
std::string name;
std::string department;
int am;
int year;
bool active;
};

std::string const & Student::getName() const


{
return name;
}

std::string const & Student::getDept() const


{
return department;
}

int const & Student::getAm() const


{
return am;
}

int const & Student::getYear() const


{
return year;
}

bool const & Student::getActive() const


{
return active;
}

void Student::setActive(bool a)
{
active = a;
}

Student::
inline
inline
inline
template

#include "Student.h"

Student s{"George␣Thomson", "Physics", 123, 2010, true};

Student p{"George␣Thomson", "Physics", 123, 2010};

true

Student(std::string const & onoma, std::string const & dept,


int armi, int y, bool act = true);

Student::Student(std::string const & onoma, std::string const & dept,


int armi, int y, bool act)
: name{onoma}, department{dept}, am{armi}, year{y},
active{act}
{}
:

bool active{true};

Student s{"George␣Thomson", "Physics", 123, 2010, true};


Student p{"George␣Thomson", "Physics", 123, 2010};

std::string

std::string name{onoma};
std::string department{dept};
int am{armi};
int year{y};
bool active{act};
Student::Student(std::string const & onoma, std::string const & dept,
int armi, int y, bool act)
{
name = onoma;
department = dept;
am = armi;
year = y;
active = act;
}

std::string name;
name = onoma;

std::string department;
department = dept;

int am;
am = armi;

int year;
year = y;

bool active;
active = act;

std::string

const

:
Student(Student const & other);

Student::Student(Student const & other)


: name{other.name}, department{other.department},
am{other.am}, year{other.year}, active{other.active}
{}

Student s{"George␣Thomson", "Physics", 123, 2010, true};


Student t1{s};
Student t2(s);
Student t3 = s;

void f(Student t);


f(s)

Student s{"George␣Thomson", "Physics", 123, 2010, true};


f(s);
Student t{s};
std::move() <utility>

void f(Student r);


Student g();

Student p = std::move(s);
f(std::move(p));
Student q = f();

Student(Student && other);

Student::Student(Student && other)


: name{std::move(other.name)},
department{std::move(other.department)},
am{std::move(other.am)}, year{std::move(other.year)},
active{std::move(other.active)}
{}
std::move()

std::string

inline X(){}
X a; X a{};

X a();

X() = default;

X() = delete;

inline X(X const & other) inline X(X & other)

std::ifstream

Student(Student const & other) = default;


X(X const & other) = delete;

inline X(X && other)

Student(Student && other) = default;

X(X const && other) = delete;

~Student(){}

delete

inline ~X(){}
~X() = default;

~X() = delete;

= + < =

Student s{"George␣Thomson", 123, 2000};


Student t;
t = s; // operator=() is called.
t=s; t.operator=(s);

Student & operator=(Student const & other);

t=t;

operator=()
Student & operator=(Student const & other)
{
if (this != &other) {
name = other.name;
AM = other.AM;
Year = other.Year;
}
return *this;
}
this
operator=() if

a=b=c=d


operator⊗

.
sizeof

<
== operator<()
operator==()

<utility>

using namespace std::rel_ops;

std::rel_ops
bool operator<(Student const & other)
{
return AM < other.AM;
}

bool operator==(Student const & other)


{
return AM == other.AM;
}

using namespace std::rel_ops;

template<typename T>
template<typename T>
class X {
T a;
...
};

X<double> x1;
X<int> x2;
x1.a x2.a

template<int N>
class Y {
std::array<double,N> v;
};
Y<10> c;
c.v 10

template<typename T>
<T>
template<typename T>
class X {
T a;
...
void f(T v);
};

template<typename T>
void X<T>::f(T v)
{
...
}
std::array<> <array>

#include <iostream>
#include "date.h"

void cmp(date const & a, date const & b)


{
if (a > b) {
std::cout << a << "␣is␣after␣" << b << '\n';
}
if (a == b) {
std::cout << a << "␣coincides␣with␣" << b <<'\n';
}

if (a < b) {
std::cout << a << "␣is␣before␣" << b << '\n';
}
}

int main()
{
date a; // a = 1/1/1970
std::cout << a << '\n';

date b{14,5,2005}; // b = 14/5/2005


std::cout << b << '\n';

date c{b}; // c = b (14/5/2005)


std::cout << c << '\n';

date d{c+3}; // d = 17/5/2005


std::cout << d << '\n';

date e{d+38}; // e = 24/6/2005


std::cout << e << '\n';
date f{d-1000}; // f = 21/8/2002
std::cout << "␣the␣day␣is␣" << f.day()
<< "␣the␣month␣is␣" << f.month()
<< "␣the␣year␣is␣" << f.year() << '\n';

int k{b-f}; // k = 997


std::cout << k << '\n';

--a; // 31/12/1969
std::cout << a << '\n';
++f; // 22/8/2002
std::cout << f << '\n';

cmp(a,b);
cmp(d,e);
cmp(c,b);

d += 4; // d = 21/5/2005
std::cout << d << '\n';

e -= 3000; // e = 7/4/1997
std::cout << e << '\n';

f = d; // f = 21/5/2005
std::cout << f << '\n';
}

std::complex<>

template<typename T>
complex<T> {
}
#include <iostream>
#include "sudoku.h"

int main(int argc, char *argv[])


{
sudoku s{argv[1]};
auto res = s.solve();
if (res != 0) {
std::cerr << "Den␣lynetai\n";
return -1;
}
s.print(argv[2]);
}

argv[1]
argv[2]
2001

#include <stdio.h>
int l;int main(int o,char **O,
int I){char c,*D=O[1];if(o>0){
for(l=0;D[l ];D[l
++]-=10){D [l++]-=120;D[l]-=
110;while (!main(0,O,l))D[l]
+= 20; putchar((D[l]+1032)
/20 ) ;}putchar(10);}else{
c=o+ (D[I]+82)%10-(I>l/2)*
(D[I-l+I]+72)/10-9;D[I]+=I<0?0
:!(o=main(c/10,O,I-1))*((c+999
)%10-(D[I]+92)%10);}return o;}

http://www.ioccc.org
1995
429539
#include <stdio.h>

#define l11l 0xFFFF


#define ll1 for
#define ll111 if
#define l1l1 unsigned
#define l111 struct
#define lll11 short
#define ll11l long
#define ll1ll putchar
#define l1l1l(l) l=malloc(sizeof(l111 llll1));l->lll1l=1-1;l->ll1l1=1-1;
#define l1ll1 *lllll++=l1ll%10000;l1ll/=10000;
#define l1lll ll111(!l1->lll1l){l1l1l(l1->lll1l);l1->lll1l->ll1l1=l1;}\
lllll=(l1=l1->lll1l)->lll;ll=1-1;
#define llll 1000

l111 llll1 {
l111 llll1 *
lll1l,*ll1l1 ;l1l1 lll11 lll [
llll];};main (){l111 llll1 *ll11,*l1l,*
l1, *ll1l, * malloc ( ) ; l1l1 ll11l l1ll ;
ll11l l11,ll ,l;l1l1 lll11 *lll1,* lllll; ll1(l
=1-1 ;l< 14; ll1ll("\t\"8)>l\"9!.)>vl" [l]^'L'),++l
);scanf("%d",&l);l1l1l(l1l) l1l1l(ll11 ) (l1=l1l)->
lll[l1l->lll[1-1] =1]=l11l;ll1(l11 =1+1;l11<=l;
++l11){l1=ll11; lll1 = (ll1l=( ll11=l1l))->
lll; lllll =( l1l=l1)->lll; ll=(l1ll=1-1
);ll1(;ll1l-> lll1l||l11l!= *lll1;){l1ll
+=l11**lll1++ ;l1ll1 ll111 (++ll>llll){
l1lll lll1=( ll1l =ll1l-> lll1l)->lll;
}}ll1(;l1ll; ){l1ll1 ll111 (++ll>=llll)
{ l1lll} } * lllll=l11l;}
ll1(l=(ll=1- 1);(l<llll)&&
(l1->lll[ l] !=l11l);++l); ll1 (;l1;l1=
l1->ll1l1,l= llll){ll1(--l ;l>=1-1;--l,
++ll)printf( (ll)?((ll%19) ?"%04d":(ll=
19,"\n%04d") ):"%4d",l1-> lll[l] ) ; }
ll1ll(10); }
1996

#define x char
#define z else
#define w gets
#define r if
#define u int
#define s main
#define v putchar
#define y while
#define t "␣A?B?K?L?CIF?MSP?E?H?O?R?DJG?NTQ?????U?V?????X???????Z????W??Y??"
s ( ) { x* c , b[ 5 * 72 ]; u a, e , d [ 9
*9 *9 ] ; y (w ( b) ){ r ( 0 [ b] -7 *
5 ) { c = b ; y ( (* c - 6
* 7 )* * c )c = c+ 1 ; r (( -0 ) [ c ] && w ( b +
8 * 5* 3 ) && w (b +8 *5 * 6) )
{ a = 0; y ( a [ b ]
) { a [ d ] = ! ! ( a[ b] - 4 *8 ) ; a = a +
1; } y (a < 8 * 5 * 3 ) d [ ( a ++ )
] = 0 ; a = 0; y( b [
a + 8 *3 * 5 ] ) { d [ a ] = a [ d] + ! !( b[
a+ 40 * 3 ] -4 * 8) * 2 ; ++ a ; } a =
0 ; y ( a [ b + 6 * 40 ]
) {a [d ] += ! ! ( b[ a + 5 * 6 * 8 ] - 4 *
8 ) * 4; a = a + 1 ;} a= 0; y (a <3 *8
*5 ) { r ( a [ d ] ) { e
= 1 ; y ( e[ a + d] ){ * ( d+ a+ e ) = a[ d
+e - 1] +( d [ a + e ] << ( 3* e) ) ;e
= e + 1 ;} a = a +
e - 1 ; v( !! ( * ( d+
a) % ( 64 ) - 12 * 5
) + ( e
> 4 ) ?t [ e >2 ? 2 :a [ d ] ] :6 * 8 +( t [ d[
a ]/ 8 /8 ] -4 ) %( 10 ) ) ; r( ! (2 [a
+ d] + 3 [d + a ] ) ) v ( 4 * 8 )
; } a = a + 1 ; } v (5 * 2 ) ; } z {c =b ; e
= 0 ; y (* c ) { * c += - (* c > 8 * 12
) * 32 ; a = 8 * 8 ; r (* c
>= 48 &&* c<8 *8 -6) {*c =( * c + 1
) %( 5 * 2 ) + 65 ;y( --a >0 &&*
c- a[ t] ); d[ (e ++ )] =4 ;(
* ( d + (e ++) ))= 07 ;}z y(a -- >1
&&* c- t[a ]); d[ ( e = e + 1) -
1] =a %8 ;y (a /= 8) d[ (e ++
) ] = a% 8 ; ++ c ; * ( e ++ +d ) =0
;} -- e ;r ( e >0 ) {a = 1 ;
y ( a< 8 ) { c = b ;y
( c <e +b ) { v ( * ( c - b + d )& a? 6 *7
:8 * 4 ) ; c ++ ;} a= a + a ; v ( 2*
5) ; } v ( 5* 2 ) ; }} }} }
(1 +
N )/2 N

N
1 + log2 N
N
0
1

16 bit
[0, 65535] [0, 255]
256
h(k) = k mod 256

[0, 255]
N O(N 2 )
N

(2, 3) (3, 4) (N − 1, N )

(N − 2, N − 1) (N − 2, N − 1)
(1, 2)
N −1 N −2 1

O(N 2 )
O(N )
O(N 2 )
O(N log N ) N
N log N N
std::vector<int>

−1

std::vector<>

−1

std::vector<>

1254

http://tinyurl.com/114nonrepeat

std::vector<>

http://tinyurl.com/114rndint

std::vector<>

http://tinyurl.com/114nonrepeat
1254
316001 499160
main()
main() PROGRAM
extern "C"

int
f(double a)
{
.....
}

void
g(int a)
{
.....
}

extern "C"
int f(double a);

extern "C"
void g(int a);

extern "C" {
int f(double a);
void g(int a);
}
gcc -c fg.c
g++ prog.cpp fg.o

DLASRT()

SUBROUTINE DLASRT( ID, N, D, INFO )

CHARACTER ID
INTEGER INFO, N
DOUBLE PRECISION D( * )

ID (input) CHARACTER*1
= 'I': sort D in increasing order;
= 'D': sort D in decreasing order.

N (input) INTEGER
The length of the array D.

D (input/output) DOUBLE PRECISION array, dimension (N)


On entry, the array to be sorted.
On exit, D has been sorted into increasing order
(D(1) <= ... <= D(N) ) or into decreasing order
(D(1) >= ... >= D(N) ), depending on ID.
INTEGER int
REAL float
DOUBLE PRECISION double
LOGICAL bool
CHARACTER char
COMPLEX std::complex<float>

INFO (output) INTEGER


= 0: successful exit
< 0: if INFO = -i, the i-th argument had an illegal value

DLASRT() SUBROUTINE
void
void dlasrt(char id, int n, double d[], int info);

std::vector<>

std::vector<> &v[0] v.data()

const

void dlasrt(char const & id, int const & n, double d[], int & info);

extern "C"
extern "C"
void dlasrt_(char const & id, int const & n, double d[], int & info);

g++ prog.cpp -llapack -lblas


http://www.iso.org/iso/home/store/
catalogue_ics/catalogue_detail_ics.htm?csnumber=64029

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3551.pdf

http://www.
cppstdlib.com

http://www.aristeia.com
&& ||

<cmath>
<cmath>
<cmath>
FE_OVERFLOW
|| FE_UNDERFLOW
INFINITY
& MATH_ERREXCEPT
&& MATH_ERRNO
*/ NAN
, NDEBUG
/* RAND_MAX
// and_eq
<< assert()
<algorithm> assign()
<array> at()
<cassert> before_begin()
<cerrno> break
<cfenv> capacity()
<cinttypes> case
<complex> cbefore_begin()
<cstddef> class
<cstdint>
<cstring> compl
<ctime> const
<ios> const_pointer
<iostream> const_reference
<iterator> constexpr
<limits> continue
<numeric> data()
<random> decltype()
<sstream> default
<string> difference_type
<vector> do while
CHAR_BIT else
FE_DIVBYZERO emplace()
FE_INVALID emplace_after()
emplace_back() std::atan()
emplace_front() std::atan2()
emplace_hint() std::atanh()
eof() std::back_insert_iterator<>
errno std::back_inserter()
std::begin()
std::bitset<>
extern std::boolalpha
false std::calloc()
fill() std::cbegin()
for std::cbrt()
get() std::ceil()
goto std::cend()
if std::cerr
inline std::cin
insert() std::clog
insert_after() std::conj()
key_comp() std::copysign()
main() std::cos()
math_errhandling std::cosh()
mutable std::cout
namespace std::default_random_engine
using std::defaultfloat
std::deque<>
std::distance()
noexcept std::div_t
nullptr std::end()
or_eq std::erf()
pointer std::erfc()
pop_back() std::exp()
precision() std::exp2()
push_back() std::expm1()
reference std::fabs()
reserve() std::fdim()
seekg() std::fetestexcept()
seekp() std::fixed
shrink_to_fit() std::floor()
size_type std::fma()
sizeof std::fmax()
static_assert() std::fmin()
static_cast<> std::fmod()
std::abs() std::forward_list<>
std::acos() std::free()
std::acosh() std::frexp()
std::advance() std::front_insert_iterator<>
std::arg()
std::array<> std::front_inserter()
std::asin() std::get()
std::asinh() std::hash<>
std::hypot() std::prev()
std::imaxdiv_t std::proj()
std::initializer_list<> std::ptrdiff_t
std::insert_iterator<> std::rand()
std::inserter() std::random_device
std::intmax_t std::real()
std::isinf() std::realloc()
std::isnan() std::remainder()
std::isnormal() std::remquo()
std::istream_iterator<> std::right
std::istringstream std::round()
std::iterator_traits<> std::scientific
std::ldexp() std::set<>
std::ldiv_t std::setfill()
std::left std::setprecision()
std::lgamma() std::setw()
std::list<> std::showpoint
std::lldiv_t std::showpos
std::log() std::sin()
std::log10() std::sinh()
std::log1p() std::size_t
std::log2() std::skipws
std::lround() std::sqrt()
std::make_move_iterator() std::srand()
std::make_pair() std::strerror()
std::make_tuple() std::string
std::malloc() std::swap()
std::map<> std::tan()
std::max() std::tanh()
std::min() std::tgamma()
std::minmax() std::time()
std::modf() std::trunc()
std::move() std::tuple<T1,T2,T3,...>
std::move_iterator std::u16string
std::multimap<> std::u32string
std::multiset<> std::uniform_int_distribution<int>
std::next()
std::noboolalpha std::uniform_real_distribution<double>
std::norm()
std::normal_distribution<double> std::vector<>
std::vector<bool>
std::noshowpoint std::wcerr
std::noshowpos std::wcin
std::noskipws std::wclog
std::ostream_iterator<> std::wcout
std::ostringstream std::wstring
std::pair<T1,T2> struct
std::polar() switch
std::pow() true
typedef std::logical_not<T>
typename std::logical_or<T>
using std::minus<T>
value_type std::modulus<T>
void std::multiplies<T>
while std::negate<T>
width() std::not_equal_to<T>
xor_eq std::plus<T>

long int
long long int
unsigned int
unsigned long int
unsigned long long int

std::bit_and<T>
std::bit_or<T>
std::bit_xor<T>
std::divides<T>
std::equal_to<T>
std::greater<T>
std::greater_equal<T>
std::less<T>
std::less_equal<T>
std::logical_and<T>
std::partial_sort_copy()
std::partial_sum()
std::partition()
std::partition_copy()
std::partition_point()
std::pop_heap()
std::accumulate() std::prev_permutation()
std::adjacent_difference() std::push_heap()
std::random_shuffle()
std::adjacent_find() std::remove()
std::all_of() std::remove_copy()
std::any_of() std::replace()
std::binary_search() std::replace_copy()
std::copy() std::reverse()
std::copy_backward() std::reverse_copy()
std::count() std::rotate()
std::equal() std::rotate_copy()
std::equal_range() std::search()
std::fill() std::search_n()
std::find() std::set_difference()
std::find_end() std::set_intersection()
std::find_first_of() std::set_symmetric_difference()
std::for_each()
std::generate() std::set_union()
std::includes() std::shuffle()
std::inner_product() std::sort()
std::inplace_merge() std::sort_heap()
std::iota() std::stable_partition()
std::is_heap() std::stable_sort()
std::is_heap_until() std::swap_ranges()
std::is_partitioned() std::transform()
std::is_permutation() std::unique()
std::is_sorted() std::unique_copy()
std::is_sorted_until() std::upper_bound()
std::lexicographical_compare()

std::lower_bound()
std::make_heap()
std::max_element()
std::merge()
std::min_element()
std::minmax_element()
std::mismatch()
std::move() struct
std::move_backward()
std::next_permutation()
std::none_of()
std::nth_element()
std::partial_sort()
|
|
<<
>>
&=
&
static_cast<> ~
^=
^

int
long int
long long int
short int

std::bind()
std::mem_fn()

constexpr imag()
real()

double
float
long double

char16_t
char32_t
char

wchar_t
namespace

Você também pode gostar