C++中常用库函数

C++中常用库函数

1 字符串处理

1.1 字符串查找: strstr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string.h>
#include <stdio.h>
 
void find_str(char const* str, char const* substr) 
{
    char* pos = strstr(str, substr);
    if(pos) {
        printf("found the string '%s' in '%s' at position: %ld\n", substr, str, pos - str);
    } else {
        printf("the string '%s' was not found in '%s'\n", substr, str);
    }
}
 
int main(void) 
{
    char* str = "one two three";
    find_str(str, "two");
    find_str(str, "");
    find_str(str, "nine");
    find_str(str, "n");
 
    return 0;
}

2 algorithm库

2.1 排序

2.1.1 sort

Sorts the elements in the range [first, last) in ascending order. The order of equal elements is not guaranteed to be preserved.

说明:sort函数默认将数据按照升序排列。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// template< class RandomIt >
// void sort( RandomIt first, RandomIt last );

std::array<int, 10> s = {5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; 
// sort using the default operator<
std::sort(s.begin(), s.end());


    // 自定义比较函数,实现降序排序
    // sort using a custom function object
    struct {
        bool operator()(int a, int b)
        {   
            return a < b;
        }   
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    for (auto a : s) {
        std::cout << a << " ";
    }   
    std::cout << '\n';

    // sort using a lambda expression 
    std::sort(s.begin(), s.end(), [](int a, int b) {
        return b < a;   
    });
    for (auto a : s) {
        std::cout << a << " ";
    } 
    std::cout << '\n';

2.1.2 stable_sort()

说明:Sorts the elements in the range [first, last) in ascending order. The order of equal elements is guaranteed to be preserved.

2.1.3 partial_sort()

Rearranges elements such that the range [first, middle) contains the sorted middle - first smallest elements in the range [first, last).

The order of equal elements is not guaranteed to be preserved. The order of the remaining elements in the range [middle, last) is unspecified.

1
2
3
4
5
6
    std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};

    std::partial_sort(s.begin(), s.begin() + 3, s.end());
    for (int a : s) {
        std::cout << a << " ";
    } 

2.2 二分查找

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// template< class ForwardIt, class T>
// bool binary_search( ForwardIt first, ForwardIt last, const T& value ); (1)   
// template< class ForwardIt, class T, class Compare >
// bool binary_search( ForwardIt first, ForwardIt last, const T& value, Compare comp ); (2)

// 示例
    std::vector<int> haystack {1, 3, 4, 5, 9};
    std::vector<int> needles {1, 2, 3};
    for (auto needle : needles) {
        std::cout << "Searching for " << needle << '\n';
        if (std::binary_search(haystack.begin(), haystack.end(), needle)) {
            std::cout << "Found " << needle << '\n';
        } else {
            std::cout << "no dice!\n";
        }
    }

2.2.2 lower_bound()

功能说明:Returns an iterator pointing to the first element in the range [first, last) that is not less than (i.e. greater or equal to) value.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// template< class ForwardIt, class T >
// ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
// template< class ForwardIt, class T, class Compare >
// ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp ); 


std::vector<int> data = { 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };

auto lower = std::lower_bound(data.begin(), data.end(), 4);
auto upper = std::upper_bound(data.begin(), data.end(), 4);

std::copy(lower, upper, std::ostream_iterator<int>(std::cout, " ")); // 4 4 4

2.2.3 upper_bound()

Returns an iterator pointing to the first element in the range [first, last) that is greater than value.

2.3 revert()

接口定义:

1
template< class BidirIt > void reverse( BidirIt first, BidirIt last );

示例:

1
2
3
    std::vector<int> v({1,2,3});
    std::reverse(std::begin(v), std::end(v));
    std::cout << v[0] << v[1] << v[2] << '\n';

2.4 heap

2.4.1 make_heap()

接口定义:

1
2
template< class RandomIt> void make_heap(RandomIt first, RandomIt last);
template< class RandomIt, class Compare> void make_heap(RandomIt first, RandomIt last, Compare comp); 

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
std::vector<int> v = {3, 1, 4, 1, 5, 9}; 
std::make_heap(v.begin(), v.end());

// heap: 9 4 5 1 1 3 
std::cout << "heap:\t";
for (const auto &i : v) {
    std::cout << i << ' ';
}   

std::sort_heap(v.begin(), v.end());
// sorted: 1 1 3 4 5 9
std::cout << "\nsorted:\t";
for (const auto &i : v) {                                                   
    std::cout << i << ' ';
}   
std::cout << '\n';

2.4.2 sort_heap()

接口定义:

1
2
template< class RandomIt > void sort_heap( RandomIt first, RandomIt last);
template< class RandomIt, class Compare > void sort_heap( RandomIt first, RandomIt last, Compare comp); 

2.4.3 push_heap()

接口定义:

1
2
3
4
template< class RandomIt >
void push_heap( RandomIt first, RandomIt last );
template< class RandomIt, class Compare>
void push_heap(RandomIt first, RandomIt last, Compare comp ); 

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    std::vector<int> v { 3, 1, 4, 1, 5, 9 };

    std::make_heap(v.begin(), v.end());

    std::cout << "v: "; // v: 9 5 4 1 1 3 
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';

    v.push_back(6);

    std::cout << "before push_heap: "; // before push_heap: 9 5 4 1 1 3 6 
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';

    std::push_heap(v.begin(), v.end());

    std::cout << "after push_heap: "; // after push_heap:  9 5 6 1 1 3 4
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';

2.4.4 pop_heap()

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    std::vector<int> v { 3, 1, 4, 1, 5, 9 };
    std::make_heap(v.begin(), v.end());

    std::cout << "v: "; // v: 9 5 4 1 1 3 
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';

    std::pop_heap(v.begin(), v.end()); // moves the largest to the end

    std::cout << "after pop_heap: "; // after pop_heap: 5 3 4 1 1 9 
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';

    int largest = v.back();
    v.pop_back();  // actually removes the largest element
    std::cout << "largest element: " << largest << '\n'; // largest element: 9

    std::cout << "heap without largest: "; // heap without largest: 5 3 4 1 1
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';