When testing code that must deal with sequences of inputs where the order of the arguments is not important, it is beneficial to test whether it results in the same output for all possible permutations of that input. Such a test could, for example, check whether a self-implemented sort algorithm sorts correctly.
No matter for what reason we need all permutations of some value range, std::next_permutation
can conveniently do it for us. We can invoke it on a modifiable range, and it changes the order of its items to the next lexicographical permutation.
In this section, we will write a program that reads multiple word strings from a standard input, and then we will use std::next_permutation
to generate and print all the permutations of those strings:
- First things first again; we include all the necessary headers and declare that we use the
std
namespace:
#include <iostream> #include <vector> #include <string...