In [10]:
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <random>
#include <cmath>
#include <functional>
#include <numeric>

The following code uses transform to convert a string in place to uppercase using the toupper function and then transforms each char to its ordinal value:

In [2]:
std::string s("hello");
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) -> unsigned char { return std::toupper(c); });
 
std::vector<size_t> ordinals;
std::transform(s.begin(), s.end(), std::back_inserter(ordinals), [](unsigned char c) 
               -> size_t { return static_cast<size_t>(c); });
 
std::cout << s << ':';
for (size_t ord : ordinals) {
    std::cout << ' ' << ord;
}
HELLO: 72 69 76 76 79

The following code fills a vector with random numbers:

In [3]:
std::vector<int> v(5);
std::generate(v.begin(), v.end(), std::rand); // Using the C function rand()
 
std::cout << "v: ";
for (auto iv: v) {
    std::cout << iv << " ";
}
std::cout << "\n";
 
// Initialize with default values 0,1,2,3,4 from a lambda function
// Equivalent to std::iota(v.begin(), v.end(), 0);
int n = {0};
std::generate(v.begin(), v.end(), [&n]{ return n++; });
 
std::cout << "v: ";
for (auto iv: v) {
    std::cout << iv << " ";
}
std::cout << "\n";
v: 1458777923 2007237709 823564440 1115438165 1784484492 
v: 0 1 2 3 4 

The following code removes all spaces from a string by shifting all non-space characters to the left and then erasing the extra.

In [4]:
std::string str1 = "Text with some   spaces";
str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());
std::cout << str1 << '\n';
 
std::string str2 = "Text\n with\tsome \t  whitespaces\n\n";
str2.erase(std::remove_if(str2.begin(), str2.end(), [](char x){return std::isspace(x);}), str2.end());
std::cout << str2 << '\n';
Textwithsomespaces
Textwithsomewhitespaces

The following code randomly shuffles the integers 1..10:

In [5]:
std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
std::random_device rd;
std::mt19937 g(rd());
 
std::shuffle(v2.begin(), v2.end(), g);
 
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
7 8 5 2 10 1 9 6 4 3 
In [6]:
int digits[] = {3, 1, 4, 1, 5};
 
for (auto i : digits) std::cout << i << ' ';
std::cout << ": is_sorted: " << std::boolalpha << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';
 
std::sort(std::begin(digits), std::end(digits));
 
for (auto i : digits) std::cout << i << ' ';
std::cout << ": is_sorted: " << std::is_sorted(std::begin(digits), std::end(digits)) << '\n';
3 1 4 1 5 : is_sorted: false
1 1 3 4 5 : is_sorted: true
In [7]:
// remove duplicate elements (normal use)
std::vector<int> v3{1,2,3,1,2,3,3,4,5,4,5,6,7};
std::sort(v3.begin(), v3.end()); // 1 1 2 2 3 3 3 4 4 5 5 6 7 

auto last = std::unique(v3.begin(), v3.end());

// v now holds {1 2 3 4 5 6 7 x x x x x x}, where 'x' is indeterminate
v3.erase(last, v3.end()); 

for (int i : v3)
    std::cout << i << " ";
std::cout << "\n";
 
// remove consecutive spaces
std::string s1 = "wanna go    to      space?";

auto end = std::unique(s1.begin(), s1.end(), [](char l, char r){
    return std::isspace(l) && std::isspace(r) && l == r;
});

// s now holds "wanna go to space?xxxxxxxx", where 'x' is indeterminate
std::cout << std::string(s1.begin(), end) << '\n';
1 2 3 4 5 6 7 
wanna go to space?
In [9]:
static bool abs_compare(int a, int b) {
    return (std::abs(a) < std::abs(b));
}

std::cout << "larger of 1 and 9999: " << std::max(1, 9999) << '\n' 
          << "larger of 'a', and 'b': " << std::max('a', 'b') << '\n' 
          << "longest of \"foo\", \"bar\", and \"hello\": " 
          << std::max( { "foo", "bar", "hello" }, [](const std::string& s1, const std::string& s2) { 
              return s1.size() < s2.size();}) << '\n';

/////////////////////////////////////// max_element

std::vector<int> v4{ 3, 1, -14, 1, 5, 9 }; 
std::vector<int>::iterator result;
                          
result = std::max_element(v4.begin(), v4.end());
std::cout << "max element at: " << std::distance(v4.begin(), result) << '\n';
                          
result = std::max_element(v4.begin(), v4.end(), abs_compare);
std::cout << "max element (absolute) at: " << std::distance(v4.begin(), result);

///////////////////////////////// min max element

std::vector<int> v5 = { 3, 9, 1, 4, 2, 5, 9 };
     
auto result2 = std::minmax_element(v5.begin(), v5.end());
std::cout << "min element at: " << (result2.first - v5.begin()) << '\n';
std::cout << "max element at: " << (result2.second - v5.begin()) << '\n';
larger of 1 and 9999: 9999
larger of 'a', and 'b': b
longest of "foo", "bar", and "hello": hello
max element at: 5
max element (absolute) at: 2min element at: 2
max element at: 6
In [11]:
std::vector<int> v6{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
 
int sum = std::accumulate(v6.begin(), v6.end(), 0);
 
int product = std::accumulate(v6.begin(), v6.end(), 1, std::multiplies<int>());
 
std::string s3 = std::accumulate(std::next(v6.begin()), v6.end(), std::to_string(v6[0]), // start with first element
                                    [](std::string a, int b) {
                                        return a + '-' + std::to_string(b);
                                    });
 
std::cout << "sum: " << sum << '\n' << "product: " << product << '\n' << "dash-separated string: " << s3 << '\n';
sum: 55
product: 3628800
dash-separated string: 1-2-3-4-5-6-7-8-9-10