博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【C++】C++11 STL算法(八):对未初始化内存的操作(Operations on uninitialized memory)、C库(C library)
阅读量:4262 次
发布时间:2019-05-26

本文共 3938 字,大约阅读时间需要 13 分钟。

对未初始化内存的操作(Operations on uninitialized memory)

一、uninitialized_copy

1、原型:
template< class InputIt, class ForwardIt >ForwardIt uninitialized_copy( InputIt first, InputIt last, ForwardIt d_first );
2、说明:

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

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
(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_

二、uninitialized_fill

1、原型:
template< class ForwardIt, class T >void uninitialized_fill( ForwardIt first, ForwardIt last, const T& value );
2、说明:

以指定值value填充。

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

Output:

ExampleExampleExampleExample

三、uninitialized_fill_n

1、原型:
template< class ForwardIt, class Size, class T >ForwardIt uninitialized_fill_n( ForwardIt first, Size count, const T& value );
2、说明:

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

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

Output:

ExampleExampleExampleExample

C库(C library)

一、qsort

1、原型:
void qsort( void *ptr, std::size_t count, std::size_t size, *comp );
2、说明:

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

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
(a); int arg2 = *static_cast
(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

二、bsearch

1、原型:
void* bsearch( const void* key, const void* ptr, std::size_t count,           std::size_t size, *comp );
2、说明:

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

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 3value 9 not found

转载地址:http://udmei.baihongyu.com/

你可能感兴趣的文章
java基础入门(一)
查看>>
Java基础入门(二)
查看>>
Java基础入门(三)
查看>>
Java基础入门(四)
查看>>
Java基础入门(完结篇)
查看>>
Java进阶之面向对象(一)——继承
查看>>
Java进阶之自定义ArrayList&斗地主发牌案例
查看>>
JavaWeb之Ajax&json
查看>>
BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用
查看>>
逻辑思维测试题
查看>>
如何用Easy CHM制作CHM格式电子书(帮助文档)
查看>>
为什么学习python
查看>>
华为进不了美国,并不是贸易保护这么简单
查看>>
markdown文件的基本常用编写语法(图文并茂)
查看>>
java变量简介
查看>>
Shell十分钟入门
查看>>
nginx 配置 upstream backup 报错
查看>>
Linux执行 wget命令:提示command not found的两种解决方法
查看>>
openssl实现md5加rsa签名
查看>>
史上最全的前端学习路线图,干货满满
查看>>