STL标准库常用语法:numeric_limits、std::map::swap、std::array::data、std::move

1、利用numeric_limits将变量初始化为无效值

#include <limits> 
int val = std::numeric_limits<int>::max(); 

利用这个库函数将val变量初始化为int的最大值(最大值作为无效值)。

2、std::map::swap交换两个变量
函数原型:

void swap (map& x); 
std::map<char,int> foo,bar; foo.swap(bar); 

原理:只是交换指针,不是将每个map中的变量赋值到另一个map中,因此时间复杂度为常量。
参考链接:std::map::swap

3、std::swap交换两个变量 函数原型:

 template <class T> void swap (T& a, T& b) 
 { 
 	T c(std::move(a)); a=std::move(b); b=std::move(c); 
 } 

代码:

 #include <utility> std::swap(foo,bar); // 交换相同类型 

原理: 执行一个移动复制构造函数和两个移动赋值运算符函数,因此时间复杂度为常量。

原文:Performs exactly one construction and two assignments (although notice that each of these operations works on its own complexity).
参考链接:std::swap

4、std::array::data函数返回对象首个元素的地址

std::array<char,100> arr; 
char* ptr = arr.data(); // 返回首地址 

原理:由于array保存在连续区域,因此返回指针可以通过移位访问到每一个元素。
原理原文Returns a pointer to the first element in the array object. Because elements in the array are stored in contiguous storage locations, the pointer retrieved can be offset to access any element in the array.
参考链接:std::array::data

5、std::move返回右值引用
函数介绍:通过将对象传递给此函数,将获得引用该对象的rvalue。
代码:注意注释

#include <utility>  // std::move  
#include <iostream> // std::cout  
#include <vector>   // std::vector  
#include <string>   // std::string  
int main()  
{ 
     std::string foo = "foo-string";
     std::string bar = "bar-string";
     std::vector<std::string> myvector;
     myvector.push_back(foo);            // copies
     myvector.push_back(std::move(bar)); // moves      
     std::cout << "myvector contains:" << std::endl;      
     for (std::string &x : myvector) {
          std::cout << ' ' << x << std::endl;
     } 
     std::cout << bar <<std::endl; // 原对象内容已经被移动走,打印为空
     std::cout << "bar is empty" << endl;
     getchar();
     return 0;
}

输出:

myvector contains: 
foo-string 
bar-string
                    // string打印为空
bar is empty  

原理:通常,rvalues是无法通过解引用(&)来获得其地址的值,这可能是因为它们是文本,或者是因为它们是临时的(例如函数或显式构造函数调用返回的值)。标准库的许多组件实现了移动语义,允许直接转移对象资产和属性的所有权,而不必在参数为rvalue时复制它们。注意,尽管在标准库中表明:moved from对象保持在有效但未指定的状态,即在std::(move)操作之后,moved from对象的值只应被销毁或分配一个新值;否则访问它将产生一个未指定的值。

原理原文:Generally, rvalues are values whose address cannot be obtained by dereferencing them, either because they are literals or because they are temporary in nature (such as values returned by functions or explicit constructor calls).Many components of the standard library implement move semantics, allowing to transfer ownership of the assets and properties of an object directly without having to copy them when the argument is an rvalue. Although note that -in the standard library- moving implies that the moved-from object is left in a valid but unspecified state. Which means that, after such an operation, the value of the moved-from object should only be destroyed or assigned a new value; accessing it otherwise yields an unspecified value.

参考链接:std::move

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值