template< class InputIt, class ForwardIt >
ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );
将对象范围复制到未初始化的内存区域
#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_
template< class ForwardIt, class T >
void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );
以指定值value填充。
#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
template< class ForwardIt, class Size, class T >
ForwardIt uninitialized_fill_n( ForwardIt first, Size count, const T& value );
以指定值value填充从first开始count个数据。
#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
void qsort( void *ptr, std::size_t count, std::size_t size, *comp );
按升序对ptr的数组进行排列
#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
void* bsearch( const void* key, const void* ptr, std::size_t count,
std::size_t size, *comp );
二分查找:在ptr数组中找到key
#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