Today we practice file I/O while introducing more useful functions in the C++ STL.
Sorting
Apart from vectors and arrays, we can also use sort
on strings.
To sort a vector of int
in descending order,
use sort(v.begin(), v.end(), greater<int>())
.
<algorithm>
swap(a, b)
swaps the contents ofa
andb
count(s.begin(), s.end(), 'x')
counts the number of occurrences of the character'x'
in the strings
reverse(s.begin(), s.end())
reverses a sequence
<string>
Functions that check if a character is part of a category (returns a boolean):
isalpha(c)
- ifc
is a - z or A - Zislower(c)
- ifc
is a - zisupper(c)
- ifc
is A - Zisdigit(c)
- ifc
is 0 - 9isalnum(c)
- ifc
is a - z, A - Z or 0 - 9isspace(c)
- ifc
is a space character (e.g. space, new line, tab)
Type conversion:
to_string(n)
- converts integern
to stringstoi(s)
- converts strings
toint
stoll(s)
- converts strings
tolong long
Authored by s16f22