find algorithm: auto result = find(vec.cbegin(), vec.cend(), val);
It returns an iterator to the first element that is equal to that value.
use the library begin and end functions to pass a pointer to the first and one past the last elements in ia.
equal(roster1.cbegin(), roster1.cend(), roster2.cbegin());
fill(vec.begin(), vec.end(), value);
fill(vec.begin(), vec.begin() + vec.size()/2, value);
fill_n(dest, n, val)
Algorithms that write to a destination iterator assume the destination is large enough to hold the number of elements being written.
back_inserter takes a reference to a container and returns an insert iterator bound to that container. When we assign through that iterator, the assignment calls push_back to add an element with the given value to the container.
auto ret = copy(begin(a1), end(a1), a2);
The value returned by copy is the (incremented) value of its destination iterator. That is, ret will point just past the last element copied into a2.
replace(ilst.begin(), ilst.end(), 0, 42);
This call replaces all instances of 0 by 42. If we want to leave the original sequence unchanged, we can call replace_copy.
back_inserter is a insert iterator, what iterator adaptor that generates an iterator that uses a container operation to add elements to a given container.
the algorithms don’t change the size, but the iterator can change it by using the container operation.
Eliminating Duplicates
- The library algorithms operate on iterators, not containers. Therefore, an
algorithm cannot (directly) add or remove elements. - To actually remove the unused elements, we must use a container operation
A lambda expression represents a callable unit of code. It can be thought of as an unnamed, inline function.
lambda
http://www.cnblogs.com/knowledgesea/p/3163725.html[capture list] (parameter list) -> return type { function body }
The capture list directs the lambda to include information needed to access those variables within the lambda itself.
The capture list is used for local nonstatic variables only; lambdas can use local statics and variables declared outside the function directly.
When we capture a variable by reference, we must ensure that the variable exists at the time that the lambda executes.
By default, a lambda may not change the value of a variable that it copies by value.
be able to change the value of a captured variable, we must follow the parameter list with the keyword mutable.
Insert Iterators
• back_inserter (§ 10.2.2, p. 382) creates an iterator that uses push_back.
• front_inserter creates an iterator that uses push_front.
• inserter creates an iterator that uses insert. This function takes a second argument, which must be an iterator into the given container. Elements are inserted ahead of the element denoted by the given iterator.
it is not possible to create a reverse iterator from a forward_list or a stream iterator.
List the five iterator categories and the operations that each supports.
• Input iterators : ==, !=, ++, , ->
• Output iterators : ++,
• Forward iterators : ==, !=, ++, , ->
• Bidirectional iterators : ==, !=, ++, –, , ->
• Random-access iterators : ==, !=, <, <=, >, >=, ++, –, +, +=, -, -=, -(two iterators), , ->, iter[n] == (iter + n)
The list member versions should be used in preference to the generic algorithms for lists and forward_lists.
a crucially important difference between the list-specific and the generic versions is that the list versions change the underlying container.