SlideShare a Scribd company logo
1 of 99
Download to read offline
STL Algorithms in Action
STL Algorithms and their
everyday application
Michael VanLoon
CPPcon 2015
STL Algorithms in Action
What are algorithms?
al∙go∙rithm
noun: algorithm; plural noun: algorithms
A procedure for solving a … problem (as of
finding the greatest common divisor) in a finite
number of steps that frequently involves
repetition of an operation; broadly: a step-by-
step procedure for solving a problem or
accomplishing some end especially by a
computer.
Merriam-Webster dictionary
Michael VanLoon - http://codeache.net - CPPcon 2015 2
STL Algorithms in Action
What are STL algorithms?
• A pre-built library of general-purpose algorithms
designed to solve specific problems
• Come “for free” with your C++ compiler
• Operate on sequences or sequenced containers
• Declarative in syntax – no explicit (“raw”) loops
• Iterate over some or all members of a sequence
performing an operation on each element in turn
• Designed by experts and proven bug-free by the
millions of lines of other peoples’ programs that
already use them!
Michael VanLoon - http://codeache.net - CPPcon 2015 3
STL Algorithms in Action
What is a raw loop anyway?
• It’s a for, while, or do… while loop
• Explicitly coded
• Often contains many lines of code (should it?)
• May cause side-effects outside its scope
vector<int> out;
bool found = false;
for (const auto& i: v) {
if (i >= 42) {
out.emplace_back(i);
++global_count;
if (i == 42) {
found = true;
}
}
}
Michael VanLoon - http://codeache.net - CPPcon 2015 4
STL Algorithms in Action
Why use algorithms?
• Often more efficient than hand-written loop
• Cleaner and more clearly abstracted than a
raw loop
• Contains side-effects inside a clear interface
• Prevents accidental leakage of side-effects
• Eases reasoning about functionality and
reasoning about post conditions
• Less likely to fail under non-obvious conditions
• Eases reasoning about the surrounding code
Michael VanLoon - http://codeache.net - CPPcon 2015 5
STL Algorithms in Action
Classes of STL algorithms
• Non-modifying sequence operations (25.2)
• Mutating sequence operations (25.3)
• Sorting and related operations (25.4)
• General C algorithms (25.5)
• General numeric operations (26.7)
(section of the C++ standard INCITS/ISO/IEC 14882-2011[2012])
Michael VanLoon - http://codeache.net - CPPcon 2015 6
STL Algorithms in Action
STL algorithms
non-modifying sequence operations
• Do not modify the input sequence.
• Do not emit a result sequence.
• Algorithm will not cause side-effects in
input sequence.
• Function object, if present, may cause side-
effects by modifying itself, the sequence (in
certain cases, e.g. for_each), or its
environment.
Michael VanLoon - http://codeache.net - CPPcon 2015 7
STL Algorithms in Action
STL algorithms
non-modifying sequence operations
• all_of
• any_of
• none_of
• for_each
• find
– find_if
– find_if_not
• find_end
• find_first_of
• adjacent_find
• count
– count_if
• mismatch
• equal
• is_permutation
• search
– search_n
Michael VanLoon - http://codeache.net - CPPcon 2015 8
STL Algorithms in Action
STL algorithms
mutating sequence operations
• Do not modify the input sequence, except in situations
when output overlaps input, resulting in modification in-
place (e.g. transform).
• Emit an output sequence of results.
• Output sequence may potentially overlap input
sequence for certain algorithms (e.g. transform).
Others (e.g. copy) explicitly disallow overlap/in-place.
• Algorithm will explicitly cause side-effects in output
sequence.
• Function object, if present, may cause side-effects by
modifying itself or its environment. Function object
should not modify the input or output sequences.
Michael VanLoon - http://codeache.net - CPPcon 2015 9
STL Algorithms in Action
STL algorithms
mutating sequence operations
• copy
– copy_n
– copy_if
– copy_backward
• move
– move_backward
• swap_ranges
– iter_swap
• transform
• replace
– replace_if
– replace_copy
– replace_copy_if
• fill
– fill_n
• generate
– generate_n
• remove
– remove_if
– remove_copy
– remove_copy_if
• unique
– unique_copy
• reverse
– reverse_copy
• rotate
– rotate_copy
• shuffle
– random_shuffle
• partition
– is_partitioned
– stable_partition
– partition_copy
– partition_point
Michael VanLoon - http://codeache.net - CPPcon 2015 10
STL Algorithms in Action
STL algorithms
sorting and related operations
• A mix of non-modifying and mutating
operations
• Mutating operations modify sequences in
place (e.g. sort, make_heap), or emit output
to an output sequence (e.g. merge,
partial_sort_copy)
• Default compare function is operator<
• Explicit compare function object, if supplied,
must not modify the sequence or iterators
Michael VanLoon - http://codeache.net - CPPcon 2015 11
STL Algorithms in Action
STL algorithms
sorting and related operations
• sorting
– sort
– stable_sort
– partial_sort
– partial_sort_copy
• nth_element
• binary search
– lower_bound
– upper_bound
– equal_range
– binary_search
• merge
– merge
– inplace_merge
• set operations on sorted
structures
– includes
– set_union
– set_intersection
– set_difference
– set_symmetric_difference
• heap operations
– push_heap
– pop_heap
– make_heap
– sort_heap
• minimum and maximum
– min
– max
– minmax
– min_element
– max_element
– minmax_element
• lexicographical comparisons
– lexicographical_compare
• permutation generators
– next_permutation
– prev_permutation
Michael VanLoon - http://codeache.net - CPPcon 2015 12
STL Algorithms in Action
STL algorithms
general numeric operations
• Library of algorithms for doing numeric
operations.
• Consist of components for complex number
types, random number generation, nu- meric
(n-at-a-time) arrays, generalized numeric
algorithms, and facilities included from the ISO
C library.1
1Description from the standard library that is surprisingly understandable
by humans.
Michael VanLoon - http://codeache.net - CPPcon 2015 13
STL Algorithms in Action
STL algorithms
general numeric operations
• accumulate
• inner_product
• partial_sum
• adjacent_difference
• iota
Michael VanLoon - http://codeache.net - CPPcon 2015 14
STL Algorithms in Action
STL algorithms
C library algorithms
• These are shown for completeness.
• You may need to know about these for
legacy reasons.
• In general, there is nothing these can do
that you can’t do better with the modern
algorithms previously mentioned.
Michael VanLoon - http://codeache.net - CPPcon 2015 15
STL Algorithms in Action
STL algorithms
C library algorithms
• bsearch
• qsort
Michael VanLoon - http://codeache.net - CPPcon 2015 16
STL Algorithms in Action
for_each and transform
• Your go-to generic algorithms for doing
general things to sequences
• Applies an operation to each element in a
sequence, in order
• Very similar except completely different
Michael VanLoon - http://codeache.net - CPPcon 2015 17
STL Algorithms in Action
for_each
• Applies an operation to each element in a sequence (like many
algorithms)
• Is a non-modifying sequence operation
• Algorithm produces no side-effect
• Function object may produce a side-effect by modifying the
input sequence
• Function object may produce a side-effect by modifying itself
• Returns a moved copy of the function object
• for_each is considered non-modifying because it produces
no output range; it relies on the function object for mutation, if
any
Michael VanLoon - http://codeache.net - CPPcon 2015 18
STL Algorithms in Action
transform
• Applies an operation to each element in a sequence (like many
algorithms)
• Is a mutating sequence operation
• If the input range(s) and result range are the same, or overlap,
mutates objects in-place
• Algorithm explicitly produces a side-effect
• Function object may not produce a side-effect
• transform is considered mutating because it explicitly
produces an output range modified by applying the function
object to elements, and forbids the function object from
modifying any of the range elements or iterators
• Returns iterator pointing one past last element in result range
Michael VanLoon - http://codeache.net - CPPcon 2015 19
STL Algorithms in Action
for_each example
Generate a single hash for all strings in a vector
struct HashString {
void operator()(const string& s) {
hash = accumulate(s.begin(), s.end(), hash, hash_char);
}
uint32_t hash = 0;
};
Michael VanLoon - http://codeache.net - CPPcon 2015 20
STL Algorithms in Action
accumulate
• Is a non-modifying numerics operation
• Algorithm produces no side-effect
• Function object may not modify the sequence or the iterator
• Function object may produce a side-effect by returning a return
code different from input parameter
• accumulate differs from for_each in that the algorithm
carries a value rather than a function object from visit to visit,
applying the operation to each element and the current value
• accumulate differs from for_each in that it has a default
operation: operator+
Michael VanLoon - http://codeache.net - CPPcon 2015 21
STL Algorithms in Action
for_each example
Generate a single hash for all strings in a vector
struct HashString {
void operator()(const string& s) {
hash = accumulate(s.begin(), s.end(), hash, hash_char);
}
uint32_t hash = 0;
};
Michael VanLoon - http://codeache.net - CPPcon 2015 22
STL Algorithms in Action
for_each example
Generate a single hash for all strings in a vector
struct HashString {
void operator()(const string& s) {
hash = accumulate(s.begin(), s.end(), hash, hash_char);
}
uint32_t hash = 0;
};
template<typename Cont>
uint32_t hash_all_strings(const Cont& v) {
const auto hasher = for_each(v.begin(), v.end(), HashString());
return hasher.hash;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 23
STL Algorithms in Action
for_each example
Generate a single hash for all strings in a vector
struct HashString {
void operator()(const string& s) {
hash = accumulate(s.begin(), s.end(), hash, hash_char);
}
uint32_t hash = 0;
};
template<typename Cont>
uint32_t hash_all_strings(const Cont& v) {
const auto hasher = for_each(v.begin(), v.end(), HashString());
return hasher.hash;
}
void test_for_each_hash() {
vector<string> v{ "one", "two", "three", "four", "five" };
uint32_t hash = hash_all_strings(v);
cout << "Hash: " << hash << dec << endl;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 24
STL Algorithms in Action
for_each example (cont)
… the rest of the code…
uint32_t rotl(uint32_t value, unsigned int count) {
const uint32_t mask =
(CHAR_BIT * sizeof(value) - 1);
count &= mask;
return (value << count) |
(value >> ((-count)&mask));
}
uint32_t hash_char(uint32_t hash, char c)
{
hash = rotl(hash, c); // circular rotate left
hash ^= c;
return hash;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 25
STL Algorithms in Action
transform example
Generate hash for each string in a vector
uint32_t hash_string(const string& s) {
return accumulate(s.begin(), s.end(), 0, hash_char);
};
Michael VanLoon - http://codeache.net - CPPcon 2015 26
STL Algorithms in Action
transform example
Generate hash for each string in a vector
uint32_t hash_string(const string& s) {
return accumulate(s.begin(), s.end(), 0, hash_char);
};
template<typename Cont>
vector<uint32_t> hash_each_string(const Cont& v) {
vector<uint32_t> res;
transform(v.begin(), v.end(), back_inserter(res), hash_string);
return res;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 27
STL Algorithms in Action
transform example
Generate hash for each string in a vector
uint32_t hash_string(const string& s) {
return accumulate(s.begin(), s.end(), 0, hash_char);
};
template<typename Cont>
vector<uint32_t> hash_each_string(const Cont& v) {
vector<uint32_t> res;
transform(v.begin(), v.end(), back_inserter(res), hash_string);
return res;
}
void test_transform_hash() {
vector<string> v{ "one", "two", "three", "four", "five" };
auto res = hash_each_string(v);
cout << "Hashes: ";
for_each(res.begin(), res.end(),
[](uint32_t rh){ cout << rh << " "; });
cout << endl;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 28
STL Algorithms in Action
any_of, all_of, and none_of
• Apply a function object to a sequence
• Determines whether any, all, or none of the
elements in the sequence are true as
determined by the function object
• May return before evaluating all elements
in sequence if outcome is determined early
Michael VanLoon - http://codeache.net - CPPcon 2015 29
STL Algorithms in Action
all_of example
validate http headers
static const regex
reHeader("([A-Za-z0-9!#$%&'*+.^_`|~-]+): *(.+) *");
inline bool
headers_valid(const vector<string>& headers) {
return all_of(headers.begin(), headers.end(),
[](const auto& header) -> bool {
smatch matches;
return regex_match(header, matches, reHeader);
}
);
}
Michael VanLoon - http://codeache.net - CPPcon 2015 30
STL Algorithms in Action
all_of example
validate http headers test and output
void all_of_headers() {
vector<string> h1 = { "Foo: bar", "Content-type: application/json",
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha << headers_valid(h1) << endl;
vector<string> h2 = { "Foo : bar", "Content-type: application/json",
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha << headers_valid(h2) << endl;
vector<string> h3 = { "Foo: bar", "Content-type: application/json",
":Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha << headers_valid(h3) << endl;
vector<string> h4 = { "Foo: bar", " Content-type: application/json"
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha << headers_valid(h4) << endl;
}
output:
headers valid: true
headers valid: false
headers valid: false
headers valid: false
Michael VanLoon - http://codeache.net - CPPcon 2015 31
STL Algorithms in Action
any_of example
http header search
inline bool
header_search(const vector<string>& headers,
const string& find_header, const string& find_value)
{
return any_of(headers.begin(), headers.end(),
[&find_header, &find_value](const auto& header) -> bool {
const regex reHeader(
"(" + find_header + "): *(" + find_value + ") *",
regex::icase);
smatch matches;
return regex_match(header, matches, reHeader);
}
);
}
Michael VanLoon - http://codeache.net - CPPcon 2015 32
STL Algorithms in Action
any_of example
http header search test and output
void any_of_headers_simple() {
vector<string> h1 = { "Foo: bar", "Content-type: application/json",
"X-SuperPower: toestrength",
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha
<< header_search(h1, "X-SuperPower", "toestrength") << endl;
vector<string> h2 = { "Foo: bar", "Content-type: application/json",
"X-SuperPower: supersmell",
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha
<< header_search(h2, "X-SuperPower", "toestrength") << endl;
vector<string> h3 = { "Foo : bar", "Content-type: application/json",
"X-SuperPower: toestrength",
"Accept: text/html,text/json,application/json" };
cout << "headers valid: " << boolalpha
<< header_search(h3, "X-Superpower", "toeStrength") << endl;
}
output:
headers valid: true
headers valid: false
headers valid: true
Michael VanLoon - http://codeache.net - CPPcon 2015 33
STL Algorithms in Action
another for_each example
simultaneously validate and search http headers
struct HeaderData {
int good_headers = 0;
int bad_headers = 0;
multimap<string, string> found_headers;
string find_header;
string find_value;
operator bool() const { return !bad_headers && good_headers > 0; }
void operator() (const string& header) {
static const regex reValid("([A-Za-z0-9!#$%&'*+.^_`|~-]+): *(.+) *");
smatch matches;
bool match = regex_match(header, matches, reValid);
if (match) {
++good_headers;
const regex reHeader("(" + find_header + "): *(" + find_value + ") *", regex::icase);
if (regex_match(header, matches, reHeader)) {
found_headers.emplace(matches[1], matches[2]);
}
} else {
++bad_headers;
}
}
};
Michael VanLoon - http://codeache.net - CPPcon 2015 34
STL Algorithms in Action
another for_each example
simultaneously validate and search http headers
struct HeaderData {
int good_headers = 0;
int bad_headers = 0;
multimap<string, string> found_headers;
string find_header;
string find_value;
operator bool() const;
void operator() (const string& header);
};
const HeaderData header_parse(const vector<string>& headers, const string&
find_header, const string& find_value) {
HeaderData hd;
hd.find_header = find_header;
hd.find_value = find_value;
return for_each(headers.begin(), headers.end(), hd);
}
Michael VanLoon - http://codeache.net - CPPcon 2015 35
STL Algorithms in Action
another for_each example
simultaneous validate/search test
void any_of_headers_full() {
{
vector<string> h1 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: toestrength",
"Accept: text/html,text/json,application/json" };
const HeaderData& hd = header_parse(h1, "X-SuperPower", "toestrength");
cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers;
for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) {
cout << "nt'" << val.first << "', '" << val.second << "'";
});
cout << endl;
}
{
vector<string> h2 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: supersmell", "Accept: text/ht
ml,text/json,application/json" };
const HeaderData& hd = header_parse(h2, "X-SuperPower", "toestrength");
cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers;
for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) {
cout << "nt'" << val.first << "', '" << val.second << "'";
});
cout << endl;
}
{
vector<string> h3 = { "Foo : bar", "Content-type: application/json", "X-Superpower: toestrength", "Accept: text/
html,text/json,application/json" };
const HeaderData& hd = header_parse(h3, "X-SuperPower", "toestrength");
cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers;
for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) {
cout << "nt'" << val.first << "', '" << val.second << "'";
});
cout << endl;
}
}
Michael VanLoon - http://codeache.net - CPPcon 2015 36
STL Algorithms in Action
another for_each example
simultaneous validate/search output
output:
headers parse: true, good 4, bad 0
'X-SuperPower', 'toestrength'
headers parse: true, good 4, bad 0
headers parse: false, good 3, bad 1
'X-Superpower', 'toestrength'
Michael VanLoon - http://codeache.net - CPPcon 2015 37
STL Algorithms in Action
adjacent_find
• adjacent_find searches for adjacent items
(pairs of elements next to each other) in a
sequence that meet a certain condition.
• Returns an iterator to the first of the pair of
elements meeting the condition.
• The default condition is equality (i.e. find two
adjacent items that are equal).
• A custom comparator may be provided to look
for other adjacent conditions.
Michael VanLoon - http://codeache.net - CPPcon 2015 38
STL Algorithms in Action
adjacent_find example
simple is_sorted implementation
vecInt_t v{ 1, 2, 3, 4, 5, 5, 6, 7, 8 };
// Greater works because it's asking if the first value is
// greater than the second value. If so, then the test
// fails (not sorted). If the first value is less than or
// equal to the second value, no match and success.
vecInt_t::iterator it =
adjacent_find(v.begin(), v.end(), greater<int>());
if (it == v.end())
cout << "Vector is sorted" << endl;
else
cout << "Vector not sorted, value " << *(it + 1)
<< ", at position " << it - v.begin() + 1 << endl;
output:
Vector is sorted
Vector not sorted, value 3, at position 9
Michael VanLoon - http://codeache.net - CPPcon 2015 39
STL Algorithms in Action
adjacent_find example
test for sequence deviation
template<typename Cont>
typename Cont::const_iterator checkDeviation(const Cont& cont,
double allowed_dev)
{
return adjacent_find(cont.begin(), cont.end(),
[allowed_dev](const typename
Cont::value_type& v1,
const typename
Cont::value_type& v2)
{
auto limit = v1 * allowed_dev;
return (v2 > v1 + limit) ||
(v2 < v1 - limit);
});
}
Michael VanLoon - http://codeache.net - CPPcon 2015 40
STL Algorithms in Action
adjacent_find example
test for sequence deviation test and output
vecDbl_t v{ 1.0, 1.05, 1.06, 1.04, 1.09, 1.15, 1.2 };
vecDbl_t::const_iterator it = checkDeviation(v, 0.1);
if (it == v.end())
cout << "Vector is within deviation limits" << endl;
else
cout << "Vector outside deviation limits, values " << *it << " and ”
<< *(it + 1) << ", at position " << it - v.begin() + 1 << endl;
v.push_back(2.0);
it = checkDeviation(v, 0.1);
if (it == v.end())
cout << "Vector is within deviation limits" << endl;
else
cout << "Vector outside deviation limits, values " << *it << " and ”
<< *(it + 1) << ", at position " << it - v.begin() + 1 << endl;
output:
Vector is within deviation limits
Vector outside deviation limits, values 1.2 and 2, at position 7
Michael VanLoon - http://codeache.net - CPPcon 2015 41
STL Algorithms in Action
remove_if (with erase)
Scott Meyers, “Effective STL,” items 9 and 32
• Scenario: you want to erase several items
from a container that meet a condition
• You could write a loop with some checks,
some explicit erases, and potential iterator
invalidation
• Or…
Michael VanLoon - http://codeache.net - CPPcon 2015 42
STL Algorithms in Action
remove_if (with erase)
Scott Meyers, “Effective STL,” items 9 and 32
struct Foo {
bool expired;
… other members…
};
vector<Foo> v;
v.erase(
remove_if(v.begin(), v.end(),
[](const Foo& foo){ return foo.expired; }),
v.end());
Output:
before: [val: 1, expired: false] [val: 2, expired: true] [val: 3,
expired: false] [val: 4, expired: false] [val: 5, expired: true]
after: [val: 1, expired: false] [val: 3, expired: false] [val: 4,
expired: false]
Michael VanLoon - http://codeache.net - CPPcon 2015 43
STL Algorithms in Action
Know your sorts and sorta-sorts
Scott Meyers’ “Effective STL” Item 31
• Sorting algorithms:
– sort1
– stable_sort1
– partial_sort, partial_sort_copy1
• Sorta-sorts:
– nth_element1
– partition, partition_copy2
– stable_partition2
1 Requires random access iterators
2 Requires bidirectional iterators
Michael VanLoon - http://codeache.net - CPPcon 2015 44
STL Algorithms in Action
Know your sorts
Scott Meyers’ “Effective STL” Item 31
• sort
– Most general-purpose sort
– Order of equivalent items
implementation-defined
– In some cases, may be more
efficient than stable_sort
since equivalent items can be
rearranged at sort’s discretion
– Sorts in place
• stable_sort
– Order of equivalent items
preserved
– Sorts in place
• partial_sort
– Sort a subset of a sequence,
drawing from a subset that is
equal to or larger than the sorted
sequence
– There is no stable version of
partial_sort
• partial_sort_copy
– Like partial_sort, but…
– Sorts specified subset of an
input sequence, emitting to an
output sequence
Michael VanLoon - http://codeache.net - CPPcon 2015 45
STL Algorithms in Action
sort and stable_sort
sort objects by last name, first, middle
• Scenario: assume an object with strings
containing fist name, middle name, and last
name, among other things
struct Person {
string first;
string middle;
string last;
… other Person stuff…
};
• We want to sort said objects by all three fields,
with precedence last > first > middle
Michael VanLoon - http://codeache.net - CPPcon 2015 46
STL Algorithms in Action
sort and stable_sort
sort objects by last name, first, middle
vector<Person> v{{
{ "Joe", "P", "Smith" },
{ "Jane", "Q", "Jones" },
{ "Frank", "P", "Johnson" },
{ "Sarah", "B", "Smith" },
{ "Joe", "X", "Jones" },
{ "Joe", "A", "Smith" } }};
// Sort by least influential data first
sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.middle < b.middle; });
Michael VanLoon - http://codeache.net - CPPcon 2015 47
STL Algorithms in Action
sort and stable_sort
sort objects by last name, first, middle
vector<Person> v{{
{ "Joe", "P", "Smith" },
{ "Jane", "Q", "Jones" },
{ "Frank", "P", "Johnson" },
{ "Sarah", "B", "Smith" },
{ "Joe", "X", "Jones" },
{ "Joe", "A", "Smith" } }};
// Sort by least influential data first
sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.middle < b.middle; });
stable_sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.first < b.first; });
Michael VanLoon - http://codeache.net - CPPcon 2015 48
STL Algorithms in Action
sort and stable_sort
sort objects by last name, first, middle
vector<Person> v{{
{ "Joe", "P", "Smith" },
{ "Jane", "Q", "Jones" },
{ "Frank", "P", "Johnson" },
{ "Sarah", "B", "Smith" },
{ "Joe", "X", "Jones" },
{ "Joe", "A", "Smith" } }};
// Sort by least influential data first
sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.middle < b.middle; });
stable_sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.first < b.first; });
stable_sort(v.begin(), v.end(),
[](const Person& a, const Person& b){ return a.last < b.last; });
// Sort by most influential data last
Michael VanLoon - http://codeache.net - CPPcon 2015 49
STL Algorithms in Action
sort and stable_sort
visual: sort objects by last name, first, middle
Michael VanLoon - http://codeache.net - CPPcon 2015 50
STL Algorithms in Action
partial_sort
• Takes an input sequence of sort candidates
• Sorts the top n elements into a potentially smaller
output sequence
• Order of items from input sequence that are
unsorted in output sequence are in implementation
defined order
• partial_sort is an in-place operation
• partial_sort_copy copies sorted output to a
separate sequence
• partial_sort is obviously more efficient than a
full sort
Michael VanLoon - http://codeache.net - CPPcon 2015 51
STL Algorithms in Action
partial_sort example
vector<int> v1{ 42, 17, 89, 22,
34, 78, 63, 12,
57, 99 };
partial_sort(v1.begin(),
v1.begin() + 5,
v1.begin() + 8,
greater<int>());
Michael VanLoon - http://codeache.net - CPPcon 2015 52
STL Algorithms in Action
partial_sort example
vector<int> v1{ 42, 17, 89, 22,
34, 78, 63, 12,
57, 99 };
partial_sort(v1.begin(),
v1.begin() + 5,
v1.begin() + 8,
greater<int>());
Michael VanLoon - http://codeache.net - CPPcon 2015 53
STL Algorithms in Action
partial_sort example
vector<int> v1{ 42, 17, 89, 22,
34, 78, 63, 12,
57, 99 };
partial_sort(v1.begin(),
v1.begin() + 5,
v1.begin() + 8,
greater<int>());
Michael VanLoon - http://codeache.net - CPPcon 2015 54
STL Algorithms in Action
partial_sort example
visual
42
17
89
22
34
78
63
12
57
99
89
78
63
42
34
17
22
12
57
99
first
last
first
middle
sort input range
sort output range
Michael VanLoon - http://codeache.net - CPPcon 2015 55
STL Algorithms in Action
Know your sorta-sorts
Scott Meyers’ “Effective STL” Item 31
• partition
– Reorder sequence so all
items before partition point
are less, and all items after
are not less
– Order of items in lower and
upper subsets is
implementation defined
– Order of equivalent items is
implementation-defined
– Operates in place
• stable_partition
– Like partition, but…
– Order of equivalent items is
preserved
– Operates in place
• partition_copy
– Like partition, but…
– Order of equivalent items is
implementation-defined
– Copies items from input
sequence to one of two
output sequences depending
on whether supplied function
object returns false or true for
each item
– There is no
stable_partition_copy
Michael VanLoon - http://codeache.net - CPPcon 2015 56
STL Algorithms in Action
Know your sorta-sorts, cont.
Scott Meyers’ “Effective STL” Item 31
• nth_element
– Reorders sequence such that all items before “nth”
are less, and all items after “nth” are not less
– Order of items in lower and upper subsets is
implementation defined
– Order of equivalent items is implementation-
defined
– “nth” element is exactly the value that would exist
in a fully sorted sequence (but without fully sorting)
– Operates in place
Michael VanLoon - http://codeache.net - CPPcon 2015 57
STL Algorithms in Action
Know your sorta-sorts, cont.
Comparison between partition and nth_element
partition
• partitions a sequence, based
on condition
• partition point is not
guaranteed to be value that
would be at that position in
fully sorted sequence
• input:
– sequence begin, end
– comparison function
• output:
– reordered sequence
– iterator to partition point
nth_element
• partitions a sequence, based
on position
• nth element is element that
would exist in that position in
fully sorted sequence
• input:
– sequence begin, end
– iterator to “nth” position
– optional comparison function
• output:
– reordered sequence, partitioned
around nth element
Michael VanLoon - http://codeache.net - CPPcon 2015 58
STL Algorithms in Action
partition example
partition elements around 50
vector<int> v{ 12, 89, 31, 18,
7, 72, 69, 50,
49, 50, 51, 49 };
vector<int>::iterator part_it =
partition(v.begin(),
v.end(),
[](const int i) {
return i < 50;
});
Michael VanLoon - http://codeache.net - CPPcon 2015 59
STL Algorithms in Action
partition example
partition elements around 50
vector<int> v{ 12, 89, 31, 18,
7, 72, 69, 50,
49, 50, 51, 49 };
vector<int>::iterator part_it =
partition(v.begin(),
v.end(),
[](const int i) {
return i < 50;
});
Michael VanLoon - http://codeache.net - CPPcon 2015 60
STL Algorithms in Action
partition example
visual: partition elements around 50
Michael VanLoon - http://codeache.net - CPPcon 2015 61
12
89
31
18
7
72
69
50
12
49
31
18
7
49
69
50
partition
point
49
50
51
49
72
50
51
89
STL Algorithms in Action
partition_copy example
partition elements around 50
vector<int> v{ 12, 89, 31, 18,
7, 72, 69, 50,
49, 50, 51, 49 };
partition_copy(v.begin(),
v.end(),
back_inserter(smaller),
back_inserter(larger),
[](const int i) {
return i < 50;
});
Michael VanLoon - http://codeache.net - CPPcon 2015 62
STL Algorithms in Action
partition_copy example
visual: partition elements around 50
Michael VanLoon - http://codeache.net - CPPcon 2015 63
STL Algorithms in Action
Mixing up partitions!
partition_copy, partition, and stable_partition example
• Extend our prior Person struct to be an Employee
struct
• Partition Management and Individuals into two
separate containers
• Partition Management between Executives and
Managers
• Partition Architects before other Individuals
• Stable partition the Senior and Junior employees
among already partitioned individuals, leaving
already partitioned elements in same relative
position
Michael VanLoon - http://codeache.net - CPPcon 2015 64
STL Algorithms in Action
Mixing up partitions!
some data…
struct Employee: public Person {
enum class EmployeeType {
Executive, Manager, Architect, Senior, Junior,
Management = Manager,
Individual = Architect
};
EmployeeType type;
… additional members and methods…
};
vector<Employee> v{
{ "Joe", "P", "Smith", Employee::EmployeeType::Manager },
{ "Jane", "Q", "Jones", Employee::EmployeeType::Junior },
{ "Frank", "P", "Johnson", Employee::EmployeeType::Architect },
{ "Sarah", "B", "Smith", Employee::EmployeeType::Executive },
{ "Joe", "X", "Jones", Employee::EmployeeType::Senior },
{ "Joe", "A", "Smith", Employee::EmployeeType::Junior },
{ "Chris", "M", "Williams", Employee::EmployeeType::Manager }
};
Michael VanLoon - http://codeache.net - CPPcon 2015 65
STL Algorithms in Action
Mixing up partitions!
separate employees into management and individuals
vector<Employee> management, individuals;
partition_copy(v.begin(), v.end(),
back_inserter(management), back_inserter(individuals),
[](const Employee& e) {
return e.type <= Employee::EmployeeType::Manager;
});
Michael VanLoon - http://codeache.net - CPPcon 2015 66
vector<Employee>::iterator car_it =
partition(management.begin(), management.end(),
[](const Employee& e) {
return e.type == Employee::EmployeeType::Executive;
});
vector<Employee>::iterator jet_it = management.begin();
cout << "Management partitioned:n";
for (auto it = management.begin(); it != management.end(); ++it) {
cout << (it < car_it ? "tjet" : "tcar") << *it;
}
executives get the company jet; managers get the company car
STL Algorithms in Action
Mixing up partitions!
architects get the company segway; everyone else gets the bike
vector<Employee>::iterator bike_it =
partition(individuals.begin(), individuals.end(),
[](const Employee& e) {
return e.type == Employee::EmployeeType::Architect;
});
vector<Employee>::iterator segway_it = individuals.begin();
cout << "Architects partitioned:n";
for (auto it = individuals.begin(); it != individuals.end(); ++it) {
cout << (it < bike_it ? "tsegway" : "tbike") << *it;
}
Michael VanLoon - http://codeache.net - CPPcon 2015 67
vector<Employee>::iterator old_bike_it =
stable_partition(individuals.begin(), individuals.end(),
[](const Employee& e) {
return e.type <= Employee::EmployeeType::Senior;
});
cout << "Individuals partitioned:n";
for (auto it = individuals.begin(); it != individuals.end(); ++it) {
cout << (it < bike_it ? "tsegway" :
(it < old_bike_it ? "tnewbike" : "toldbike")) << *it;
}
partition the non-architects from higher to lower seniority
STL Algorithms in Action
Mixing up partitions!
example output
Management partitioned:
jet Sarah B Smith: Executive
car Joe P Smith: Manager
car Chris M Williams: Manager
Architects partitioned (junior/senior unpartitioned):
segway Frank P Johnson: Architect
bike Jane Q Jones: Junior
bike Joe X Jones: Senior
bike Joe A Smith: Junior
Individuals fully partitioned:
segway Frank P Johnson: Architect
newbike Joe X Jones: Senior
oldbike Jane Q Jones: Junior
oldbike Joe A Smith: Junior
Michael VanLoon - http://codeache.net - CPPcon 2015 68
STL Algorithms in Action
nth_element (not a super-hero, but still pretty super)
find median value in sequence
vector<int> v{ 12, 2, 89, 78, 18, 7, 72,
69, 81, 50, 49, 50, 51, 49 };
const size_t nth = v.size() / 2;
nth_element(v.begin(), v.begin() + nth, v.end());
output:
49 2 12 49 18 7 50 >50< 51 78 72 69 81 89
Michael VanLoon - http://codeache.net - CPPcon 2015 69
find percentile value in sequence
vector<int> v{ 12, 2, 89, 78, 18, 7, 72,
69, 81, 50, 49, 50, 51, 49 };
const size_t percentile = 75;
const size_t nth = v.size() * percentile / 100;
nth_element(v.begin(), v.begin() + nth, v.end());
output:
49 2 12 49 18 7 50 50 51 69 >72< 78 81 89
STL Algorithms in Action
partial_sort vs. nth_element + sort
• Scenario: find top (say 20) web pages by
number of hits, among a much larger set
• We have a large sequence where we are only
concerned with the top 20 elements
• We want the top 20 elements of this sequence
in order; we don’t care about the rest
• Requirement: top 20 must be partitioned to
front of sequence, before the don’t-cares
• Requirement: top 20 must be sorted
Michael VanLoon - http://codeache.net - CPPcon 2015 70
STL Algorithms in Action
partial_sort vs. nth_element + sort
partial_sort(vec.begin(),
vec.begin() + 20,
vec.end(),
greater<int64_t>());
nth_element(vec.begin(),
vec.begin() + 20,
vec.end(),
greater<int64_t>());
sort(vec.begin(), vec.begin() + 20,
greater<int64_t>());
Michael VanLoon - http://codeache.net - CPPcon 2015 71
STL Algorithms in Action
partial_sort vs. nth_element + sort
• Are they equivalent?
vector<int64_t> v1 = init_vec();
vector<int64_t> v2 = v1; // make a copy
partial_sort(v1.begin(), v1.begin() + 20, v1.end(), greater<int64_t>());
nth_element(v2.begin(), v2.begin() + 20, v2.end(), greater<int64_t>());
sort(v2.begin(), v2.begin() + 20, greater<int64_t>());
cout << "sorted portions of vectors are equal: " << boolalpha
<< equal(v1.begin(), v1.begin() + 20, v2.begin()) << endl;
cout << "unsorted portions of vectors are equal: " << boolalpha
<< equal(v1.begin() + 20, v1.end(), v2.begin() + 20) << endl;
output:
sorted portions of vectors are equal: true
unsorted portions of vectors are equal: false
Michael VanLoon - http://codeache.net - CPPcon 2015 72
STL Algorithms in Action
rotate
• Moves elements specified in a source range to
destination position, while moving displaced items to
source position
• Simple way to move one or more elements from one
position in a container to another without using erase and
insert
• Technically, this is a left rotate
• rotate moves element in the sequence in-place
• rotate_copy does the same, except that it copies the
elements to a separate sequence
• Requires:
– [first, middle) and [middle, last) are valid ranges
– i.e. first <= middle <= last
Michael VanLoon - http://codeache.net - CPPcon 2015 73
STL Algorithms in Action
rotate
A naïve implementation – move one item in sequence
vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int>::iterator it = v.begin() + 2;
int i = *it;
v.erase(it);
vector<int>::iterator it2 = v.begin() + 6 - 1;
v.insert(it2, i);
Output:
before: 0 1 2 3 4 5 6 7 8 9
After: 0 1 3 4 5 2 6 7 8 9
Michael VanLoon - http://codeache.net - CPPcon 2015 74
STL Algorithms in Action
rotate
A naïve implementation – move one item in sequence
vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int>::iterator it = v.begin() + 2;
int i = *it;
v.erase(it);
vector<int>::iterator it2 = v.begin() + 6 - 1;
v.insert(it2, i);
Output:
before: 0 1 2 3 4 5 6 7 8 9
After: 0 1 3 4 5 2 6 7 8 9
Michael VanLoon - http://codeache.net - CPPcon 2015 75
STL Algorithms in Action
rotate
A better implementation – move one item in sequence
vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int>::iterator it = v.begin() + 2;
vector<int>::iterator it2 = v.begin() + 6;
rotate(it, it + 1, it2);
Output:
before: 0 1 2 3 4 5 6 7 8 9
After: 0 1 3 4 5 2 6 7 8 9
Michael VanLoon - http://codeache.net - CPPcon 2015 76
STL Algorithms in Action
rotate
Rotate a range of items
vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int>::iterator it = v.begin() + 2;
vector<int>::iterator it2 = v.begin() + 7;
rotate(it, it + 3, it2);
Output:
before: 0 1 2 3 4 5 6 7 8 9
after: 0 1 5 6 2 3 4 7 8 9
Michael VanLoon - http://codeache.net - CPPcon 2015 77
STL Algorithms in Action
rotate
GCC’s definition
• Rotates the elements of the range
[first, last) by (middle - first)
positions so that the element at middle is
moved to first, the element at middle
+ 1 is moved to first + 1 and so on
for each element in the range.
• This effectively swaps the ranges [first,
middle) and [middle, last).
Michael VanLoon - http://codeache.net - CPPcon 2015 78
STL Algorithms in Action
slide (move range of elements to new position)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would move a range
to a point in either direction without
worrying about iterator ordering?
• For example, move a range of
selected GUI items to a new position
in a list.
• When using rotate, it is important
to get the order of iterators correct:
first <= middle <= last.
• Bonus: return a pair indicating
destination range of relocated
elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 79
STL Algorithms in Action
slide (move range of elements to new position)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would move a range
to a point in either direction without
worrying about iterator ordering?
• For example, move a range of
selected GUI items to a new position
in a list.
• When using rotate, it is important
to get the order of iterators correct:
first <= middle <= last.
• Bonus: return a pair indicating
destination range of relocated
elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 80
STL Algorithms in Action
slide (move range of elements to new position)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would move a range
to a point in either direction without
worrying about iterator ordering?
• For example, move a range of
selected GUI items to a new position
in a list.
• When using rotate, it is important
to get the order of iterators correct:
first <= middle <= last.
• Bonus: return a pair indicating
destination range of relocated
elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 81
STL Algorithms in Action
slide (move range of elements to new position)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would move a range
to a point in either direction without
worrying about iterator ordering?
• For example, move a range of
selected GUI items to a new position
in a list.
• When using rotate, it is important
to get the order of iterators correct:
first <= middle <= last.
• Bonus: return a pair indicating
destination range of relocated
elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 82
STL Algorithms in Action
slide (move range of elements to new position)
Sean Parent, “C++ Seasoning”
Michael VanLoon - http://codeache.net - CPPcon 2015 83
template <typename I> // I models RandomAccessIterator
auto slide(I f, I l, I p) -> pair<I, I> {
if (p < f)
return { p, rotate(p, f, l) };
if (l < p)
return { rotate(f, l, p), p };
return { f, l };
}
STL Algorithms in Action
gather (gather multiple elements around a point)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would allow a scattered
selection of elements to be gathered to a
specific location?
• For example, move a range of scattered
multi-selected GUI items to a new
position in a list, gathered together.
• How can you gather diverse elements to
a single location without loops and
special-casing code?
• Bonus: return a pair indicating destination
range of relocated elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 84
STL Algorithms in Action
gather (gather multiple elements around a point)
Sean Parent, “C++ Seasoning”
• What if you wanted to implement an
algorithm that would allow a scattered
selection of elements to be gathered to a
specific location?
• For example, move a range of scattered
multi-selected GUI items to a new
position in a list, gathered together.
• How can you gather diverse elements to
a single location without loops and
special-casing code?
• Bonus: return a pair indicating destination
range of relocated elements.
Michael VanLoon - http://codeache.net - CPPcon 2015 85
STL Algorithms in Action
gather (gather multiple elements around a point)
Sean Parent, “C++ Seasoning”
• Break the problem space into
two pieces:
– The sequence before the
destination position
– The sequence after the
destination position
• What algorithm can gather the
select items while maintaining
their relative position?
Michael VanLoon - http://codeache.net - CPPcon 2015 86
STL Algorithms in Action
gather (gather multiple elements around a point)
Sean Parent, “C++ Seasoning”
• Break the problem space
into two pieces:
– The sequence before the
destination position
– The sequence after the
destination position
• That looks a lot like
partitioning…
Michael VanLoon - http://codeache.net - CPPcon 2015 87
STL Algorithms in Action
gather (gather multiple elements around a point)
Sean Parent, “C++ Seasoning”
Michael VanLoon - http://codeache.net - CPPcon 2015 88
template <typename I, // models BidirectionalIterator
typename S> // models UnaryPredicate
auto gather(I f, I l, I p, S s) -> pair<I, I>
{
return { stable_partition(f, p, not1(s)),
stable_partition(p, l, s) };
}
STL Algorithms in Action
set_difference
process configuration updates
• Scenario: you store some configuration
information in an ordered container
• You receive updated configuration on a
regular basis
• You want to quickly determine what has
changed: i.e. what new items have been
added, and what items have been removed
Michael VanLoon - http://codeache.net - CPPcon 2015 89
STL Algorithms in Action
set_difference
process configuration updates
vector<string> new_items, removed_items;
set_difference(current.begin(), current.end(),
update.begin(), update.end(),
back_inserter(removed_items));
remove_config(removed_items);
set_difference(update.begin(), update.end(),
current.begin(), current.end(),
back_inserter(new_items));
add_config(new_items);
Michael VanLoon - http://codeache.net - CPPcon 2015 90
STL Algorithms in Action
set_difference
process configuration updates test example
set<string> current { "one", "two", "three", "four", "five" };
set<string> update { "one", "three", "four", "six", "seven" };
vector<string> new_items, removed_items;
set_difference(current.begin(), current.end(),
update.begin(), update.end(),
back_inserter(removed_items));
remove_config(removed_items);
set_difference(update.begin(), update.end(),
current.begin(), current.end(),
back_inserter(new_items));
add_config(new_items);
output:
Removed: five two
Added: seven six
Michael VanLoon - http://codeache.net - CPPcon 2015 91
STL Algorithms in Action
Coming soon to a compiler near you!
Parallel algorithms
• Currently experimental, proposed for a future
release.
• In the std::experimental::parallel
namespace.
• Have the same interfaces as standard
algorithms.
• Controlled by use of parallelism
execution_policy.
• See the Parallelism Technical Specification for
more info.
Michael VanLoon - http://codeache.net - CPPcon 2015 92
STL Algorithms in Action
A quick review…
• Be familiar with all the varied algorithms
provided for free with your compiler
• Write some code that exercises each of
them so you are familiar with their usage
and individual personalities
• Write your own adaptations of existing
algorithms
• Implement your own algorithms
Michael VanLoon - http://codeache.net - CPPcon 2015 93
STL Algorithms in Action
STL algorithms
non-modifying sequence operations
• all_of
• any_of
• none_of
• for_each
• find
– find_if
– find_if_not
• find_end
• find_first_of
• adjacent_find
• count
– count_if
• mismatch
• equal
• is_permutation
• search
– search_n
Michael VanLoon - http://codeache.net - CPPcon 2015 94
STL Algorithms in Action
STL algorithms
mutating sequence operations
• copy
– copy_n
– copy_if
– copy_backward
• move
– move_backward
• swap_ranges
– iter_swap
• transform
• replace
– replace_if
– replace_copy
– replace_copy_if
• fill
– fill_n
• generate
– generate_n
• remove
– remove_if
– remove_copy
– remove_copy_if
• unique
– unique_copy
• reverse
– reverse_copy
• rotate
– rotate_copy
• shuffle
– random_shuffle
• partition
– is_partitioned
– stable_partition
– partition_copy
– partition_point
Michael VanLoon - http://codeache.net - CPPcon 2015 95
STL Algorithms in Action
STL algorithms
sorting and related operations
• sorting
– sort
– stable_sort
– partial_sort
– partial_sort_copy
• nth_element
• binary search
– lower_bound
– upper_bound
– equal_range
– binary_search
• merge
– merge
– inplace_merge
• set operations on sorted
structures
– includes
– set_union
– set_intersection
– set_difference
– set_symmetric_difference
• heap operations
– push_heap
– pop_heap
– make_heap
– sort_heap
• minimum and maximum
– min
– max
– minmax
– min_element
– max_element
– minmax_element
• lexicographical comparisons
– lexicographical_compare
• permutation generators
– next_permutation
– prev_permutation
Michael VanLoon - http://codeache.net - CPPcon 2015 96
STL Algorithms in Action
STL algorithms
general numeric operations
• accumulate
• inner_product
• partial_sum
• adjacent_difference
• iota
Michael VanLoon - http://codeache.net - CPPcon 2015 97
STL Algorithms in Action
Shout out to my “sponsor”
• F5 Networks is a highly technical company
with a belief in well engineered software
• F5 Networks has graciously sent me here,
and they tolerate encourage me working on
this stuff in addition to my “real” work
• If you’re looking for something cool and
challenging to do with C++, check out F5!
• https://f5.com/about-us/careers
Michael VanLoon - http://codeache.net - CPPcon 2015 98
STL Algorithms in Action
Shameless advertising…
• I’m working on a book on this very topic!
• It isn’t done yet…
• If you have some creative or clever uses of
STL algorithms you’d like me consider, please
drop me a line with some example code!
• michaelv@codeache.net
• Visit my blog for more adventures:
http://codeache.net
Michael VanLoon - http://codeache.net - CPPcon 2015 99

More Related Content

What's hot

Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8icarter09
 
Fast, Faster and Super-Fast Queries
Fast, Faster and Super-Fast QueriesFast, Faster and Super-Fast Queries
Fast, Faster and Super-Fast QueriesEdward Willink
 
A DSL for Model Mutation and its Applications to Different Domains
A DSL for Model Mutation and its Applications to Different DomainsA DSL for Model Mutation and its Applications to Different Domains
A DSL for Model Mutation and its Applications to Different DomainsPablo Gómez Abajo
 
Safe navigation in OCL
Safe navigation in OCLSafe navigation in OCL
Safe navigation in OCLEdward Willink
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project LambdaRahman USTA
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentationVan Huong
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in JavaErhan Bagdemir
 
Scilab for real dummies
Scilab for real dummiesScilab for real dummies
Scilab for real dummiesSunu Pradana
 
Embedded OCL Integration and Debugging
Embedded OCL Integration and DebuggingEmbedded OCL Integration and Debugging
Embedded OCL Integration and DebuggingEdward Willink
 
Advanced
AdvancedAdvanced
Advancedmxmxm
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java langer4711
 
Scilab optimization workshop
Scilab optimization workshop Scilab optimization workshop
Scilab optimization workshop Scilab
 

What's hot (20)

Java8 features
Java8 featuresJava8 features
Java8 features
 
Lambda Expressions in Java 8
Lambda Expressions in Java 8Lambda Expressions in Java 8
Lambda Expressions in Java 8
 
Fast, Faster and Super-Fast Queries
Fast, Faster and Super-Fast QueriesFast, Faster and Super-Fast Queries
Fast, Faster and Super-Fast Queries
 
A DSL for Model Mutation and its Applications to Different Domains
A DSL for Model Mutation and its Applications to Different DomainsA DSL for Model Mutation and its Applications to Different Domains
A DSL for Model Mutation and its Applications to Different Domains
 
Safe navigation in OCL
Safe navigation in OCLSafe navigation in OCL
Safe navigation in OCL
 
Aligning OCL and UML
Aligning OCL and UMLAligning OCL and UML
Aligning OCL and UML
 
Java 8 - Project Lambda
Java 8 - Project LambdaJava 8 - Project Lambda
Java 8 - Project Lambda
 
Anr cair meeting feb 2016
Anr cair meeting feb 2016Anr cair meeting feb 2016
Anr cair meeting feb 2016
 
Java 8 presentation
Java 8 presentationJava 8 presentation
Java 8 presentation
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
Java8
Java8Java8
Java8
 
Ikc 2015
Ikc 2015Ikc 2015
Ikc 2015
 
Scilab for real dummies
Scilab for real dummiesScilab for real dummies
Scilab for real dummies
 
Embedded OCL Integration and Debugging
Embedded OCL Integration and DebuggingEmbedded OCL Integration and Debugging
Embedded OCL Integration and Debugging
 
Advanced
AdvancedAdvanced
Advanced
 
Java 8 Feature Preview
Java 8 Feature PreviewJava 8 Feature Preview
Java 8 Feature Preview
 
Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java Programming with Lambda Expressions in Java
Programming with Lambda Expressions in Java
 
Scilab optimization workshop
Scilab optimization workshop Scilab optimization workshop
Scilab optimization workshop
 
Functions
FunctionsFunctions
Functions
 
OCL 2.4. (... 2.5)
OCL 2.4. (... 2.5)OCL 2.4. (... 2.5)
OCL 2.4. (... 2.5)
 

Viewers also liked

9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011Infuz
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановYandex
 
Effective stl notes
Effective stl notesEffective stl notes
Effective stl notesUttam Gandhi
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)Ovidiu Farauanu
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Daniele Pallastrelli
 
Effective c++notes
Effective c++notesEffective c++notes
Effective c++notesUttam Gandhi
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersRichard Thomson
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and deletePlatonov Sergey
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Sameer Rathoud
 
Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Platonov Sergey
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingDustin Chase
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0Patrick Charrier
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementAnil Bapat
 

Viewers also liked (20)

9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
The State of Twitter: STL 2011
The State of Twitter: STL 2011The State of Twitter: STL 2011
The State of Twitter: STL 2011
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий Леванов
 
Effective stl notes
Effective stl notesEffective stl notes
Effective stl notes
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
 
Effective c++notes
Effective c++notesEffective c++notes
Effective c++notes
 
Modern C++
Modern C++Modern C++
Modern C++
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())Memory Management C++ (Peeling operator new() and delete())
Memory Management C++ (Peeling operator new() and delete())
 
Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”Михаил Матросов, “С++ без new и delete”
Михаил Матросов, “С++ без new и delete”
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
C++ Dependency Management 2.0
C++ Dependency Management 2.0C++ Dependency Management 2.0
C++ Dependency Management 2.0
 
Set Theory In C++
Set Theory In C++Set Theory In C++
Set Theory In C++
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
File Pointers
File PointersFile Pointers
File Pointers
 

Similar to STL Algorithms In Action

New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNico Ludwig
 
Creating queuing system simulations with enterprise architect sysml parametri...
Creating queuing system simulations with enterprise architect sysml parametri...Creating queuing system simulations with enterprise architect sysml parametri...
Creating queuing system simulations with enterprise architect sysml parametri...Pragmatic Cohesion Consulting, LLC
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Software Architectures, Week 2 - Decomposition techniques
Software Architectures, Week 2 - Decomposition techniquesSoftware Architectures, Week 2 - Decomposition techniques
Software Architectures, Week 2 - Decomposition techniquesAngelos Kapsimanis
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++Khushal Mehta
 
Fortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASFortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASJongsu "Liam" Kim
 
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8EPAM_Systems_Bulgaria
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Martin Toshev
 
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxDeepakJangid87
 
Simulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaSimulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaObeo
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshopVinay Kumar
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programmingSteve Zhang
 
Aerospace Project Integration at OPAL-RT
Aerospace Project Integration at OPAL-RT Aerospace Project Integration at OPAL-RT
Aerospace Project Integration at OPAL-RT OPAL-RT TECHNOLOGIES
 
From System Modeling to Automated System Testing
From System Modeling to Automated System TestingFrom System Modeling to Automated System Testing
From System Modeling to Automated System TestingFlorian Lier
 
Application of the automation know-how within the EGS-CC project
Application of the automation know-how within the EGS-CC projectApplication of the automation know-how within the EGS-CC project
Application of the automation know-how within the EGS-CC projectNieves Salor
 
Java 8
Java 8Java 8
Java 8vpulec
 
Dances with bits - industrial data analytics made easy!
Dances with bits - industrial data analytics made easy!Dances with bits - industrial data analytics made easy!
Dances with bits - industrial data analytics made easy!Julian Feinauer
 

Similar to STL Algorithms In Action (20)

New c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iiiNew c sharp3_features_(linq)_part_iii
New c sharp3_features_(linq)_part_iii
 
Creating queuing system simulations with enterprise architect sysml parametri...
Creating queuing system simulations with enterprise architect sysml parametri...Creating queuing system simulations with enterprise architect sysml parametri...
Creating queuing system simulations with enterprise architect sysml parametri...
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
When Should I Use Simulation?
When Should I Use Simulation?When Should I Use Simulation?
When Should I Use Simulation?
 
Software Architectures, Week 2 - Decomposition techniques
Software Architectures, Week 2 - Decomposition techniquesSoftware Architectures, Week 2 - Decomposition techniques
Software Architectures, Week 2 - Decomposition techniques
 
stacks and queues class 12 in c++
stacks and  queues class 12 in c++stacks and  queues class 12 in c++
stacks and queues class 12 in c++
 
Fortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLASFortran & Link with Library & Brief Explanation of MKL BLAS
Fortran & Link with Library & Brief Explanation of MKL BLAS
 
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8
Tech Talks_04.07.15_Session 3_Martin Toshev_Concurrency Utilities In Java 8
 
Concurrency Utilities in Java 8
Concurrency Utilities in Java 8Concurrency Utilities in Java 8
Concurrency Utilities in Java 8
 
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptxIntro to LV in 3 Hours for Control and Sim 8_5.pptx
Intro to LV in 3 Hours for Control and Sim 8_5.pptx
 
Simulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in CapellaSimulation with Python and MATLAB® in Capella
Simulation with Python and MATLAB® in Capella
 
Mat lab workshop
Mat lab workshopMat lab workshop
Mat lab workshop
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programming
 
Aerospace Project Integration at OPAL-RT
Aerospace Project Integration at OPAL-RT Aerospace Project Integration at OPAL-RT
Aerospace Project Integration at OPAL-RT
 
Java Micro-Benchmarking
Java Micro-BenchmarkingJava Micro-Benchmarking
Java Micro-Benchmarking
 
From System Modeling to Automated System Testing
From System Modeling to Automated System TestingFrom System Modeling to Automated System Testing
From System Modeling to Automated System Testing
 
Application of the automation know-how within the EGS-CC project
Application of the automation know-how within the EGS-CC projectApplication of the automation know-how within the EGS-CC project
Application of the automation know-how within the EGS-CC project
 
data structure
data structuredata structure
data structure
 
Java 8
Java 8Java 8
Java 8
 
Dances with bits - industrial data analytics made easy!
Dances with bits - industrial data analytics made easy!Dances with bits - industrial data analytics made easy!
Dances with bits - industrial data analytics made easy!
 

Recently uploaded

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

STL Algorithms In Action

  • 1. STL Algorithms in Action STL Algorithms and their everyday application Michael VanLoon CPPcon 2015
  • 2. STL Algorithms in Action What are algorithms? al∙go∙rithm noun: algorithm; plural noun: algorithms A procedure for solving a … problem (as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation; broadly: a step-by- step procedure for solving a problem or accomplishing some end especially by a computer. Merriam-Webster dictionary Michael VanLoon - http://codeache.net - CPPcon 2015 2
  • 3. STL Algorithms in Action What are STL algorithms? • A pre-built library of general-purpose algorithms designed to solve specific problems • Come “for free” with your C++ compiler • Operate on sequences or sequenced containers • Declarative in syntax – no explicit (“raw”) loops • Iterate over some or all members of a sequence performing an operation on each element in turn • Designed by experts and proven bug-free by the millions of lines of other peoples’ programs that already use them! Michael VanLoon - http://codeache.net - CPPcon 2015 3
  • 4. STL Algorithms in Action What is a raw loop anyway? • It’s a for, while, or do… while loop • Explicitly coded • Often contains many lines of code (should it?) • May cause side-effects outside its scope vector<int> out; bool found = false; for (const auto& i: v) { if (i >= 42) { out.emplace_back(i); ++global_count; if (i == 42) { found = true; } } } Michael VanLoon - http://codeache.net - CPPcon 2015 4
  • 5. STL Algorithms in Action Why use algorithms? • Often more efficient than hand-written loop • Cleaner and more clearly abstracted than a raw loop • Contains side-effects inside a clear interface • Prevents accidental leakage of side-effects • Eases reasoning about functionality and reasoning about post conditions • Less likely to fail under non-obvious conditions • Eases reasoning about the surrounding code Michael VanLoon - http://codeache.net - CPPcon 2015 5
  • 6. STL Algorithms in Action Classes of STL algorithms • Non-modifying sequence operations (25.2) • Mutating sequence operations (25.3) • Sorting and related operations (25.4) • General C algorithms (25.5) • General numeric operations (26.7) (section of the C++ standard INCITS/ISO/IEC 14882-2011[2012]) Michael VanLoon - http://codeache.net - CPPcon 2015 6
  • 7. STL Algorithms in Action STL algorithms non-modifying sequence operations • Do not modify the input sequence. • Do not emit a result sequence. • Algorithm will not cause side-effects in input sequence. • Function object, if present, may cause side- effects by modifying itself, the sequence (in certain cases, e.g. for_each), or its environment. Michael VanLoon - http://codeache.net - CPPcon 2015 7
  • 8. STL Algorithms in Action STL algorithms non-modifying sequence operations • all_of • any_of • none_of • for_each • find – find_if – find_if_not • find_end • find_first_of • adjacent_find • count – count_if • mismatch • equal • is_permutation • search – search_n Michael VanLoon - http://codeache.net - CPPcon 2015 8
  • 9. STL Algorithms in Action STL algorithms mutating sequence operations • Do not modify the input sequence, except in situations when output overlaps input, resulting in modification in- place (e.g. transform). • Emit an output sequence of results. • Output sequence may potentially overlap input sequence for certain algorithms (e.g. transform). Others (e.g. copy) explicitly disallow overlap/in-place. • Algorithm will explicitly cause side-effects in output sequence. • Function object, if present, may cause side-effects by modifying itself or its environment. Function object should not modify the input or output sequences. Michael VanLoon - http://codeache.net - CPPcon 2015 9
  • 10. STL Algorithms in Action STL algorithms mutating sequence operations • copy – copy_n – copy_if – copy_backward • move – move_backward • swap_ranges – iter_swap • transform • replace – replace_if – replace_copy – replace_copy_if • fill – fill_n • generate – generate_n • remove – remove_if – remove_copy – remove_copy_if • unique – unique_copy • reverse – reverse_copy • rotate – rotate_copy • shuffle – random_shuffle • partition – is_partitioned – stable_partition – partition_copy – partition_point Michael VanLoon - http://codeache.net - CPPcon 2015 10
  • 11. STL Algorithms in Action STL algorithms sorting and related operations • A mix of non-modifying and mutating operations • Mutating operations modify sequences in place (e.g. sort, make_heap), or emit output to an output sequence (e.g. merge, partial_sort_copy) • Default compare function is operator< • Explicit compare function object, if supplied, must not modify the sequence or iterators Michael VanLoon - http://codeache.net - CPPcon 2015 11
  • 12. STL Algorithms in Action STL algorithms sorting and related operations • sorting – sort – stable_sort – partial_sort – partial_sort_copy • nth_element • binary search – lower_bound – upper_bound – equal_range – binary_search • merge – merge – inplace_merge • set operations on sorted structures – includes – set_union – set_intersection – set_difference – set_symmetric_difference • heap operations – push_heap – pop_heap – make_heap – sort_heap • minimum and maximum – min – max – minmax – min_element – max_element – minmax_element • lexicographical comparisons – lexicographical_compare • permutation generators – next_permutation – prev_permutation Michael VanLoon - http://codeache.net - CPPcon 2015 12
  • 13. STL Algorithms in Action STL algorithms general numeric operations • Library of algorithms for doing numeric operations. • Consist of components for complex number types, random number generation, nu- meric (n-at-a-time) arrays, generalized numeric algorithms, and facilities included from the ISO C library.1 1Description from the standard library that is surprisingly understandable by humans. Michael VanLoon - http://codeache.net - CPPcon 2015 13
  • 14. STL Algorithms in Action STL algorithms general numeric operations • accumulate • inner_product • partial_sum • adjacent_difference • iota Michael VanLoon - http://codeache.net - CPPcon 2015 14
  • 15. STL Algorithms in Action STL algorithms C library algorithms • These are shown for completeness. • You may need to know about these for legacy reasons. • In general, there is nothing these can do that you can’t do better with the modern algorithms previously mentioned. Michael VanLoon - http://codeache.net - CPPcon 2015 15
  • 16. STL Algorithms in Action STL algorithms C library algorithms • bsearch • qsort Michael VanLoon - http://codeache.net - CPPcon 2015 16
  • 17. STL Algorithms in Action for_each and transform • Your go-to generic algorithms for doing general things to sequences • Applies an operation to each element in a sequence, in order • Very similar except completely different Michael VanLoon - http://codeache.net - CPPcon 2015 17
  • 18. STL Algorithms in Action for_each • Applies an operation to each element in a sequence (like many algorithms) • Is a non-modifying sequence operation • Algorithm produces no side-effect • Function object may produce a side-effect by modifying the input sequence • Function object may produce a side-effect by modifying itself • Returns a moved copy of the function object • for_each is considered non-modifying because it produces no output range; it relies on the function object for mutation, if any Michael VanLoon - http://codeache.net - CPPcon 2015 18
  • 19. STL Algorithms in Action transform • Applies an operation to each element in a sequence (like many algorithms) • Is a mutating sequence operation • If the input range(s) and result range are the same, or overlap, mutates objects in-place • Algorithm explicitly produces a side-effect • Function object may not produce a side-effect • transform is considered mutating because it explicitly produces an output range modified by applying the function object to elements, and forbids the function object from modifying any of the range elements or iterators • Returns iterator pointing one past last element in result range Michael VanLoon - http://codeache.net - CPPcon 2015 19
  • 20. STL Algorithms in Action for_each example Generate a single hash for all strings in a vector struct HashString { void operator()(const string& s) { hash = accumulate(s.begin(), s.end(), hash, hash_char); } uint32_t hash = 0; }; Michael VanLoon - http://codeache.net - CPPcon 2015 20
  • 21. STL Algorithms in Action accumulate • Is a non-modifying numerics operation • Algorithm produces no side-effect • Function object may not modify the sequence or the iterator • Function object may produce a side-effect by returning a return code different from input parameter • accumulate differs from for_each in that the algorithm carries a value rather than a function object from visit to visit, applying the operation to each element and the current value • accumulate differs from for_each in that it has a default operation: operator+ Michael VanLoon - http://codeache.net - CPPcon 2015 21
  • 22. STL Algorithms in Action for_each example Generate a single hash for all strings in a vector struct HashString { void operator()(const string& s) { hash = accumulate(s.begin(), s.end(), hash, hash_char); } uint32_t hash = 0; }; Michael VanLoon - http://codeache.net - CPPcon 2015 22
  • 23. STL Algorithms in Action for_each example Generate a single hash for all strings in a vector struct HashString { void operator()(const string& s) { hash = accumulate(s.begin(), s.end(), hash, hash_char); } uint32_t hash = 0; }; template<typename Cont> uint32_t hash_all_strings(const Cont& v) { const auto hasher = for_each(v.begin(), v.end(), HashString()); return hasher.hash; } Michael VanLoon - http://codeache.net - CPPcon 2015 23
  • 24. STL Algorithms in Action for_each example Generate a single hash for all strings in a vector struct HashString { void operator()(const string& s) { hash = accumulate(s.begin(), s.end(), hash, hash_char); } uint32_t hash = 0; }; template<typename Cont> uint32_t hash_all_strings(const Cont& v) { const auto hasher = for_each(v.begin(), v.end(), HashString()); return hasher.hash; } void test_for_each_hash() { vector<string> v{ "one", "two", "three", "four", "five" }; uint32_t hash = hash_all_strings(v); cout << "Hash: " << hash << dec << endl; } Michael VanLoon - http://codeache.net - CPPcon 2015 24
  • 25. STL Algorithms in Action for_each example (cont) … the rest of the code… uint32_t rotl(uint32_t value, unsigned int count) { const uint32_t mask = (CHAR_BIT * sizeof(value) - 1); count &= mask; return (value << count) | (value >> ((-count)&mask)); } uint32_t hash_char(uint32_t hash, char c) { hash = rotl(hash, c); // circular rotate left hash ^= c; return hash; } Michael VanLoon - http://codeache.net - CPPcon 2015 25
  • 26. STL Algorithms in Action transform example Generate hash for each string in a vector uint32_t hash_string(const string& s) { return accumulate(s.begin(), s.end(), 0, hash_char); }; Michael VanLoon - http://codeache.net - CPPcon 2015 26
  • 27. STL Algorithms in Action transform example Generate hash for each string in a vector uint32_t hash_string(const string& s) { return accumulate(s.begin(), s.end(), 0, hash_char); }; template<typename Cont> vector<uint32_t> hash_each_string(const Cont& v) { vector<uint32_t> res; transform(v.begin(), v.end(), back_inserter(res), hash_string); return res; } Michael VanLoon - http://codeache.net - CPPcon 2015 27
  • 28. STL Algorithms in Action transform example Generate hash for each string in a vector uint32_t hash_string(const string& s) { return accumulate(s.begin(), s.end(), 0, hash_char); }; template<typename Cont> vector<uint32_t> hash_each_string(const Cont& v) { vector<uint32_t> res; transform(v.begin(), v.end(), back_inserter(res), hash_string); return res; } void test_transform_hash() { vector<string> v{ "one", "two", "three", "four", "five" }; auto res = hash_each_string(v); cout << "Hashes: "; for_each(res.begin(), res.end(), [](uint32_t rh){ cout << rh << " "; }); cout << endl; } Michael VanLoon - http://codeache.net - CPPcon 2015 28
  • 29. STL Algorithms in Action any_of, all_of, and none_of • Apply a function object to a sequence • Determines whether any, all, or none of the elements in the sequence are true as determined by the function object • May return before evaluating all elements in sequence if outcome is determined early Michael VanLoon - http://codeache.net - CPPcon 2015 29
  • 30. STL Algorithms in Action all_of example validate http headers static const regex reHeader("([A-Za-z0-9!#$%&'*+.^_`|~-]+): *(.+) *"); inline bool headers_valid(const vector<string>& headers) { return all_of(headers.begin(), headers.end(), [](const auto& header) -> bool { smatch matches; return regex_match(header, matches, reHeader); } ); } Michael VanLoon - http://codeache.net - CPPcon 2015 30
  • 31. STL Algorithms in Action all_of example validate http headers test and output void all_of_headers() { vector<string> h1 = { "Foo: bar", "Content-type: application/json", "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << headers_valid(h1) << endl; vector<string> h2 = { "Foo : bar", "Content-type: application/json", "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << headers_valid(h2) << endl; vector<string> h3 = { "Foo: bar", "Content-type: application/json", ":Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << headers_valid(h3) << endl; vector<string> h4 = { "Foo: bar", " Content-type: application/json" "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << headers_valid(h4) << endl; } output: headers valid: true headers valid: false headers valid: false headers valid: false Michael VanLoon - http://codeache.net - CPPcon 2015 31
  • 32. STL Algorithms in Action any_of example http header search inline bool header_search(const vector<string>& headers, const string& find_header, const string& find_value) { return any_of(headers.begin(), headers.end(), [&find_header, &find_value](const auto& header) -> bool { const regex reHeader( "(" + find_header + "): *(" + find_value + ") *", regex::icase); smatch matches; return regex_match(header, matches, reHeader); } ); } Michael VanLoon - http://codeache.net - CPPcon 2015 32
  • 33. STL Algorithms in Action any_of example http header search test and output void any_of_headers_simple() { vector<string> h1 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: toestrength", "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << header_search(h1, "X-SuperPower", "toestrength") << endl; vector<string> h2 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: supersmell", "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << header_search(h2, "X-SuperPower", "toestrength") << endl; vector<string> h3 = { "Foo : bar", "Content-type: application/json", "X-SuperPower: toestrength", "Accept: text/html,text/json,application/json" }; cout << "headers valid: " << boolalpha << header_search(h3, "X-Superpower", "toeStrength") << endl; } output: headers valid: true headers valid: false headers valid: true Michael VanLoon - http://codeache.net - CPPcon 2015 33
  • 34. STL Algorithms in Action another for_each example simultaneously validate and search http headers struct HeaderData { int good_headers = 0; int bad_headers = 0; multimap<string, string> found_headers; string find_header; string find_value; operator bool() const { return !bad_headers && good_headers > 0; } void operator() (const string& header) { static const regex reValid("([A-Za-z0-9!#$%&'*+.^_`|~-]+): *(.+) *"); smatch matches; bool match = regex_match(header, matches, reValid); if (match) { ++good_headers; const regex reHeader("(" + find_header + "): *(" + find_value + ") *", regex::icase); if (regex_match(header, matches, reHeader)) { found_headers.emplace(matches[1], matches[2]); } } else { ++bad_headers; } } }; Michael VanLoon - http://codeache.net - CPPcon 2015 34
  • 35. STL Algorithms in Action another for_each example simultaneously validate and search http headers struct HeaderData { int good_headers = 0; int bad_headers = 0; multimap<string, string> found_headers; string find_header; string find_value; operator bool() const; void operator() (const string& header); }; const HeaderData header_parse(const vector<string>& headers, const string& find_header, const string& find_value) { HeaderData hd; hd.find_header = find_header; hd.find_value = find_value; return for_each(headers.begin(), headers.end(), hd); } Michael VanLoon - http://codeache.net - CPPcon 2015 35
  • 36. STL Algorithms in Action another for_each example simultaneous validate/search test void any_of_headers_full() { { vector<string> h1 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: toestrength", "Accept: text/html,text/json,application/json" }; const HeaderData& hd = header_parse(h1, "X-SuperPower", "toestrength"); cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers; for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) { cout << "nt'" << val.first << "', '" << val.second << "'"; }); cout << endl; } { vector<string> h2 = { "Foo: bar", "Content-type: application/json", "X-SuperPower: supersmell", "Accept: text/ht ml,text/json,application/json" }; const HeaderData& hd = header_parse(h2, "X-SuperPower", "toestrength"); cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers; for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) { cout << "nt'" << val.first << "', '" << val.second << "'"; }); cout << endl; } { vector<string> h3 = { "Foo : bar", "Content-type: application/json", "X-Superpower: toestrength", "Accept: text/ html,text/json,application/json" }; const HeaderData& hd = header_parse(h3, "X-SuperPower", "toestrength"); cout << "headers parse: " << hd << ", good " << hd.good_headers << ", bad " << hd.bad_headers; for_each(hd.found_headers.begin(), hd.found_headers.end(), [](const auto& val) { cout << "nt'" << val.first << "', '" << val.second << "'"; }); cout << endl; } } Michael VanLoon - http://codeache.net - CPPcon 2015 36
  • 37. STL Algorithms in Action another for_each example simultaneous validate/search output output: headers parse: true, good 4, bad 0 'X-SuperPower', 'toestrength' headers parse: true, good 4, bad 0 headers parse: false, good 3, bad 1 'X-Superpower', 'toestrength' Michael VanLoon - http://codeache.net - CPPcon 2015 37
  • 38. STL Algorithms in Action adjacent_find • adjacent_find searches for adjacent items (pairs of elements next to each other) in a sequence that meet a certain condition. • Returns an iterator to the first of the pair of elements meeting the condition. • The default condition is equality (i.e. find two adjacent items that are equal). • A custom comparator may be provided to look for other adjacent conditions. Michael VanLoon - http://codeache.net - CPPcon 2015 38
  • 39. STL Algorithms in Action adjacent_find example simple is_sorted implementation vecInt_t v{ 1, 2, 3, 4, 5, 5, 6, 7, 8 }; // Greater works because it's asking if the first value is // greater than the second value. If so, then the test // fails (not sorted). If the first value is less than or // equal to the second value, no match and success. vecInt_t::iterator it = adjacent_find(v.begin(), v.end(), greater<int>()); if (it == v.end()) cout << "Vector is sorted" << endl; else cout << "Vector not sorted, value " << *(it + 1) << ", at position " << it - v.begin() + 1 << endl; output: Vector is sorted Vector not sorted, value 3, at position 9 Michael VanLoon - http://codeache.net - CPPcon 2015 39
  • 40. STL Algorithms in Action adjacent_find example test for sequence deviation template<typename Cont> typename Cont::const_iterator checkDeviation(const Cont& cont, double allowed_dev) { return adjacent_find(cont.begin(), cont.end(), [allowed_dev](const typename Cont::value_type& v1, const typename Cont::value_type& v2) { auto limit = v1 * allowed_dev; return (v2 > v1 + limit) || (v2 < v1 - limit); }); } Michael VanLoon - http://codeache.net - CPPcon 2015 40
  • 41. STL Algorithms in Action adjacent_find example test for sequence deviation test and output vecDbl_t v{ 1.0, 1.05, 1.06, 1.04, 1.09, 1.15, 1.2 }; vecDbl_t::const_iterator it = checkDeviation(v, 0.1); if (it == v.end()) cout << "Vector is within deviation limits" << endl; else cout << "Vector outside deviation limits, values " << *it << " and ” << *(it + 1) << ", at position " << it - v.begin() + 1 << endl; v.push_back(2.0); it = checkDeviation(v, 0.1); if (it == v.end()) cout << "Vector is within deviation limits" << endl; else cout << "Vector outside deviation limits, values " << *it << " and ” << *(it + 1) << ", at position " << it - v.begin() + 1 << endl; output: Vector is within deviation limits Vector outside deviation limits, values 1.2 and 2, at position 7 Michael VanLoon - http://codeache.net - CPPcon 2015 41
  • 42. STL Algorithms in Action remove_if (with erase) Scott Meyers, “Effective STL,” items 9 and 32 • Scenario: you want to erase several items from a container that meet a condition • You could write a loop with some checks, some explicit erases, and potential iterator invalidation • Or… Michael VanLoon - http://codeache.net - CPPcon 2015 42
  • 43. STL Algorithms in Action remove_if (with erase) Scott Meyers, “Effective STL,” items 9 and 32 struct Foo { bool expired; … other members… }; vector<Foo> v; v.erase( remove_if(v.begin(), v.end(), [](const Foo& foo){ return foo.expired; }), v.end()); Output: before: [val: 1, expired: false] [val: 2, expired: true] [val: 3, expired: false] [val: 4, expired: false] [val: 5, expired: true] after: [val: 1, expired: false] [val: 3, expired: false] [val: 4, expired: false] Michael VanLoon - http://codeache.net - CPPcon 2015 43
  • 44. STL Algorithms in Action Know your sorts and sorta-sorts Scott Meyers’ “Effective STL” Item 31 • Sorting algorithms: – sort1 – stable_sort1 – partial_sort, partial_sort_copy1 • Sorta-sorts: – nth_element1 – partition, partition_copy2 – stable_partition2 1 Requires random access iterators 2 Requires bidirectional iterators Michael VanLoon - http://codeache.net - CPPcon 2015 44
  • 45. STL Algorithms in Action Know your sorts Scott Meyers’ “Effective STL” Item 31 • sort – Most general-purpose sort – Order of equivalent items implementation-defined – In some cases, may be more efficient than stable_sort since equivalent items can be rearranged at sort’s discretion – Sorts in place • stable_sort – Order of equivalent items preserved – Sorts in place • partial_sort – Sort a subset of a sequence, drawing from a subset that is equal to or larger than the sorted sequence – There is no stable version of partial_sort • partial_sort_copy – Like partial_sort, but… – Sorts specified subset of an input sequence, emitting to an output sequence Michael VanLoon - http://codeache.net - CPPcon 2015 45
  • 46. STL Algorithms in Action sort and stable_sort sort objects by last name, first, middle • Scenario: assume an object with strings containing fist name, middle name, and last name, among other things struct Person { string first; string middle; string last; … other Person stuff… }; • We want to sort said objects by all three fields, with precedence last > first > middle Michael VanLoon - http://codeache.net - CPPcon 2015 46
  • 47. STL Algorithms in Action sort and stable_sort sort objects by last name, first, middle vector<Person> v{{ { "Joe", "P", "Smith" }, { "Jane", "Q", "Jones" }, { "Frank", "P", "Johnson" }, { "Sarah", "B", "Smith" }, { "Joe", "X", "Jones" }, { "Joe", "A", "Smith" } }}; // Sort by least influential data first sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.middle < b.middle; }); Michael VanLoon - http://codeache.net - CPPcon 2015 47
  • 48. STL Algorithms in Action sort and stable_sort sort objects by last name, first, middle vector<Person> v{{ { "Joe", "P", "Smith" }, { "Jane", "Q", "Jones" }, { "Frank", "P", "Johnson" }, { "Sarah", "B", "Smith" }, { "Joe", "X", "Jones" }, { "Joe", "A", "Smith" } }}; // Sort by least influential data first sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.middle < b.middle; }); stable_sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.first < b.first; }); Michael VanLoon - http://codeache.net - CPPcon 2015 48
  • 49. STL Algorithms in Action sort and stable_sort sort objects by last name, first, middle vector<Person> v{{ { "Joe", "P", "Smith" }, { "Jane", "Q", "Jones" }, { "Frank", "P", "Johnson" }, { "Sarah", "B", "Smith" }, { "Joe", "X", "Jones" }, { "Joe", "A", "Smith" } }}; // Sort by least influential data first sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.middle < b.middle; }); stable_sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.first < b.first; }); stable_sort(v.begin(), v.end(), [](const Person& a, const Person& b){ return a.last < b.last; }); // Sort by most influential data last Michael VanLoon - http://codeache.net - CPPcon 2015 49
  • 50. STL Algorithms in Action sort and stable_sort visual: sort objects by last name, first, middle Michael VanLoon - http://codeache.net - CPPcon 2015 50
  • 51. STL Algorithms in Action partial_sort • Takes an input sequence of sort candidates • Sorts the top n elements into a potentially smaller output sequence • Order of items from input sequence that are unsorted in output sequence are in implementation defined order • partial_sort is an in-place operation • partial_sort_copy copies sorted output to a separate sequence • partial_sort is obviously more efficient than a full sort Michael VanLoon - http://codeache.net - CPPcon 2015 51
  • 52. STL Algorithms in Action partial_sort example vector<int> v1{ 42, 17, 89, 22, 34, 78, 63, 12, 57, 99 }; partial_sort(v1.begin(), v1.begin() + 5, v1.begin() + 8, greater<int>()); Michael VanLoon - http://codeache.net - CPPcon 2015 52
  • 53. STL Algorithms in Action partial_sort example vector<int> v1{ 42, 17, 89, 22, 34, 78, 63, 12, 57, 99 }; partial_sort(v1.begin(), v1.begin() + 5, v1.begin() + 8, greater<int>()); Michael VanLoon - http://codeache.net - CPPcon 2015 53
  • 54. STL Algorithms in Action partial_sort example vector<int> v1{ 42, 17, 89, 22, 34, 78, 63, 12, 57, 99 }; partial_sort(v1.begin(), v1.begin() + 5, v1.begin() + 8, greater<int>()); Michael VanLoon - http://codeache.net - CPPcon 2015 54
  • 55. STL Algorithms in Action partial_sort example visual 42 17 89 22 34 78 63 12 57 99 89 78 63 42 34 17 22 12 57 99 first last first middle sort input range sort output range Michael VanLoon - http://codeache.net - CPPcon 2015 55
  • 56. STL Algorithms in Action Know your sorta-sorts Scott Meyers’ “Effective STL” Item 31 • partition – Reorder sequence so all items before partition point are less, and all items after are not less – Order of items in lower and upper subsets is implementation defined – Order of equivalent items is implementation-defined – Operates in place • stable_partition – Like partition, but… – Order of equivalent items is preserved – Operates in place • partition_copy – Like partition, but… – Order of equivalent items is implementation-defined – Copies items from input sequence to one of two output sequences depending on whether supplied function object returns false or true for each item – There is no stable_partition_copy Michael VanLoon - http://codeache.net - CPPcon 2015 56
  • 57. STL Algorithms in Action Know your sorta-sorts, cont. Scott Meyers’ “Effective STL” Item 31 • nth_element – Reorders sequence such that all items before “nth” are less, and all items after “nth” are not less – Order of items in lower and upper subsets is implementation defined – Order of equivalent items is implementation- defined – “nth” element is exactly the value that would exist in a fully sorted sequence (but without fully sorting) – Operates in place Michael VanLoon - http://codeache.net - CPPcon 2015 57
  • 58. STL Algorithms in Action Know your sorta-sorts, cont. Comparison between partition and nth_element partition • partitions a sequence, based on condition • partition point is not guaranteed to be value that would be at that position in fully sorted sequence • input: – sequence begin, end – comparison function • output: – reordered sequence – iterator to partition point nth_element • partitions a sequence, based on position • nth element is element that would exist in that position in fully sorted sequence • input: – sequence begin, end – iterator to “nth” position – optional comparison function • output: – reordered sequence, partitioned around nth element Michael VanLoon - http://codeache.net - CPPcon 2015 58
  • 59. STL Algorithms in Action partition example partition elements around 50 vector<int> v{ 12, 89, 31, 18, 7, 72, 69, 50, 49, 50, 51, 49 }; vector<int>::iterator part_it = partition(v.begin(), v.end(), [](const int i) { return i < 50; }); Michael VanLoon - http://codeache.net - CPPcon 2015 59
  • 60. STL Algorithms in Action partition example partition elements around 50 vector<int> v{ 12, 89, 31, 18, 7, 72, 69, 50, 49, 50, 51, 49 }; vector<int>::iterator part_it = partition(v.begin(), v.end(), [](const int i) { return i < 50; }); Michael VanLoon - http://codeache.net - CPPcon 2015 60
  • 61. STL Algorithms in Action partition example visual: partition elements around 50 Michael VanLoon - http://codeache.net - CPPcon 2015 61 12 89 31 18 7 72 69 50 12 49 31 18 7 49 69 50 partition point 49 50 51 49 72 50 51 89
  • 62. STL Algorithms in Action partition_copy example partition elements around 50 vector<int> v{ 12, 89, 31, 18, 7, 72, 69, 50, 49, 50, 51, 49 }; partition_copy(v.begin(), v.end(), back_inserter(smaller), back_inserter(larger), [](const int i) { return i < 50; }); Michael VanLoon - http://codeache.net - CPPcon 2015 62
  • 63. STL Algorithms in Action partition_copy example visual: partition elements around 50 Michael VanLoon - http://codeache.net - CPPcon 2015 63
  • 64. STL Algorithms in Action Mixing up partitions! partition_copy, partition, and stable_partition example • Extend our prior Person struct to be an Employee struct • Partition Management and Individuals into two separate containers • Partition Management between Executives and Managers • Partition Architects before other Individuals • Stable partition the Senior and Junior employees among already partitioned individuals, leaving already partitioned elements in same relative position Michael VanLoon - http://codeache.net - CPPcon 2015 64
  • 65. STL Algorithms in Action Mixing up partitions! some data… struct Employee: public Person { enum class EmployeeType { Executive, Manager, Architect, Senior, Junior, Management = Manager, Individual = Architect }; EmployeeType type; … additional members and methods… }; vector<Employee> v{ { "Joe", "P", "Smith", Employee::EmployeeType::Manager }, { "Jane", "Q", "Jones", Employee::EmployeeType::Junior }, { "Frank", "P", "Johnson", Employee::EmployeeType::Architect }, { "Sarah", "B", "Smith", Employee::EmployeeType::Executive }, { "Joe", "X", "Jones", Employee::EmployeeType::Senior }, { "Joe", "A", "Smith", Employee::EmployeeType::Junior }, { "Chris", "M", "Williams", Employee::EmployeeType::Manager } }; Michael VanLoon - http://codeache.net - CPPcon 2015 65
  • 66. STL Algorithms in Action Mixing up partitions! separate employees into management and individuals vector<Employee> management, individuals; partition_copy(v.begin(), v.end(), back_inserter(management), back_inserter(individuals), [](const Employee& e) { return e.type <= Employee::EmployeeType::Manager; }); Michael VanLoon - http://codeache.net - CPPcon 2015 66 vector<Employee>::iterator car_it = partition(management.begin(), management.end(), [](const Employee& e) { return e.type == Employee::EmployeeType::Executive; }); vector<Employee>::iterator jet_it = management.begin(); cout << "Management partitioned:n"; for (auto it = management.begin(); it != management.end(); ++it) { cout << (it < car_it ? "tjet" : "tcar") << *it; } executives get the company jet; managers get the company car
  • 67. STL Algorithms in Action Mixing up partitions! architects get the company segway; everyone else gets the bike vector<Employee>::iterator bike_it = partition(individuals.begin(), individuals.end(), [](const Employee& e) { return e.type == Employee::EmployeeType::Architect; }); vector<Employee>::iterator segway_it = individuals.begin(); cout << "Architects partitioned:n"; for (auto it = individuals.begin(); it != individuals.end(); ++it) { cout << (it < bike_it ? "tsegway" : "tbike") << *it; } Michael VanLoon - http://codeache.net - CPPcon 2015 67 vector<Employee>::iterator old_bike_it = stable_partition(individuals.begin(), individuals.end(), [](const Employee& e) { return e.type <= Employee::EmployeeType::Senior; }); cout << "Individuals partitioned:n"; for (auto it = individuals.begin(); it != individuals.end(); ++it) { cout << (it < bike_it ? "tsegway" : (it < old_bike_it ? "tnewbike" : "toldbike")) << *it; } partition the non-architects from higher to lower seniority
  • 68. STL Algorithms in Action Mixing up partitions! example output Management partitioned: jet Sarah B Smith: Executive car Joe P Smith: Manager car Chris M Williams: Manager Architects partitioned (junior/senior unpartitioned): segway Frank P Johnson: Architect bike Jane Q Jones: Junior bike Joe X Jones: Senior bike Joe A Smith: Junior Individuals fully partitioned: segway Frank P Johnson: Architect newbike Joe X Jones: Senior oldbike Jane Q Jones: Junior oldbike Joe A Smith: Junior Michael VanLoon - http://codeache.net - CPPcon 2015 68
  • 69. STL Algorithms in Action nth_element (not a super-hero, but still pretty super) find median value in sequence vector<int> v{ 12, 2, 89, 78, 18, 7, 72, 69, 81, 50, 49, 50, 51, 49 }; const size_t nth = v.size() / 2; nth_element(v.begin(), v.begin() + nth, v.end()); output: 49 2 12 49 18 7 50 >50< 51 78 72 69 81 89 Michael VanLoon - http://codeache.net - CPPcon 2015 69 find percentile value in sequence vector<int> v{ 12, 2, 89, 78, 18, 7, 72, 69, 81, 50, 49, 50, 51, 49 }; const size_t percentile = 75; const size_t nth = v.size() * percentile / 100; nth_element(v.begin(), v.begin() + nth, v.end()); output: 49 2 12 49 18 7 50 50 51 69 >72< 78 81 89
  • 70. STL Algorithms in Action partial_sort vs. nth_element + sort • Scenario: find top (say 20) web pages by number of hits, among a much larger set • We have a large sequence where we are only concerned with the top 20 elements • We want the top 20 elements of this sequence in order; we don’t care about the rest • Requirement: top 20 must be partitioned to front of sequence, before the don’t-cares • Requirement: top 20 must be sorted Michael VanLoon - http://codeache.net - CPPcon 2015 70
  • 71. STL Algorithms in Action partial_sort vs. nth_element + sort partial_sort(vec.begin(), vec.begin() + 20, vec.end(), greater<int64_t>()); nth_element(vec.begin(), vec.begin() + 20, vec.end(), greater<int64_t>()); sort(vec.begin(), vec.begin() + 20, greater<int64_t>()); Michael VanLoon - http://codeache.net - CPPcon 2015 71
  • 72. STL Algorithms in Action partial_sort vs. nth_element + sort • Are they equivalent? vector<int64_t> v1 = init_vec(); vector<int64_t> v2 = v1; // make a copy partial_sort(v1.begin(), v1.begin() + 20, v1.end(), greater<int64_t>()); nth_element(v2.begin(), v2.begin() + 20, v2.end(), greater<int64_t>()); sort(v2.begin(), v2.begin() + 20, greater<int64_t>()); cout << "sorted portions of vectors are equal: " << boolalpha << equal(v1.begin(), v1.begin() + 20, v2.begin()) << endl; cout << "unsorted portions of vectors are equal: " << boolalpha << equal(v1.begin() + 20, v1.end(), v2.begin() + 20) << endl; output: sorted portions of vectors are equal: true unsorted portions of vectors are equal: false Michael VanLoon - http://codeache.net - CPPcon 2015 72
  • 73. STL Algorithms in Action rotate • Moves elements specified in a source range to destination position, while moving displaced items to source position • Simple way to move one or more elements from one position in a container to another without using erase and insert • Technically, this is a left rotate • rotate moves element in the sequence in-place • rotate_copy does the same, except that it copies the elements to a separate sequence • Requires: – [first, middle) and [middle, last) are valid ranges – i.e. first <= middle <= last Michael VanLoon - http://codeache.net - CPPcon 2015 73
  • 74. STL Algorithms in Action rotate A naïve implementation – move one item in sequence vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int>::iterator it = v.begin() + 2; int i = *it; v.erase(it); vector<int>::iterator it2 = v.begin() + 6 - 1; v.insert(it2, i); Output: before: 0 1 2 3 4 5 6 7 8 9 After: 0 1 3 4 5 2 6 7 8 9 Michael VanLoon - http://codeache.net - CPPcon 2015 74
  • 75. STL Algorithms in Action rotate A naïve implementation – move one item in sequence vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int>::iterator it = v.begin() + 2; int i = *it; v.erase(it); vector<int>::iterator it2 = v.begin() + 6 - 1; v.insert(it2, i); Output: before: 0 1 2 3 4 5 6 7 8 9 After: 0 1 3 4 5 2 6 7 8 9 Michael VanLoon - http://codeache.net - CPPcon 2015 75
  • 76. STL Algorithms in Action rotate A better implementation – move one item in sequence vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int>::iterator it = v.begin() + 2; vector<int>::iterator it2 = v.begin() + 6; rotate(it, it + 1, it2); Output: before: 0 1 2 3 4 5 6 7 8 9 After: 0 1 3 4 5 2 6 7 8 9 Michael VanLoon - http://codeache.net - CPPcon 2015 76
  • 77. STL Algorithms in Action rotate Rotate a range of items vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector<int>::iterator it = v.begin() + 2; vector<int>::iterator it2 = v.begin() + 7; rotate(it, it + 3, it2); Output: before: 0 1 2 3 4 5 6 7 8 9 after: 0 1 5 6 2 3 4 7 8 9 Michael VanLoon - http://codeache.net - CPPcon 2015 77
  • 78. STL Algorithms in Action rotate GCC’s definition • Rotates the elements of the range [first, last) by (middle - first) positions so that the element at middle is moved to first, the element at middle + 1 is moved to first + 1 and so on for each element in the range. • This effectively swaps the ranges [first, middle) and [middle, last). Michael VanLoon - http://codeache.net - CPPcon 2015 78
  • 79. STL Algorithms in Action slide (move range of elements to new position) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would move a range to a point in either direction without worrying about iterator ordering? • For example, move a range of selected GUI items to a new position in a list. • When using rotate, it is important to get the order of iterators correct: first <= middle <= last. • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 79
  • 80. STL Algorithms in Action slide (move range of elements to new position) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would move a range to a point in either direction without worrying about iterator ordering? • For example, move a range of selected GUI items to a new position in a list. • When using rotate, it is important to get the order of iterators correct: first <= middle <= last. • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 80
  • 81. STL Algorithms in Action slide (move range of elements to new position) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would move a range to a point in either direction without worrying about iterator ordering? • For example, move a range of selected GUI items to a new position in a list. • When using rotate, it is important to get the order of iterators correct: first <= middle <= last. • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 81
  • 82. STL Algorithms in Action slide (move range of elements to new position) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would move a range to a point in either direction without worrying about iterator ordering? • For example, move a range of selected GUI items to a new position in a list. • When using rotate, it is important to get the order of iterators correct: first <= middle <= last. • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 82
  • 83. STL Algorithms in Action slide (move range of elements to new position) Sean Parent, “C++ Seasoning” Michael VanLoon - http://codeache.net - CPPcon 2015 83 template <typename I> // I models RandomAccessIterator auto slide(I f, I l, I p) -> pair<I, I> { if (p < f) return { p, rotate(p, f, l) }; if (l < p) return { rotate(f, l, p), p }; return { f, l }; }
  • 84. STL Algorithms in Action gather (gather multiple elements around a point) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would allow a scattered selection of elements to be gathered to a specific location? • For example, move a range of scattered multi-selected GUI items to a new position in a list, gathered together. • How can you gather diverse elements to a single location without loops and special-casing code? • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 84
  • 85. STL Algorithms in Action gather (gather multiple elements around a point) Sean Parent, “C++ Seasoning” • What if you wanted to implement an algorithm that would allow a scattered selection of elements to be gathered to a specific location? • For example, move a range of scattered multi-selected GUI items to a new position in a list, gathered together. • How can you gather diverse elements to a single location without loops and special-casing code? • Bonus: return a pair indicating destination range of relocated elements. Michael VanLoon - http://codeache.net - CPPcon 2015 85
  • 86. STL Algorithms in Action gather (gather multiple elements around a point) Sean Parent, “C++ Seasoning” • Break the problem space into two pieces: – The sequence before the destination position – The sequence after the destination position • What algorithm can gather the select items while maintaining their relative position? Michael VanLoon - http://codeache.net - CPPcon 2015 86
  • 87. STL Algorithms in Action gather (gather multiple elements around a point) Sean Parent, “C++ Seasoning” • Break the problem space into two pieces: – The sequence before the destination position – The sequence after the destination position • That looks a lot like partitioning… Michael VanLoon - http://codeache.net - CPPcon 2015 87
  • 88. STL Algorithms in Action gather (gather multiple elements around a point) Sean Parent, “C++ Seasoning” Michael VanLoon - http://codeache.net - CPPcon 2015 88 template <typename I, // models BidirectionalIterator typename S> // models UnaryPredicate auto gather(I f, I l, I p, S s) -> pair<I, I> { return { stable_partition(f, p, not1(s)), stable_partition(p, l, s) }; }
  • 89. STL Algorithms in Action set_difference process configuration updates • Scenario: you store some configuration information in an ordered container • You receive updated configuration on a regular basis • You want to quickly determine what has changed: i.e. what new items have been added, and what items have been removed Michael VanLoon - http://codeache.net - CPPcon 2015 89
  • 90. STL Algorithms in Action set_difference process configuration updates vector<string> new_items, removed_items; set_difference(current.begin(), current.end(), update.begin(), update.end(), back_inserter(removed_items)); remove_config(removed_items); set_difference(update.begin(), update.end(), current.begin(), current.end(), back_inserter(new_items)); add_config(new_items); Michael VanLoon - http://codeache.net - CPPcon 2015 90
  • 91. STL Algorithms in Action set_difference process configuration updates test example set<string> current { "one", "two", "three", "four", "five" }; set<string> update { "one", "three", "four", "six", "seven" }; vector<string> new_items, removed_items; set_difference(current.begin(), current.end(), update.begin(), update.end(), back_inserter(removed_items)); remove_config(removed_items); set_difference(update.begin(), update.end(), current.begin(), current.end(), back_inserter(new_items)); add_config(new_items); output: Removed: five two Added: seven six Michael VanLoon - http://codeache.net - CPPcon 2015 91
  • 92. STL Algorithms in Action Coming soon to a compiler near you! Parallel algorithms • Currently experimental, proposed for a future release. • In the std::experimental::parallel namespace. • Have the same interfaces as standard algorithms. • Controlled by use of parallelism execution_policy. • See the Parallelism Technical Specification for more info. Michael VanLoon - http://codeache.net - CPPcon 2015 92
  • 93. STL Algorithms in Action A quick review… • Be familiar with all the varied algorithms provided for free with your compiler • Write some code that exercises each of them so you are familiar with their usage and individual personalities • Write your own adaptations of existing algorithms • Implement your own algorithms Michael VanLoon - http://codeache.net - CPPcon 2015 93
  • 94. STL Algorithms in Action STL algorithms non-modifying sequence operations • all_of • any_of • none_of • for_each • find – find_if – find_if_not • find_end • find_first_of • adjacent_find • count – count_if • mismatch • equal • is_permutation • search – search_n Michael VanLoon - http://codeache.net - CPPcon 2015 94
  • 95. STL Algorithms in Action STL algorithms mutating sequence operations • copy – copy_n – copy_if – copy_backward • move – move_backward • swap_ranges – iter_swap • transform • replace – replace_if – replace_copy – replace_copy_if • fill – fill_n • generate – generate_n • remove – remove_if – remove_copy – remove_copy_if • unique – unique_copy • reverse – reverse_copy • rotate – rotate_copy • shuffle – random_shuffle • partition – is_partitioned – stable_partition – partition_copy – partition_point Michael VanLoon - http://codeache.net - CPPcon 2015 95
  • 96. STL Algorithms in Action STL algorithms sorting and related operations • sorting – sort – stable_sort – partial_sort – partial_sort_copy • nth_element • binary search – lower_bound – upper_bound – equal_range – binary_search • merge – merge – inplace_merge • set operations on sorted structures – includes – set_union – set_intersection – set_difference – set_symmetric_difference • heap operations – push_heap – pop_heap – make_heap – sort_heap • minimum and maximum – min – max – minmax – min_element – max_element – minmax_element • lexicographical comparisons – lexicographical_compare • permutation generators – next_permutation – prev_permutation Michael VanLoon - http://codeache.net - CPPcon 2015 96
  • 97. STL Algorithms in Action STL algorithms general numeric operations • accumulate • inner_product • partial_sum • adjacent_difference • iota Michael VanLoon - http://codeache.net - CPPcon 2015 97
  • 98. STL Algorithms in Action Shout out to my “sponsor” • F5 Networks is a highly technical company with a belief in well engineered software • F5 Networks has graciously sent me here, and they tolerate encourage me working on this stuff in addition to my “real” work • If you’re looking for something cool and challenging to do with C++, check out F5! • https://f5.com/about-us/careers Michael VanLoon - http://codeache.net - CPPcon 2015 98
  • 99. STL Algorithms in Action Shameless advertising… • I’m working on a book on this very topic! • It isn’t done yet… • If you have some creative or clever uses of STL algorithms you’d like me consider, please drop me a line with some example code! • michaelv@codeache.net • Visit my blog for more adventures: http://codeache.net Michael VanLoon - http://codeache.net - CPPcon 2015 99