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 of a and b
  • count(s.begin(), s.end(), 'x') counts the number of occurrences of the character 'x' in the string s
  • 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) - if c is a - z or A - Z
  • islower(c) - if c is a - z
  • isupper(c) - if c is A - Z
  • isdigit(c) - if c is 0 - 9
  • isalnum(c) - if c is a - z, A - Z or 0 - 9
  • isspace(c) - if c is a space character (e.g. space, new line, tab)

Type conversion:

  • to_string(n) - converts integer n to string
  • stoi(s) - converts string s to int
  • stoll(s) - converts string s to long long

Authored by s16f22