Tips : Upper can see easily than stare at the lower
出于背景的明暗层次,有时候在上半页的文字会更容易分辨一些
The begin of Vector
The length of Vector
Comp [Optional]
#include <functional> -> greater<int>( )
int arr[ ] = { 0, 1, 5, 8, 9, 6, 7, 3, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr + 2, arr + n); // default std::less<int>( ) -> ascending order
sort(arr + 2, arr + n, greater<int>( )) -> descending order
C++- convertible to bool
- returns a value whether the passed “first” argument should be placed before the passed “second” argument or not
bool compareInterval(Interval i1, Interval i2){
return (i1.start < i2.start);
}
...
Interval arr[] = { { 6, 8 }, { 1, 9 }, { 2, 4 }, { 4, 7 } };
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n, compareInterval);
C++vector<int> v1, v2;
swap( v1, v2 );
C++vector<int> v1;
deque<int> d1;
swap_ranges ( v1.begin( ), v1.end( ), d1.begin( ) );
C++void selection_sort(ForwardIt begin, ForwardIt end)
{
for (ForwardIt it = begin; it != end; ++it)
std::iter_swap(it, std::min_element(it, end));
}
C++bool mod_equal ( int elem1, int elem2 ){
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 == elem2;
};
...
v1_NewEnd1 = unique ( v1.begin( ), v1.end( ) );
v1_NewEnd2 = unique ( v1.begin( ), v1_NewEnd1 , mod_equal );
v1_NewEnd3 = unique ( v1.begin( ), v1_NewEnd2, greater<int>( ) );
vector<int> vec;
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
C++bool mod_equal ( int elem1, int elem2 ){
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 == elem2;
};
...
v1_NewEnd1 = unique_copy ( v1.begin( ), v1.begin( ) + 8, v1.begin( ) + 8 );
v1_NewEnd2 = unique_copy ( v1.begin( ), v1.begin( ) + 14,
v1.begin( ) + 14 , mod_equal );
std::string s1 {"The string with many spaces!"};
std::string s2;
std::unique_copy(s1.begin(), s1.end(), std::back_inserter(s2),
[](char c1, char c2) { return c1 == ' ' && c2 == ' '; });
C++bool mod_lesser( int elem1, int elem2 ){
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
}
...
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end(), greater<int>());
sort(v3.begin(), v3.end(), mod_lesser);
Result = upper_bound(v1.begin(), v1.end(), 3);
Result = upper_bound(v2.begin(), v2.end(), 3, greater<int>());
Result = upper_bound(v3.begin(), v3.end(), 3, mod_lesser);
Starting vector v1 = ( -1 0 1 2 3 4 -3 -2 -1 0 ).
Original vector v1 with range sorted by the
binary predicate less than is v1 = ( -3 -2 -1 -1 0 0 1 2 3 4 ).
Original vector v2 with range sorted by the
binary predicate greater is v2 = ( 4 3 2 1 0 0 -1 -1 -2 -3 ).
Original vector v3 with range sorted by the
binary predicate mod_lesser is v3 = ( 0 0 -1 -1 1 -2 2 -3 3 4 ).
The upper_bound in v1 for the element with a value of 3 is: 4.
The upper_bound in v2 for the element with a value of 3 is: 2.
The upper_bound in v3 for the element with a value of 3 is: 4.
// 对于v1,upper_bound 在 v1 中找到第一个大于3的元素,也就是4。
// 对于v2,由于使用 greater<int>() 作为排序谓词,upper_bound 实际上在查找小于3的第一个元素的位置,即2。
// 对于v3,使用自定义的 mod_lesser 谓词进行排序,upper_bound 实际上在查找大于3的第一个元素,即4。
C++bool twice (int elem1, int elem2 ){
return elem1 * 2 == elem2;
}
...
result1 = adjacent_find( L.begin( ), L.end( ) );
result2 = adjacent_find( L.begin( ), L.end( ), twice );
L = ( 50 40 10 20 20 )
There are two adjacent elements that are equal.
They have a value of 20.
There are two adjacent elements where the second is twice the first.
They have values of 10 & 20.
C++vector<int> v1;
fill( v1.begin( ) + 5, v1.end( ), 2 );
C++vector<int> v1;
fill( v1.begin( ), 5, 2 );
C++cout << is_permutation(vec_3.begin(), vec_3.end(),
vec_4.begin(), vec_4.end(),
[](int lhs, int rhs) { return lhs % 2 == rhs % 2; }) << endl; // true
C++bool abs_greater ( int elem1, int elem2 ){
if ( elem1 < 0 )
elem1 = -elem1;
if ( elem2 < 0 )
elem2 = -elem2;
return elem1 < elem2;
};
...
const int& result1 = max(a, b, abs_greater);
const int& result2 = max(a, b);
vector<int> v1, v2;
v4 = max ( v1, v2 );
C++bool mod_lesser ( int elem1, int elem2 ){
if ( elem1 < 0 )
elem1 = - elem1;
if ( elem2 < 0 )
elem2 = - elem2;
return elem1 < elem2;
};
...
v1_R1_Iter = max_element ( v1.begin( ), v1.end( ) );
v1_R2_Iter = max_element ( v1.begin( ), v1.end( ), mod_lesser);
C++merge ( v1a.begin( ), v1a.end( ), v1b.begin( ), v1b.end( ), v1.begin( ) );
merge ( v2a.begin( ), v2a.end( ), v2b.begin( ), v2b.end( ),
v2.begin( ), greater<int>( ) );
merge ( v3a.begin( ), v3a.end( ), v3b.begin( ), v3b.end( ),
v3.begin( ), mod_lesser );
C++bool deq1Result;
deq1Result = next_permutation ( deq1.begin( ), deq1.end( ) );
next_permutation ( v1.begin( ), v1.end( ), mod_lesser );
std::vector<int> elements = {1, 2, 3, 4};
do {
// 在这里处理当前排列
for (int element : elements) {
std::cout << element << ' ';
}
std::cout << std::endl;
} while (std::next_permutation(elements.begin(), elements.end()));
C++nth_element(v1.begin( ), v1.begin( ) + 3, v1.end( ) );
nth_element( v1.begin( ), v1.begin( ) + 4, v1.end( ),greater<int>( ) );
nth_element( v1.begin( ), v1.begin( ) + 5, v1.end( ), UDgreater );
std::nth_element(v.begin(), v.begin() + 1, v.end(), std::greater{});
std::cout << "\nThe second largest element is " << v[1] << '\n';
std::cout << "The largest element is " << v[0] << '\n';
printVec(v);
The second largest element is 9
The largest element is 10
v = {10, 9, 6, 7, 6, 3, 5, 4, 3, 2};
C++rotate ( d1.begin( ), d1.begin( ) + 1 , d1.end( ) );
( 1 2 3 4 5 0 ) -> ( 2 3 4 5 0 1 )
C++