reference

is_permutation

Test whether range is permutation of another

Parameters

  • first1, last1

    Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.

  • first2

    Input iterator to the initial position of the second sequence. The function considers as many elements of this sequence as those in the range [first1,last1). If this sequence is shorter, it causes undefined behavior. 所以这一段序列应该要长于前一段

  • pred

    Binary function that accepts two elements as argument (one of each of the two sequences, in the same order), and returns a value convertible to bool. The value returned indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

InputIterator1 and InputIterator2 shall point to the same type.

Return value

true if all the elements in the range [first1,last1) compare equal to those of the range starting at first2 in any order, and false otherwise.

Example

// is_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::is_permutation
#include <array> // std::array int main () {
std::array<int,5> foo = {1,2,3,4,5};
std::array<int,5> bar = {3,1,4,5,2}; if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )
std::cout << "foo and bar contain the same elements.\n"; return 0;
}

Output:

foo and bar contain the same elements.

search

Parameters

  • first1, last1

    Forward iterators to the initial and final positions of the searched sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.

  • first2, last2

    Forward iterators to the initial and final positions of the sequence to be searched for. The range used is [first2,last2). For (1), the elements in both ranges shall be of types comparable using operator== (with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).

  • pred

    Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to the first element of the first occurrence of [first2,last2) in [first1,last1)

  • If the sequence is not found, the function returns last1.
  • If [first2,last2) is an empty range, the function returns first1.

Example

// search algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::search
#include <vector> // std::vector bool mypredicate (int i, int j) {
return (i==j);
} int main () {
std::vector<int> haystack; // set some values: haystack: 10 20 30 40 50 60 70 80 90
for (int i=1; i<10; i++) haystack.push_back(i*10); // using default comparison:
int needle1[] = {40,50,60,70};
std::vector<int>::iterator it;
it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4); if (it!=haystack.end())
std::cout << "needle1 found at position " << (it-haystack.begin()) << '\n';
else
std::cout << "needle1 not found\n"; // using predicate comparison:
int needle2[] = {20,30,50};
it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate); if (it!=haystack.end())
std::cout << "needle2 found at position " << (it-haystack.begin()) << '\n';
else
std::cout << "needle2 not found\n"; return 0;
}

Output:

`needle1 found at position 3 needle2 not found `

search_n

Parameters

  • first, last

    Forward iterators to the initial and final positions of the searched sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • count

    Minimum number of successive elements to match. Size shall be (convertible to) an integral type.

  • val

    Individual value to be compared, or to be used as argument for pred (in the second version). for the first version, T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand size operands, and val as right-hand side).

  • pred

    Binary function that accepts two arguments (one element from the sequence as first, and val as second), and returns a value convertible to bool. The value returned indicates whether the element is considered a match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to the first element of the sequence.

If no such sequence is found, the function returns last.

Example

// search_n example
#include <iostream> // std::cout
#include <algorithm> // std::search_n
#include <vector> // std::vector bool mypredicate (int i, int j) {
return (i==j);
} int main () {
int myints[]={10,20,30,30,20,10,10,20};
std::vector<int> myvector (myints,myints+8); std::vector<int>::iterator it; // using default comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 30); if (it!=myvector.end())
std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
else
std::cout << "match not found\n"; // using predicate comparison:
it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate); if (it!=myvector.end())
std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
else
std::cout << "match not found\n"; return 0;
}

output:

Two 30s found at position 2
Two 10s found at position 5

swap

Parameters

  • a, b

    Two objects, whose contents are swapped.

Example

// swap algorithm example (C++98)
#include <iostream> // std::cout
#include <algorithm> // std::swap
#include <vector> // std::vector int main () { int x=10, y=20; // x:10 y:20
std::swap(x,y); // x:20 y:10 std::vector<int> foo (4,x), bar (6,y); // foo:4x20 bar:6x10
std::swap(foo,bar); // foo:6x10 bar:4x20 std::cout << "foo contains:";
for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

Output:

foo contains: 10 10 10 10 10 10

swap_ranges

Parameters

  • first1, last1

    Forward iterators to the initial and final positions in one of the sequences to be swapped. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.

  • first2

    Forward iterator to the initial position in the other sequence to be swapped. The range used includes the same number of elements as the range [first1,last1). The two ranges shall not overlap.

The ranges shall not overlap.

swap shall be defined to exchange the types pointed by both iterator types symmetrically (in both orders).

Return value

An iterator to the last element swapped in the second sequence.

Example

// swap_ranges example
#include <iostream> // std::cout
#include <algorithm> // std::swap_ranges
#include <vector> // std::vector int main () {
std::vector<int> foo (5,10); // foo: 10 10 10 10 10
std::vector<int> bar (5,33); // bar: 33 33 33 33 33 std::swap_ranges(foo.begin()+1, foo.end()-1, bar.begin()); // print out results of swap:
std::cout << "foo contains:";
for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; std::cout << "bar contains:";
for (std::vector<int>::iterator it=bar.begin(); it!=bar.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

Output:

foo contains: 10 33 33 33 10
bar contains: 10 10 10 33 33

iter_swap

Parameters

  • a, b

    Forward iterators to the objects to swap. swap shall be defined to exchange values of the type pointed to by the iterators.

Example

// iter_swap example
#include <iostream> // std::cout
#include <algorithm> // std::iter_swap
#include <vector> // std::vector int main () { int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50
std::vector<int> myvector (4,99); // myvector: 99 99 99 99 std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50
// myvector: [10] 99 99 99 std::iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99] 50
// myvector: 10 99 [40] 99 std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

myvector contains: 10 99 40 99

transform

Parameters

  • first1, last1

    Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed to by first1 but not the element pointed to by last1.

  • first2

    Input iterator to the initial position of the second range. The range includes as many elements as [first1,last1).

  • result

    Output iterator to the initial position of the range where the operation results are stored. The range includes as many elements as [first1,last1).

  • op

    Unary function that accepts one element of the type pointed to by InputIterator as argument, and returns some result value convertible to the type pointed to by OutputIterator. This can either be a function pointer or a function object.

  • binary_op

    Binary function that accepts two elements as argument (one of each of the two sequences), and returns some result value convertible to the type pointed to by OutputIterator. This can either be a function pointer or a function object.

Neither nor should directly modify the elements passed as its arguments: These are indirectly modified by the algorithm (using the return value) if the same range is specified for .

Return value

An iterator pointing to the element that follows the last element written in the result sequence.

Example

// transform algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::transform
#include <vector> // std::vector
#include <functional> // std::plus int op_increase (int i) { return ++i; } int main () {
std::vector<int> foo;
std::vector<int> bar; // set some values:
for (int i=1; i<6; i++)
foo.push_back (i*10); // foo: 10 20 30 40 50 bar.resize(foo.size()); // allocate space std::transform (foo.begin(), foo.end(), bar.begin(), op_increase);
// bar: 11 21 31 41 51 // std::plus adds together its two arguments:
std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
// foo: 21 41 61 81 101 std::cout << "foo contains:";
for (std::vector<int>::iterator it=foo.begin(); it!=foo.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

Output:

foo contains: 21 41 61 81 101

replace

Parameters

  • first, last

    Forward iterators to the initial and final positions in a sequence of elements that support being compared and assigned a value of type T. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • old_value

    Value to be replaced.

  • new_value

    Replacement value.

Example

// replace algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::replace
#include <vector> // std::vector int main () {
int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
std::vector<int> myvector (myints, myints+8); // 10 20 30 30 20 10 10 20 std::replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99 std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

myvector contains: 10 99 30 30 99 10 10 99

replace_if

Parameters

  • first, last

    Forward iterators to the initial and final positions in a sequence of elements that support being assigned a value of type T. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • pred

    Unary function that accepts an element in the range as argument, and returns a value convertible to bool. The value returned indicates whether the element is to be replaced (if true, it is replaced). The function shall not modify its argument. This can either be a function pointer or a function object.

  • new_value

    Value to assign to replaced elements.

Example

// replace_if example
#include <iostream> // std::cout
#include <algorithm> // std::replace_if
#include <vector> // std::vector bool IsOdd (int i) { return ((i%2)==1); } int main () {
std::vector<int> myvector; // set some values:
for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::replace_if (myvector.begin(), myvector.end(), IsOdd, 0); // 0 2 0 4 0 6 0 8 0 std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

myvector contains: 0 2 0 4 0 6 0 8 0

fill

Parameters

  • first, last

    Forward iterators to the initial and final positions in a sequence of elements that support being assigned a value of type T. The range filled is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • val

    Value to assign to the elements in the filled range.

Example

// fill algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::fill
#include <vector> // std::vector int main () {
std::vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0 std::fill (myvector.begin(),myvector.begin()+4,5); // myvector: 5 5 5 5 0 0 0 0
std::fill (myvector.begin()+3,myvector.end()-2,8); // myvector: 5 5 5 8 8 8 0 0 std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

myvector contains: 5 5 5 8 8 8 0 0

reverse

Parameters

  • first, last

    Bidirectional iterators to the initial and final positions of the sequence to be reversed. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. BidirectionalIterator shall point to a type for which swap is properly defined.

Example

// reverse algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::reverse
#include <vector> // std::vector int main () {
std::vector<int> myvector; // set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 std::reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1 // print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

myvector contains: 9 8 7 6 5 4 3 2 1

random_shuffle

Parameters

  • first, last

    Random-access iterators to the initial and final positions of the sequence to be shuffled. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • gen

    Unary function taking one argument and returning a value, both convertible to/from the appropriate difference type used by the iterators. The function shall return a non-negative value less than its argument. This can either be a function pointer or a function object.

RandomAccessIterator shall point to a type for which swap is defined and swaps the value of its arguments.

Example

// random_shuffle example
#include <iostream> // std::cout
#include <algorithm> // std::random_shuffle
#include <vector> // std::vector
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand // random generator function:
int myrandom (int i) { return std::rand()%i;} int main () {
std::srand ( unsigned ( std::time(0) ) );
std::vector<int> myvector; // set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator:
std::random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom:
std::random_shuffle ( myvector.begin(), myvector.end(), myrandom); // print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it; std::cout << '\n'; return 0;
}

output:

myvector contains: 3 4 1 6 8 9 2 7 5

shuffle

Parameters

  • first, last

    Forward iterators to the initial and final positions of the sequence to be shuffled. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. ForwardIterator shall point to a type for which swap is defined and swaps the value of its arguments.

  • g

    A uniform random number generator, used as the source of randomness. URNG shall be a uniform random number generator, such as one of the standard generator classes (see for more information).

Example

// shuffle algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::shuffle
#include <array> // std::array
#include <random> // std::default_random_engine
#include <chrono> // std::chrono::system_clock int main () {
std::array<int,5> foo {1,2,3,4,5}; // obtain a time-based seed:
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); shuffle (foo.begin(), foo.end(), std::default_random_engine(seed)); std::cout << "shuffled elements:";
for (int& x: foo) std::cout << ' ' << x;
std::cout << '\n'; return 0;
}

Possible output:

shuffled elements: 3 1 4 2 5

is_sorted

Parameters

  • first, last

    Forward iterators to the initial and final positions of the sequence. The range checked is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • comp

    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

true if the range [first,last) is sorted into ascending order, false otherwise.

If the range [first,last) contains less than two elements, the function always returns true.

Example

// is_sorted example
#include <iostream> // std::cout
#include <algorithm> // std::is_sorted, std::prev_permutation
#include <array> // std::array int main () {
std::array<int,4> foo {2,4,1,3}; do {
// try a new permutation:
std::prev_permutation(foo.begin(),foo.end()); // print range:
std::cout << "foo:";
for (int& x:foo) std::cout << ' ' << x;
std::cout << '\n'; } while (!std::is_sorted(foo.begin(),foo.end())); std::cout << "the range is sorted!\n"; return 0;
}
Edit & Run

output:

foo: 2 3 4 1
foo: 2 3 1 4
foo: 2 1 4 3
foo: 2 1 3 4
foo: 1 4 3 2
foo: 1 4 2 3
foo: 1 3 4 2
foo: 1 3 2 4
foo: 1 2 4 3
foo: 1 2 3 4
the range is sorted!

is_sorted_until

Parameters

  • first, last

    Forward iterators to the initial and final positions in a sequence. The range checked is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • comp

    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to the first element in the range which does not follow an ascending order, or last if all elements are sorted or if the range contains less than two elements.

Example

// is_sorted_until example
#include <iostream> // std::cout
#include <algorithm> // std::is_sorted_until, std::prev_permutation
#include <array> // std::array int main () {
std::array<int,4> foo {2,4,1,3};
std::array<int,4>::iterator it; do {
// try a new permutation:
std::prev_permutation(foo.begin(),foo.end()); // print range:
std::cout << "foo:";
for (int& x:foo) std::cout << ' ' << x;
it=std::is_sorted_until(foo.begin(),foo.end());
std::cout << " (" << (it-foo.begin()) << " elements sorted)\n"; } while (it!=foo.end()); std::cout << "the range is sorted!\n"; return 0;
}

Output:

foo: 2 3 4 1 (3 elements sorted)
foo: 2 3 1 4 (2 elements sorted)
foo: 2 1 4 3 (1 elements sorted)
foo: 2 1 3 4 (1 elements sorted)
foo: 1 4 3 2 (2 elements sorted)
foo: 1 4 2 3 (2 elements sorted)
foo: 1 3 4 2 (3 elements sorted)
foo: 1 3 2 4 (2 elements sorted)
foo: 1 2 4 3 (3 elements sorted)
foo: 1 2 3 4 (4 elements sorted)
the range is sorted!

lower_bound

Parameters

  • first, last

    Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • val

    Value of the lower bound to search for in the range. For (1), T shall be a type supporting being compared with elements of the range [first,last) as the right-hand side operand of operator<.

  • comp

    Binary function that accepts two arguments (the first of the type pointed by ForwardIterator, and the second, always val), and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to the lower bound of val in the range.

If all the element in the range compare less than val, the function returns last.

Example

// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), 20); // ^
up= std::upper_bound (v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0;
}

output:

lower_bound at position 3
upper_bound at position 6

upper_bound

Parameters

  • first, last

    Forward iterators to the initial and final positions of a sorted (or properly partitioned) sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • val

    Value of the upper bound to search for in the range. For (1), T shall be a type supporting being compared with elements of the range [first,last) as the left-hand side operand of operator<.

  • comp

    Binary function that accepts two arguments (the first is always val, and the second of the type pointed by ForwardIterator), and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to the upper bound position for val in the range.

If no element in the range compares greater than val, the function returns last.

Example

// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vector int main () {
int myints[] = {10,20,30,30,20,10,10,20};
std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low,up;
low=std::lower_bound (v.begin(), v.end(), 20); // ^
up= std::upper_bound (v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0;
}

output:

lower_bound at position 3
upper_bound at position 6

merge

Parameters

  • first1, last1

    Input iterators to the initial and final positions of the first sorted sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.

  • first2, last2

    Input iterators to the initial and final positions of the second sorted sequence. The range used is [first2,last2).

  • result

    Output iterator to the initial position of the range where the resulting combined range is stored. Its size is equal to the sum of both ranges above.

  • comp

    Binary function that accepts two arguments of the types pointed by the iterators, and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

The ranges shall not overlap.The elements in both input ranges should be assignable to the elements in the range pointed by result. They should also be comparable (with operator< for (1), and with comp for (2)).

Return value

An iterator pointing to the past-the-end element in the resulting sequence.

Example

// merge algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::merge, std::sort
#include <vector> // std::vector int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10); std::sort (first,first+5);
std::sort (second,second+5);
std::merge (first,first+5,second,second+5,v.begin()); std::cout << "The resulting vector contains:";
for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

output:

The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

min

Parameters

  • a, b

    Values to compare.

  • comp

    Binary function that accepts two values of type T as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

  • il

    An initializer_list object. These objects are automatically constructed from initializer list declarators.

Return value

The lesser of the values passed as arguments.

Example

// min example
#include <iostream> // std::cout
#include <algorithm> // std::min int main () {
std::cout << "min(1,2)==" << std::min(1,2) << '\n';
std::cout << "min(2,1)==" << std::min(2,1) << '\n';
std::cout << "min('a','z')==" << std::min('a','z') << '\n';
std::cout << "min(3.14,2.72)==" << std::min(3.14,2.72) << '\n';
return 0;
}
Edit & Run

output:

min(1,2)==1
min(2,1)==1
min('a','z')==a
min(3.14,2.72)==2.72

max

同min,具体请看文档

Example

// max example
#include <iostream> // std::cout
#include <algorithm> // std::max int main () {
std::cout << "max(1,2)==" << std::max(1,2) << '\n';
std::cout << "max(2,1)==" << std::max(2,1) << '\n';
std::cout << "max('a','z')==" << std::max('a','z') << '\n';
std::cout << "max(3.14,2.73)==" << std::max(3.14,2.73) << '\n';
return 0;
}

Output:

`max(1,2)==2 max(2,1)==2 max('a','z')==z max(3.14,2.73)==3.14 `

min_element

Parameters

  • first, last

    Input iterators to the initial and final positions of the sequence to compare. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • comp

    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to smallest value in the range, or last if the range is empty.

Example

// min_element/max_element example
#include <iostream> // std::cout
#include <algorithm> // std::min_element, std::max_element bool myfn(int i, int j) { return i<j; } struct myclass {
bool operator() (int i,int j) { return i<j; }
} myobj; int main () {
int myints[] = {3,7,2,5,6,4,9}; // using default comparison:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n'; // using function myfn as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n'; // using object myobj as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n'; return 0;
}

Output:

The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9

max_element

Parameters

  • first, last

    Input iterators to the initial and final positions of the sequence to compare. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  • comp

    Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

An iterator to largest value in the range, or last if the range is empty.

Example

// min_element/max_element example
#include <iostream> // std::cout
#include <algorithm> // std::min_element, std::max_element bool myfn(int i, int j) { return i<j; } struct myclass {
bool operator() (int i,int j) { return i<j; }
} myobj; int main () {
int myints[] = {3,7,2,5,6,4,9}; // using default comparison:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7) << '\n'; // using function myfn as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myfn) << '\n'; // using object myobj as comp:
std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
std::cout << "The largest element is " << *std::max_element(myints,myints+7,myobj) << '\n'; return 0;
}

Output:

The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9

next_permutation

Parameters

  • first, last

    Bidirectional iterators to the initial and final positions of the sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. BidirectionalIterator shall point to a type for which swap is properly defined.

  • comp

    Binary function that accepts two arguments of the type pointed by BidirectionalIterator, and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

true if the function could rearrange the object as a lexicographicaly greater permutation.

Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

Example

// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort int main () {
int myints[] = {1,2,3}; std::sort (myints,myints+3); std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::next_permutation(myints,myints+3) ); std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; return 0;
}

Output:

The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

prev_permutation

Parameters

  • first, last

    Bidirectional iterators to the initial and final positions of the sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. BidirectionalIterator shall point to a type for which swap is properly defined.

  • comp

    Binary function that accepts two arguments of the type pointed by BidirectionalIterator, and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Return value

true if the function could rearrange the object as a lexicographicaly smaller permutation.

Otherwise, the function returns false to indicate that the arrangement is not less than the previous, but the largest possible (sorted in descending order).

Example

// next_permutation example
#include <iostream> // std::cout
#include <algorithm> // std::next_permutation, std::sort, std::reverse int main () {
int myints[] = {1,2,3}; std::sort (myints,myints+3);
std::reverse (myints,myints+3); std::cout << "The 3! possible permutations with 3 elements:\n";
do {
std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
} while ( std::prev_permutation(myints,myints+3) ); std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; return 0;
}

output:

3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
After loop: 3 2 1

【pat】algorithm常用函数整理的更多相关文章

  1. NiosII常用函数整理

    NiosII常用函数整理 IO操作函数函数原型:IORD(BASE, REGNUM) 输入参数:BASE为寄存器的基地址,REGNUM为寄存器的偏移量函数说明:从基地址为BASE的设备中读取寄存器中偏 ...

  2. pandas 常用函数整理

    pandas常用函数整理,作为个人笔记. 仅标记函数大概用途做索引用,具体使用方式请参照pandas官方技术文档. 约定 from pandas import Series, DataFrame im ...

  3. oracle常用函数整理

    oracle常用函数整理    1.绝对值.取余.判断数值正负函数    绝对值:ABS(n)        示例: SELECT ABS(100),ABS(-100),ABS('100') FROM ...

  4. Python 学习:常用函数整理

    整理Python中常用的函数 一,把字符串形式的list转换为list 使用ast模块中的literal_eval函数来实现,把字符串形式的list转换为Python的基础类型list from as ...

  5. JavaScript Array 常用函数整理

    按字母顺序整理 索引 Array.prototype.concat() Array.prototype.filter() Array.prototype.indexOf() Array.prototy ...

  6. mysql常用函数整理

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 ) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 SELECT CEIL( ...

  7. js常用函数整理

    类型转换:parseInt\parseFloat\toString 类型判断:typeof;eg:if(typeof(var)!="undefined")\isNaN 字符处理函数 ...

  8. MySQL常用函数整理,建议收藏!

    常见函数 字符串函数 数字函数 日期函数 聚合函数 流程控制函数 一.字符串函数 concat(s1,s2...,sn) --将s1,s2...,sn连接成字符串,如果该函数中的任何参数为 null, ...

  9. PHP常用函数整理

    推荐网址:http://php.net/manual/zh/http://www.w3cschool.cc/php/php-ref-array.html 错误报告: error_reporting(E ...

随机推荐

  1. Soc常见问题

    SOC常见问题解答 1.SOC FPGA中的ARM是软核还是硬核?ARM核的外设是软核还是硬核? SOC FPGA 中的ARM核是硬核.所以简称HPS,Hardware Processor Syste ...

  2. WebGPU学习系列目录

    介绍 大家好,本系列从0开始学习WebGPU API,并给出相关的demo. WebGPU介绍 WebGPU相当于DX12/Vulkan,能让程序员更灵活地操作GPU,从而大幅提升性能. 为什么要学习 ...

  3. 自制导纳信号发生器 [原创cnblogs.com/helesheng]

    最近正在研制一种通过测量人体导纳,估算体内血液变化率,进而评估心血管系统泵血功能的医疗仪器.为测量人体导纳,我们设计了一套巧妙的激励信号幅度反馈电路,该电路由于涉及商业机密就不在这里讨论了.这里主要分 ...

  4. python GUI编程tkinter示例之目录树遍历工具

    摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...

  5. php使用微信登录

    1.第一步 $hosturl = urlencode('');//异步回调地址 $wechatInfo = WechatInfo::get_wechat(); //查询appid $url = &qu ...

  6. Android 日期对话框 DatePickerDialog

    private int year; private int monthOfYear; private int dayOfMonth; @Override protected void onCreate ...

  7. VS2019 开发Django(四)------models

    导航:VS2019开发Django系列 继上篇我们匆匆迁移数据库成功之后,又花了一个晚上研究了Django的模型,这里的模型其实就是ORM模型中的Entity,.Net里边用的比较多的有Entity ...

  8. Java描述设计模式(14):解释器模式

    本文源码:GitHub·点这里 || GitEE·点这里 一.解释器模式 1.基础概念 解释器模式是对象的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端 ...

  9. 计算机组成原理——I/O接口以及I/O设备数据传送控制方式

    接口可以看作是两个部件之间交接的部分.硬件与硬件之间有接口,硬件与软件之间有接口,软件与软件之间也有接口. 这里我们所说的I/O接口,一边连接着主机,一边连接着外设. I/O接口的功能 I/O接口的基 ...

  10. vue3.0 本地调试时Invalid Host header

    问题场景: vue在开发时只能在本地浏览器里查看效果,我想在手机端真机调试,连接的代码还是本地环境,这样就不用频繁的发布了,于是绑定域名并指向本机的localhost:8080: 但是用域名访问时浏览 ...