文章目录
- all_of :所有元素是否都
- any_of :所有元素是否存在
- none_of :是否都没有满足条件
- for_each:将自定义函数用于指定范围
- find :查找指定范围内的值
- find_if :获取自定义函数查找指定范围内的第一个元素
- adjacent_find:查找范围内相等的相邻元素
- count:统计该范围内出现的次数
- equal:两个范围内的元素是否相等
- is_permutation:判断范围是否是另一个排列【与顺序无关】
- search:子序列的搜索范围
- copy:复制指定范围元素
- move:元素移动
- swap:交换两个对象的值
- transform:处理指定范围的值
- replace:替换指定范围内的值
- fill:用值填充
- generate:生成
- remove:将指定范围的数值移除,需要修改索引
- unique:删除重复值
- reverse:指定范围反转
- rotate:将指定范围旋转
- random_shuffle:随机打乱
- sort:排序
- merge:合并两个有序的列表
- includes:是否包含序列
- set_union:求两个序列的并集
- set_intersection:求两个集合的交集
- set_difference:求两个序列的差异
- push_heap:将元素推入堆范围
- max、min:判断最大或最小
- minmax_element:获取序列中的最大最小元素
- next_permutation:全排列
all_of :所有元素是否都
src
/**
* @func: 如果pred返回true;
* 范围内的所有元素[first,last)或范围为空,则返回;
* 否则返回false;
*/
template<class InputIterator, class UnaryPredicate>
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (!pred(*first)) return false;
++first;
}
return true;
}
案例
/* 判断范围内是否都满足func */
void test_all_of() {
std::vector<int> vc = {1, 3, 4, 5, 6};
bool rt = std::all_of(vc.begin(), vc.end(), [](int i){
return i != 0;
});
std::cout << "vc 中是否都没有0:" << (rt ? "是" : "否") << std::endl;
}
any_of :所有元素是否存在
src
template<class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return true;
++first;
}
return false;
}
案例
void test_any_of() {
std::vector<int> vc = {1, 3, 4, 5, 6};
bool rt = std::any_of(vc.begin(), vc.end(), [](int i){
return i != 0;
});
std::cout << "vc 中是否存在0:" << (rt ? "否" : "是") << std::endl;
}
none_of :是否都没有满足条件
src
template<class InputIterator, class UnaryPredicate>
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return false;
++first;
}
return true;
}
案例
void test_none_of() {
std::vector<int> vc = {1, 3, 14, 15, 6};
bool rt = std::none_of(vc.begin(), vc.end(), [](int i){
return i > 9;
});
std::cout << "vc 中是否都大于9:" << (rt ? "是" : "否") << std::endl;
}
for_each:将自定义函数用于指定范围
src
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) {
fn (*first);
++first;
}
return fn;
}
案例
void test_for_each() {
std::vector<int> vc = {1, 3, 14, 15, 6};
std::for_each(vc.begin(), vc.end(), [](int& i){
i += 2;
});
for(auto& i : vc)
std::cout << i << " ";
std::cout << std::endl;
}
find :查找指定范围内的值
若没有找到返回
end()
;
src
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
案例
void test_find() {
std::vector<int> vc = {1, 3, 14, 15, 6};
auto i = std::find(vc.begin(), vc.end(), 3);
std::cout << *i;
}
find_if :获取自定义函数查找指定范围内的第一个元素
- find_if_not:条件相反;
- find_end:从后面查找;
src
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
案例
void test_find_if() {
std::vector<int> vc = {1, 3, 14, 15, 6};
auto i = std::find_if(vc.begin(), vc.end(), [](int i){
return i > 4;
});
std::cout << *i;
}
adjacent_find:查找范围内相等的相邻元素
src
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
案例
void test_adjacent_find() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
auto it = std::adjacent_find(vc.begin(), vc.end());
std::cout << *it;
}
count:统计该范围内出现的次数
- count_if:阔以使用自定义函数进行统计;
src
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (*first == val) ++ret;
++first;
}
return ret;
}
案例
void test_count() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
int it = std::count(vc.begin(), vc.end(), 8);
std::cout << it << std::endl;
}
equal:两个范围内的元素是否相等
也阔以传入自定义函数;
src
template <class InputIterator1, class InputIterator2>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
bool equal (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, BinaryPredicate pred);
案例
void test_equal() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc1 = {1, 3, 3, 15, 15, 8};
bool it = std::equal(vc.begin(), vc.end(), vc1.begin());
std::cout << it << std::endl;
}
is_permutation:判断范围是否是另一个排列【与顺序无关】
src
template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, BinaryPredicate pred);
案例
void test_is_permutation() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc1 = {1, 3, 8, 15, 3, 15};
bool it = std::is_permutation(vc.begin(), vc.end(), vc1.begin());
std::cout << it << std::endl;
}
search:子序列的搜索范围
search_n:指定位置搜索特定值出现的个数;
src
template <class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2);
{
if (first2==last2) return first1; // specified in C++11
while (first1!=last1)
{
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1==*it2) { // or: while (pred(*it1,*it2)) for version 2
++it1; ++it2;
if (it2==last2) return first1;
if (it1==last1) return last1;
}
++first1;
}
return last1;
}
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
案例
void test_search () {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc1 = {1, 3};
auto it = std::search (vc.begin(), vc.end(), vc1.begin(), vc1.end());
std::cout << "vc1在vc中第一次出现的位置:" << it - vc.begin() << std::endl;
}
copy:复制指定范围元素
- copy_if:自定义函数指定复制;
- copy_backward:向后复制;
src
template<class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = *first;
++result; ++first;
}
return result;
}
案例
void test_copy() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc1;
vc1.resize(vc.size());
std::copy(vc.begin(), vc.end(), vc1.begin());
for(auto& i:vc){
std::cout << i << std::endl;
}
}
move:元素移动
使用该方法移动是,原对象无效;
src
template<class InputIterator, class OutputIterator>
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result)
{
while (first!=last) {
*result = std::move(*first);
++result; ++first;
}
return result;
}
案例
void test_move() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc1;
std::vector<int> vc2;
vc2.resize(vc.size());
vc1.resize(vc.size());
std::move(vc.begin(), vc.end(), vc1.begin());
for(auto& i:vc1){
std::cout << i << " ";
}
std::cout << std::endl;
vc2 = std::move(vc1);
for(auto& i:vc2){
std::cout << i << " ";
}
}
swap:交换两个对象的值
src
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
transform:处理指定范围的值
src
template <class InputIterator, class OutputIterator, class UnaryOperator>
OutputIterator transform (InputIterator first1, InputIterator last1,
OutputIterator result, UnaryOperator op)
{
while (first1 != last1) {
*result = op(*first1); // or: *result=binary_op(*first1,*first2++);
++result; ++first1;
}
return result;
}
template <class InputIterator1, class InputIterator2,
class OutputIterator, class BinaryOperation>
OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, OutputIterator result,
BinaryOperation binary_op);
案例
void test_transform() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::vector<int> vc2;
vc2.resize(vc.size());
std::transform(vc.begin(), vc.end(), vc2.begin(), [](int &i) {
return i*2;
});
for(auto& i:vc2){
std::cout << i << " ";
}
std::cout << std::endl;
}
replace:替换指定范围内的值
- replace:自定义函数指定替换;
- replace_copy:将替换好列表i的复制;
src
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
案例
void test_replace() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::replace(vc.begin(), vc.end(), 3, 20);
for(auto& i:vc){
std::cout << i << " ";
}
std::cout << std::endl;
}
fill:用值填充
src
template <class ForwardIterator, class T>
void fill (ForwardIterator first, ForwardIterator last, const T& val)
{
while (first != last) {
*first = val;
++first;
}
}
案例
void test_fill() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
std::fill(vc.begin(), vc.begin()+3, 100);
for(auto& i:vc){
std::cout << i << " ";
}
std::cout << std::endl;
}
generate:生成
src
template <class ForwardIterator, class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{
while (first != last) {
*first = gen();
++first;
}
}
案例
void test_generate() {
std::srand(unsigned(std::time(0)));
std::vector<int> vc;
vc.resize(5);
std::generate(vc.begin(), vc.end(), [](){
return (std::rand()%10);
});
for(auto& i:vc){
std::cout << i << " ";
}
std::cout << std::endl;
}
remove:将指定范围的数值移除,需要修改索引
src
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
if (result!=first)
*result = *first;
++result;
}
++first;
}
return result;
}
案例
void test_remove() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
auto it = std::remove(vc.begin(), vc.end(), 3);
for(auto& i:vc){
std::cout << i << " ";
}
std::cout << std::endl;
vc.erase(it, vc.end()); // 需要删除后面的索引
for(auto& i:vc){
std::cout << i << " ";
}
}
unique:删除重复值
src
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last;
ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
案例
void test_unique() {
std::vector<int> vc = {1, 3, 3, 15, 15, 8};
auto it = std::unique(vc.begin(), vc.end());
vc.erase(it, vc.end());
for(auto& i:vc){
std::cout << i << " ";
}
}
reverse:指定范围反转
src
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
案例
void test_reverse() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::reverse(vc.begin(), vc.end());
for(auto& i:vc){
std::cout << i << " ";
}
}
rotate:将指定范围旋转
src
template <class ForwardIterator>
void rotate (ForwardIterator first, ForwardIterator middle,
ForwardIterator last)
{
ForwardIterator next = middle;
while (first!=next)
{
swap (*first++,*next++);
if (next==last) next=middle;
else if (first==middle) middle=next;
}
}
案例
void test_rotate() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
// 开头到第二个开始旋转
std::rotate(vc.begin(), vc.begin()+2, vc.end());
for(auto& i:vc){
std::cout << i << " ";
}
}
random_shuffle:随机打乱
src
template <class RandomAccessIterator, class RandomNumberGenerator>
void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
RandomNumberGenerator& gen)
{
iterator_traits<RandomAccessIterator>::difference_type i, n;
n = (last-first);
for (i=n-1; i>0; --i) {
swap (first[i],first[gen(i+1)]);
}
}
案例
void test_random_shuffle() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::random_shuffle(vc.begin(), vc.end());
for(auto& i:vc){
std::cout << i << " ";
}
}
sort:排序
可提供自定义排序规则;
src
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
案例
void test_sort() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::sort(vc.begin(), vc.end());
for(auto& i:vc){
std::cout << i << " ";
}
}
merge:合并两个有序的列表
src
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true) {
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
*result++ = (*first2<*first1)? *first2++ : *first1++;
}
}
案例
void test_merge() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::vector<int> vc2 = {1, 6, 2, 9, 14, 8};
std::vector<int> vc1;
vc1.resize(vc.size() + vc2.size());
std::sort(vc.begin(), vc.end());
std::sort(vc2.begin(), vc2.end());
std::merge(vc.begin(), vc.end(), vc2.begin(), vc2.end(), vc1.begin());
for(auto& i:vc1){
std::cout << i << " ";
}
}
includes:是否包含序列
src
template <class InputIterator1, class InputIterator2>
bool includes (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while (first2!=last2) {
if ( (first1==last1) || (*first2<*first1) ) return false;
if (!(*first1<*first2)) ++first2;
++first1;
}
return true;
}
案例
void test_includes() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::vector<int> vc2 = {1, 3, 2};
bool rt = std::includes(vc.begin(), vc.end(), vc2.begin(), vc2.end());
std::cout << "vc是否包含vc2" << rt << std::endl;
}
set_union:求两个序列的并集
src
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (true)
{
if (first1==last1) return std::copy(first2,last2,result);
if (first2==last2) return std::copy(first1,last1,result);
if (*first1<*first2) { *result = *first1; ++first1; }
else if (*first2<*first1) { *result = *first2; ++first2; }
else { *result = *first1; ++first1; ++first2; }
++result;
}
}
案例
void test_set_union() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::vector<int> vc2 = {1, 3, 2, 19, 11, 20};
std::vector<int> vc1;
vc1.resize(vc.size() + vc2.size());
auto rt = std::set_union(vc.begin(), vc.end(), vc2.begin(), vc2.end(), vc1.begin());
vc1.resize(rt-vc1.begin());
for(auto& i:vc1){
std::cout << i << " ";
}
}
set_intersection:求两个集合的交集
src
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
*result = *first1;
++result; ++first1; ++first2;
}
}
return result;
}
案例
void test_set_intersection () {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::vector<int> vc2 = {1, 3, 2, 19, 11, 20};
std::vector<int> vc1;
vc1.resize(vc.size() + vc2.size());
auto rt = std::set_intersection (vc.begin(), vc.end(), vc2.begin(), vc2.end(), vc1.begin());
vc1.resize(rt-vc1.begin());
for(auto& i:vc1){
std::cout << i << " ";
}
}
set_difference:求两个序列的差异
src
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result)
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) { *result = *first1; ++result; ++first1; }
else if (*first2<*first1) ++first2;
else { ++first1; ++first2; }
}
return std::copy(first1,last1,result);
}
案例
void test_set_difference(){
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::vector<int> vc2 = {1, 3, 2, 19, 11, 20};
std::vector<int> vc1;
vc1.resize(vc.size() + vc2.size());
auto rt = std::set_difference(vc.begin(), vc.end(), vc2.begin(), vc2.end(), vc1.begin());
vc1.resize(rt-vc1.begin());
for(auto& i:vc1){
std::cout << i << " ";
}
}
push_heap:将元素推入堆范围
- pop_heap:删除堆顶;
- make_heap:创建堆;
- sort_push:排序;
- is_heap:是否为堆;
src
template <class RandomAccessIterator>
void push_heap (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void push_heap (RandomAccessIterator first, RandomAccessIterator last,
Compare comp);
案例
void test_heap(){
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
std::make_heap(vc.begin(), vc.end());
std::cout << "heap top:" << vc.front() << std::endl;
std::pop_heap(vc.begin(), vc.end());
vc.pop_back();
std::cout << "pop after, heap top:" << vc.front() << std::endl;
vc.push_back(300);
std::push_heap(vc.begin(), vc.end());
std::cout << "add after, heap top:" << vc.front() << std::endl;
for(auto& i:vc) {
std::cout << i << " ";
}
}
max、min:判断最大或最小
src
template <class T> const T& min (const T& a, const T& b) {
return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)
}
案例
void test_min_max() {
std::cout << "min: " << std::min(4, 1) << std::endl;
std::cout << "max: " << std::max(4, 1) << std::endl;
}
minmax_element:获取序列中的最大最小元素
src
template <class ForwardIterator>
pair<ForwardIterator,ForwardIterator>
minmax_element (ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class Compare>
pair<ForwardIterator,ForwardIterator>
minmax_element (ForwardIterator first, ForwardIterator last, Compare comp);
案例
void test_minmax_element() {
std::vector<int> vc = {1, 3, 2, 9, 15, 8};
auto rt = std::minmax_element(vc.begin(), vc.end());
std::cout << *rt.first << ", " << *rt.second << std::endl;
}
next_permutation:全排列
src
template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last);
template <class BidirectionalIterator, class Compare>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
案例
void test_next_permutation() {
std::vector<int> vc = {1, 3, 2};
std::sort(vc.begin(), vc.end());
std::cout << "全排列\n";
do {
std::cout << vc[0] << ' ' << vc[1] << ' ' << vc[2] << '\n';
} while ( std::next_permutation(vc.begin(), vc.end()));
}