【C++】C++11 STL算法(八):对未初始化内存的操作(Operations on uninitialized memory)、C库(C library)

【C++】郭老二博文之:C++目录

1、uninitialized_copy

1.1、原型:

template< class InputIt, class ForwardIt >
ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );

1.2、说明:

将对象范围复制到未初始化的内存区域

1.3、官方demo

#include 
#include 
#include 
#include 
 
int main()
{
    const char *v[] = {"This", "is", "an", "example"};
 
    auto sz = std::size(v);
 
    if(void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) {
        try {
            auto first = static_cast<std::string*>(pbuf);
            auto last = std::uninitialized_copy(std::begin(v), std::end(v), first);
 
            for (auto it = first; it != last; ++it)
                std::cout << *it << '_';
 
            std::destroy(first, last);
        } catch(...) {}
        std::free(pbuf);
    }
}

Output:

This_is_an_example_

2、uninitialized_fill

2.1、原型:

template< class ForwardIt, class T >
void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );

2.2、说明:

以指定值value填充。

2.3、官方demo

#include 
#include 
#include 
#include 
#include 
 
int main()
{
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(4);
 
    std::uninitialized_fill(p, p+sz, "Example");
 
    for (std::string* i = p; i != p+sz; ++i) {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

Output:

Example
Example
Example
Example

3、uninitialized_fill_n

3.1、原型:

template< class ForwardIt, class Size, class T >
ForwardIt uninitialized_fill_n( ForwardIt first, Size count, const T& value );

3.2、说明:

以指定值value填充从first开始count个数据。

3.3、官方demo

#include 
#include 
#include 
#include 
#include 
 
int main()
{
    std::string* p;
    std::size_t sz;
    std::tie(p, sz) = std::get_temporary_buffer<std::string>(4);
    std::uninitialized_fill_n(p, sz, "Example");
 
    for (std::string* i = p; i != p+sz; ++i) {
        std::cout << *i << '\n';
        i->~basic_string<char>();
    }
    std::return_temporary_buffer(p);
}

Output:

Example
Example
Example
Example

4、qsort

4.1、原型:

void qsort( void *ptr, std::size_t count, std::size_t size, *comp );

4.2、说明:

按升序对ptr的数组进行排列

4.3、官方demo

#include 
#include 
#include 
 
int main()
{
    int a[] = {-2, 99, 0, -743, 2, INT_MIN, 4};
    constexpr std::size_t size = sizeof a / sizeof *a;
 
    std::qsort(a, size, sizeof *a, [](const void* a, const void* b){
        int arg1 = *static_cast<const int*>(a);
        int arg2 = *static_cast<const int*>(b);
 
        if(arg1 < arg2) return -1;
        if(arg1 > arg2) return 1;
        return 0;
    });
 
    for(int ai : a)
        std::cout << ai << ' ';
}

Output:

-2147483648 -743 -2 0 2 4 99

5、bsearch

5.1、原型:

void* bsearch( const void* key, const void* ptr, std::size_t count,
           std::size_t size, *comp );

5.2、说明:

二分查找:在ptr数组中找到key

5.3、官方demo

#include 
#include 
 
int compare(const void *ap, const void *bp)
{
    const int *a = (int *) ap;
    const int *b = (int *) bp;
    if(*a < *b)
        return -1;
    else if(*a > *b)
        return 1;
    else
        return 0;
}
 
int main(int argc, char **argv)
{
    const int ARR_SIZE = 8;
    int arr[ARR_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8 };
 
    int key1 = 4;
    int *p1 = (int *) std::bsearch(&key1, arr, ARR_SIZE, sizeof(arr[0]), compare);
    if(p1)
        std::cout << "value " << key1 << " found at position " << (p1 - arr) << '\n';
     else
        std::cout << "value " << key1 << " not found\n";
 
    int key2 = 9;
    int *p2 = (int *) std::bsearch(&key2, arr, ARR_SIZE, sizeof(arr[0]), compare);
    if(p2)
        std::cout << "value " << key2 << " found at position " << (p2 - arr) << '\n';
     else
        std::cout << "value " << key2 << " not found\n";
}

Output:

value 4 found at position 3
value 9 not found

你可能感兴趣的:(C++,c++,算法)