2019-9月7号C++编程笔记
auto
定义变量时放在变量前,无需知道具体变量类型,系统可自行推断类型,减少编程工作,特别是在模板使用时,使用更方便。
1  | auto a=1;  | 
std::next
1  | // Defined in header <iterator>  | 
- 
Parameters:
- it – 迭代指针
 - n – 向前进的元素个数,缺省默认为1
 
 - 
Return value
The nth successor of iterator it.(返回it的第n个后继迭代指针)
 
1  | for (int i = 0; i < vals.size(); ++i) {  | 
std::prev
1  | // 使用方法与next相似,不同的是prev返回的是it的第n个前驱迭代指针,即返回指针是begin()和rbegin()区别  | 
std::advance
方法与prev和next相似,只是无返回指针
1  | template<class ForwardIt>  | 
const_cast
- 
去掉const属性:
const_cast<int*> (&num),常用,因为不能把一个const变量直接赋给一个非const变量,必须要转换。 - 
加上const属性:
const int* k = const_cast<const int*>(j),一般很少用,因为可以把一个非const变量直接赋给一个const变量,比如:const int* k = j; 
1  | binary_tree* tmp = const_cast<binary_tree*>(node);  | 
C++11对vector成员函数的扩展
emplace_back
std::vector::cbegin和std::vector::cend
这两个方法是与std::vector::begin和std::vector::end相对应的,从字面就能看出来,多了一个’c’,顾名思义就是const的意思。同理,std::vector::crbegin和std::vector::crend
std::vector::emplace
之前已经对emplace_back进行了讨论,其实还有一个方法叫emplace。
我想说的就是,emplace之于emplace_back就像insert之于push_back。如下使用
1  | <code class="hljs cpp">  | 
std::vector::data
Returns a direct pointer to the memory array used internally by the vector to store its owned elements.
1  | <code class="hljs cpp">#include <iostream>  | 
Author: Mrli
Link: https://nymrli.top/2019/11/26/2019-9月7号C-编程笔记/
Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.