16 Standard Library Algorithms

目录:

  1. lambda expressions
  2. algorithms fill, fill_n, generate, generate_n
  3. Algorithms equal, mismatch and lexicographical_compare
  4. Algorithms replace, replace_if, replace_copy and replace_copy_if.
  5. Mathematical algorithm of the Standard Library
  6. Standard Library search and sort algorithm
  7. Algorithm swap, iter_swap and swap_ranges
  8. Algorithms copy_backward, merge, unique and reverse
  9. Algorithms inplace_merge, reverse_copy and unique_copy
  10. includes, set_difference, set_intersection, set_symmetric_difference and set_union

1. lambda expressions

// lambda expressions
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
using namespace std;


int main() {
    const size_t SIZE{4};
    array<int, SIZE> values{1,2, 3, 4};
    ostream_iterator<int> output{cout, " "};

    cout << "values contains: ";
    copy(values.cbegin(), values.cend(), output);

    cout << "\nDisplay each element multiplied by two: ";
    for_each(values.cbegin(), values.cend(), [](auto i) {cout << i*2 << " ";});

    int sum = 0;
    for_each(values.cbegin(), values.cend(), [&sum](auto i) {sum += i;});
    cout << "\nSum of value's elements is : " << sum << endl;

}


2. algorithms fill, fill_n, generate, generate_n

// algorithms fill, fill_n, generate, generate_n
#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>

using namespace std;

char nextLetter() {
    static char letter{'A'};
    return letter++;
}

int main() {
    array<char, 10> chars;
    fill(chars.begin(), chars.end(), '5');

    cout << "chars after filling with 5s:\n";
    ostream_iterator<char> output{cout , " "};
    copy(chars.cbegin(), chars.cend(), output);

    cout << "\n\nchars after filling five elements with As:\n";
    copy(chars.cbegin(), chars.cend(), output);

    // nextLetter
    generate(chars.begin(), chars.end(), nextLetter);
    cout << "\n\nchars after generating letters A-J:\n";
    copy(chars.cbegin(), chars.cend(), output);

    // generate_n
    generate_n(chars.begin(), 5, nextLetter);
    cout << "\n\nchars after generating K-O for the " << " first five elements:\n";
    copy(chars.cbegin(), chars.cend(), output);
    cout << endl;

    // lambda and generate_n
    generate_n(chars.begin(), 3,
               [](){
        static char letter{'A'};
        return letter++;
    }
               );
    cout << "\n\nchars after using a lambda to generate A-C" << "for the first three elements:\n";
    copy(chars.cbegin(), chars.cend(), output);
    cout << endl;
}


3. Algorithms equal, mismatch and lexicographical_compare

// Algorithms equal, mismatch and lexicographical_compare

#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>

using namespace std;

int main() {
    const size_t SIZE{10};
    array<int, SIZE> a1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    array<int, SIZE> a2{a1};
    array<int, SIZE> a3{1,2, 3, 4, 1000, 6, 7, 8, 9, 10};
    ostream_iterator<int> output{cout, " "};

    cout << "a1 contains: ";
    copy(a1.cbegin(), a1.cend(), output);
    cout << "\na2 contains: ";
    copy(a2.cbegin(), a2.cend(), output);
    cout << "\na3 contains: ";
    copy(a3.cbegin(), a3.cend(), output);

    // compare a1 and a2 for equality
    bool result{equal(a1.cbegin(), a1.cend(), a2.cbegin(), a2.cend())};
    cout << "\n\na1 " << (result ? "is":"is not") << " equal to a2\n";

    // compare a1 and a3
    bool result2{equal(a1.cbegin(), a1.cend(), a3.cbegin(), a3.cend())};
    cout << "\na1 " << (result2 ? "is":"is not") << " equal to a3\n";

    // check for mismatch between a1 and a3, 这里只找了第一个不同
    auto location = mismatch(a1.cbegin(), a1.cend(), a3.cbegin(), a3.cend());
    cout << "\nThere is a mismatch between a1 and a3 at location " << (location.first - a1.begin())
    << "\nwhere a1 contains: " << *location.first << " and a3 contains " << *location.second << "\n\n";

    char c1[SIZE] = "HELLO";
    char c2[SIZE] = "BYE BYE";

    // perform lexicographical comparison of c1 and c2
    result = lexicographical_compare(cbegin(c1), cend(c1), cbegin(c2), cend(c2));
    cout << c1 << (result ? " is less than " : " is greater than or equal to ") << c2 << endl;
}


4. Algorithms replace, replace_if, replace_copy and replace_copy_if

// Algorithms replace, replace_if, replace_copy and replace_copy_if.

#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>
using namespace std;

int main() {
    const size_t SIZE{10};
    array<int, SIZE> init{10, 2, 10, 4, 16, 6, 14, 8, 12, 10};
    ostream_iterator<int> output{cout , " "};

    array<int, SIZE> a1(init);
    cout << "a1 before replacing all 10s:\n";
    copy(a1.cbegin(), a1.cend(), output);

    // repalce a1  10 100
    replace(a1.begin(), a1.end(), 10, 100);
    cout << "\na1 after replacing 10s with 100s:\n";
    copy(a1.cbegin(), a1.cend(), output);

    array<int, SIZE> a2(init);
    array<int, SIZE> c1;
    cout << "\n\na2 before replacing all 10s and copying:\n";
    copy(a2.cbegin(), a2.cend(), output);
    cout << "\nc1 before replacing all 10s and copying:\n";
    copy(c1.cbegin(), c1.cend(), output);

    // copy from a2 to c1, replacing 10s with 100s
    replace_copy(a2.cbegin(), a2.cend(), c1.begin(), 10, 100);
    cout << "\n\na2 after replacing all 10s and copying:\n";
    copy(a2.cbegin(), a2.cend(), output);
    cout << "\nc1 after replacing all 10s and copying:\n";
    copy(c1.cbegin(), c1.cend(), output);

    // replace_if
    array<int, SIZE> a3(init);
    cout << "\n\na3 before replacing values greater than 9:\n ";
    copy(a3.cbegin(), a3.cend(), output);
    replace_if(a3.begin(), a3.end(), [](auto x) {return x > 9;}, 100);
    cout << "\na3 after replacing all values grater than 9 with 100s:\n ";
    copy(a3.cbegin(), a3.cend(), output);


    // replace_copy_if
    array<int, SIZE> a4(init);
    array<int, SIZE> c2;
    cout << "\n\na4 before replacing all values greater " << "than 9 and copying:\n ";
    copy(a4.cbegin(), a4.cend(), output);
    cout << "\nc2 before:\n";
    copy(c2.begin(), c2.end(), output);

    replace_copy_if(a4.cbegin(), a4.cend(), c2.begin(), [](auto x){return x >9 ;}, 100);
    cout << "\n\na4 after replacing all values greater " << "than 9 and copying:\n ";
    copy(a4.cbegin(), a4.cend(), output);
    cout << "\nc2 after replacing all values grater than 9 in a4:\n";
    copy(c2.begin(), c2.end(), output);
    cout << endl;
}


5. Mathematical algorithm of the Standard Library

// Mathematical algorithm of the Standard Library

#include <iostream>
#include <algorithm>
#include <numeric>
#include <array>
#include <iterator>
#include <random>

using namespace std;

int main() {
    const size_t SIZE{10};
    array<int, SIZE> a1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    ostream_iterator<int> output{cout , " "};

    cout << "a1 before random_shuffle: ";
    copy(a1.cbegin(), a1.cend(), output);

    // shuffle
    default_random_engine randomEngine{random_device{}()};
    shuffle(a1.begin(), a1.end(), randomEngine);  // 这里是会改变a1的,不能传cbegin, cend
    cout << "\na1 after random_shuffle: ";
    copy(a1.cbegin(), a1.cend(), output);

    array<int, SIZE> a2 {100, 2, 8, 1, 50, 3, 8, 8, 9, 10};
    cout << "\n\na2 contains: ";
    copy(a2.cbegin(), a2.cend(), output);

    // count
    auto result = count(a2.cbegin(), a2.cend(), 8);
    cout << "\nNumber of element matching 8: " << result;

    // count_if
    result = count_if(a2.cbegin(), a2.cend(), [](auto x){return x > 9;});
    cout << "\nNumber of element greater than 9: " << result;

    // locate min
    cout << "\n\nMinimum element in a2 is: " << *(min_element(a2.cbegin(), a2.cend()));

    // locate max
    cout << "\n\nMaximun element in a2 is: " << *(max_element(a2.cbegin(), a2.cend()));

    // locate min and max
    auto minAndMax = minmax_element(a2.cbegin(), a2.cend());
    cout << "\n\nThe min and max in a2 are " << *minAndMax.first << " and " << *minAndMax.second << ", respectively";

    // sum
    cout << "\n\nThe total of the element in a1 is: " << accumulate(a1.cbegin(), a1.cend(), 0);

    array<int, SIZE> cubes; // 初始化
    cout << "\n\ninitial cubes:\n";
    copy(cubes.cbegin(), cubes.cend(), output);
    // transform
    transform(a1.cbegin(), a1.cend(), cubes.begin(), [](auto x){return x*x*x;});
    cout << "\nThe cube of every integer in a1 is:\n";
    copy(cubes.cbegin(), cubes.cend(), output);
    cout << endl;
}


6. Standard Library search and sort algorithm

// Standard Library search and sort algorithm

#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>
using namespace std;

int main() {
    const size_t SIZE{10};
    array<int, SIZE> a{10, 2, 17, 5, 16, 8, 13, 11, 20, 7};
    ostream_iterator<int> output{cout, " "};
    cout << "array a contains: ";
    copy(a.cbegin(), a.cend(), output);

    // locate first occurance of 16 in a
    auto location = find(a.cbegin(), a.cend(), 16);
    if (location != a.cend()) {
        cout << "\n\nFound 16 at location " << (location - a.cbegin());
    }
    else{
        cout << "\n\n16 not found";
    }

    location = find(a.cbegin(), a.cend(), 100);
    if (location != a.cend()) {
        cout << "\n\nFound 100 at location " << (location - a.cbegin());
    }
    else{
        cout << "\n\n100 not found";
    }

    // find_if  &  store lambda -> varialbe
    auto isGraterThan10{[](auto x){return x > 10;}};
    location = find_if(a.cbegin(), a.cend(), isGraterThan10);
    if (location != a.cend()) {
        cout << "\n\nThe first value greater then 10 is " << *location << "\nFound at location " << (location - a.cbegin());
    }
    else{
        cout << "\n\nNo values grater than 10 wer found";
    }

    // sort element of a
    sort(a.begin(), a.end());
    cout << "\n\narray a after sort: ";
    copy(a.cbegin(), a.cend(), output);

    // binary_search to check whether 13 exists in a
    if (binary_search(a.cbegin(), a.cend(), 13)){
        cout << "\n\n13 was found in a";
    }
    else{
        cout << "\n\n13 was not found in a";
    }

    // use binary_search to check whether 100 exists
    if (binary_search(a.cbegin(), a.cend(), 100)){
        cout << "\n\n100 was found in a";
    }
    else{
        cout << "\n\n100 was not found in a";
    }

    // all_of
    if (all_of(a.cbegin(), a.cend(), isGraterThan10)) {
        cout << "\n\nAll the element in a are greater than 10";
    }
    else{
        cout << "\n\nSome elements in a are not greater than 10";
    }

    // any_of
    if (any_of(a.cbegin(), a.cend(), isGraterThan10)) {
        cout << "\n\nsome the element in a are greater than 10";
    }
    else{
        cout << "\n\nNone elements in a are not greater than 10";
    }

    // none_of
    if (none_of(a.cbegin(), a.cend(), isGraterThan10)) {
        cout << "\n\nNone the element in a are greater than 10";
    }
    else{
        cout << "\n\nsome elements in a are not greater than 10";
    }

    // find_if_not
    location = find_if_not(a.cbegin(), a.cend(), isGraterThan10);
    if (location != a.cend()) {
        cout << "\n\nThe first value not greater than 10 is " << *location <<
        "\nfound at location " << (location - a.cbegin());
    }
    else {
        cout << "\n\nOnly values greater than 10 were found";
    }
    cout << endl;
}


7. Algorithm swap, iter_swap and swap_ranges

// Algorithm swap, iter_swap and swap_ranges
#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
using namespace std;

int main() {
    const size_t SIZE{10};
    array<int, SIZE> a{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    ostream_iterator<int> output{cout, " "};

    cout << "Array a contains: \n";
    copy(a.cbegin(), a.cend(), output);

    // swap
    swap(a[0], a[1]);
    cout << "\nArray a after swapping a[0] and a[1] using swap:\n";
    copy(a.cbegin(), a.cend(), output);

    // iter_swap (iterator方式)
    iter_swap(a.begin()+2, a.begin()+3);
    cout << "\nArray a after swapping a[0] and a[1] using iter_swap:\n";
    copy(a.cbegin(), a.cend(), output);

    // swap_range (注意overlap啥的)
    swap_ranges(a.begin(), a.begin()+5, a.begin()+5);
    cout << "\n\nArray a after swapping the first five elements "  << "with the last five elements:\n ";
    copy(a.cbegin(), a.cend(), output);
    cout << endl;
}

 

 


8. Algorithms copy_backward, merge, unique and reverse

// Algorithms copy_backward, merge, unique and reverse.
#include <iostream>
#include <algorithm>
#include <array>
#include <iterator>
using namespace std;

int main() {
    const size_t SIZE{5};
    array<int, SIZE> a1{1, 3, 5,  7, 9};
    array<int, SIZE> a2{2, 4, 5, 7, 9};
    ostream_iterator<int> output{cout, " "};

    cout << "array a1 contains: ";
    copy(a1.cbegin(), a1.cend(), output);
    cout << "\narray a2 contains: ";
    copy(a2.cbegin(), a2.cend(), output);

    array<int, SIZE> results{};
    cout << "\n\ninit result: ";
    copy(results.cbegin(), results.cend(), output);

    // copy_backward, 算法反正来copy,但是结果顺序还是原来的
    copy_backward(a1.cbegin(), a1.cend(), results.end());
    cout << "\n\nAfter copy_backward, results contains: ";
    copy(results.cbegin(), results.cend(), output);

    // merge, 合并两个有序的
    array<int, SIZE + SIZE> results2{};
    cout << "\n\ninit result2: ";
    copy(results2.cbegin(), results2.cend(), output);

    merge(a1.cbegin(), a1.cend(), a2.cbegin(), a2.cend(), results2.begin());
    cout << "\n\nAfter merge of a1 and a2, results2 contains: ";
    copy(results2.cbegin(), results2.cend(), output);

    // unique
    auto endLocation = unique(results2.begin(), results2.end());
    cout << "\n\nAfter unique results2 contains: ";
    copy(results2.begin(), endLocation, output);

    // reverse
    cout << "\n\narray a1 after reverse: ";
    reverse(a1.begin(), a1.end());
    copy(a1.cbegin(), a1.cend(), output);
    cout << endl;
}


9. Algorithms inplace_merge, reverse_copy and unique_copy

//  Algorithms inplace_merge, reverse_copy and unique_copy.
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <iterator>
using namespace std;

int main() {
    const int SIZE{10};
    array<int, SIZE> a1{1, 3, 5, 7, 9, 1, 3, 5, 7, 9};
    ostream_iterator<int> output{cout, " "};

    cout << "array a1 contains: ";
    copy(a1.cbegin(), a1.cend(), output);

    // inplace_merge
    inplace_merge(a1.begin(), a1.begin()+5, a1.end());
    cout << "\nAfter inplace_merge, a1 contains: ";
    copy(a1.cbegin(), a1.cend(), output);

    // unique_copy
    vector<int> results1;
    unique_copy(a1.cbegin(), a1.cend(), back_inserter(results1));
    cout << "\nAfter unique_copy results1 contains: ";
    copy(results1.cbegin(), results1.cend(), output);

    // reverse_copy
    vector<int> results2;
    reverse_copy(a1.cbegin(), a1.cend(), back_inserter(results2));
    cout << "\nAfter reverse_copy, results2 contains: ";
    copy(results2.cbegin(), results2.cend(), output);
    cout << endl;
}


10. includes, set_difference, set_intersection, set_symmetric_difference and set_union

// includes, set_difference, set_intersection, set_symmetric_difference and set_union

#include <iostream>
#include <array>
#include <algorithm>
#include <iterator>
using namespace std;


int main() {
    const size_t SIZE1{10}, SIZE2{5}, SIZE3{20};
    array<int, SIZE1> a1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    array<int, SIZE2> a2{4, 5, 6, 7, 8};
    array<int, SIZE3> a3{4, 5, 6, 11, 15};
    ostream_iterator<int> output{cout , " "};

    cout << "a1 contains: ";
    copy(a1.cbegin(), a1.cend(), output);
    cout << "\na2 contains: ";
    copy(a2.cbegin(), a2.cend(), output);
    cout << "\na3 contains: ";
    copy(a3.cbegin(), a3.cend(), output);

    // includes
    if (includes(a1.cbegin(), a1.cend(), a2.cbegin(), a2.cend())){
        cout << "\n\na1 includes a2";
    }
    else {
        cout << "\n\na1 does not includes a2";
    }

    if (includes(a1.cbegin(), a1.cend(), a3.cbegin(), a3.cend())){
        cout << "\n\na1 includes a3";
    }
    else {
        cout << "\n\na1 does not includes a3";
    }

    // set_difference - 差集
    array<int, SIZE1> difference{};
    auto result1 = set_difference(a1.cbegin(), a1.cend(), a2.cbegin(), a2.cend(), difference.begin());
    cout << "\n\nset_difference of a1 and a2 is :";
    copy(difference.begin(), result1, output);

    // 并集
    array<int, SIZE1> intersection{};
    auto result2 = set_intersection(a1.cbegin(), a1.cend(), a2.cbegin(), a2.cend(), intersection.begin());
    cout << "\n\nset_intersection of a1 and a2 is :";
    copy(intersection.begin(), result2, output);

    //

    array<int, SIZE1 + SIZE2> symmetric_difference{};
    auto result3 = set_symmetric_difference(a1.cbegin(), a1.cend(), a3.cbegin(), a3.cend(), symmetric_difference.begin());
    cout << "J\n\nset_symmetric_difference of a1 and a3 is: ";
    copy(symmetric_difference.begin(), result3, output);

    //
    array<int, SIZE3> unionSet;
    auto result4 = set_union(a1.cbegin(), a1.cend(), a3.cbegin(), a3.cend(), unionSet.begin());
    cout << "\n\nset_union of a1 and a3 is : ";
    copy(unionSet.begin(), result4, output);
    cout << endl;

}


 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值