C++之begin()和end()分析(C艹11)
来源:互联网 发布:pr淘宝主图视频比特率 编辑:程序博客网 时间:2024/06/05 23:48
被一网友问到过个这个问题,看了下源码,差不多应该明白了
例子:
int a[] = {1,3,5,7,9};std::for_each(std::begin<int>(a), std::end<int>(a), [&](int n) {cout << n;});//这样会把13579输出来。
1,先看一下begin和end这源码
template<class _Tp> constexpr const _Tp* begin(initializer_list<_Tp> __ils) noexcept { return __ils.begin(); } /** * @brief Return an iterator pointing to one past the last element * of the initializer_list. * @param __ils Initializer list. */ template<class _Tp> constexpr const _Tp* end(initializer_list<_Tp> __ils) noexcept { return __ils.end(); }
2.嘿!原来是调用了一个initializer_list这个类模版的begin和end方法,我们可以继续查看
// The compiler can call a private constructor.这个编译器主动调用的构造函数,开发者调用不了 constexpr initializer_list(const_iterator __a, size_type __l) : _M_array(__a), _M_len(__l) { } //size()方法直接返回_M_len,也就是编译器确定的长度 constexpr size_type size() const noexcept { return _M_len; } //begin()方法直接返回_M_array,首地址了 constexpr const_iterator begin() const noexcept { return _M_array; } //end()方法返回的时首地址加上数组长度,即尾端 constexpr const_iterator end() const noexcept { return begin() + size(); }
这下应该明白了,是编译器的锅。
更多文章:http://blog.csdn.net/what951006?viewmode=list
powered by:小乌龟在大乌龟背上~
阅读全文
0 0
- C++之begin()和end()分析(C艹11)
- 收集的C标签下拉框,感觉比较实用(c标签的begin和end)
- C++begin和end运算符的返回迭代器的类型如何判断
- perl BEGIN和END用法
- awk的BEGIN和END
- awk的BEGIN和END
- 非成员begin()和end()
- perl BEGIN 和END块
- 标准库函数begin和end
- 迭代器的begin,end和指针的begin,end
- 语法甜点11:非成员的begin和end
- C++11标准库中的begin()和end()函数
- BEGIN...END
- begin-end
- Begin ... End
- C++STL中vector容器 begin()与end()函数、front()与back()的用法
- C++STL中vector容器 begin()与end()函数、front()与back()的用法
- C++STL中vector容器 begin()与end()函数、front()与back()的用法
- 在jquery 1.11.1版本中,如何实现jquery 1.9以下版本里面的toggle功能?
- 内网通过nginx请求外网
- Spring MVC 多语言
- ubuntu16.04编译android5.1环境配置
- 多视角立体影像匹配三维重建---- visualSFM的使用方法
- C++之begin()和end()分析(C艹11)
- VS2010 手动为控件添加事件处理函数
- STL源码剖析: 第5章 关联式容器
- Kotlin基础教程-内联函数
- Java应用线上问题排查的常用工具和方法
- AlgoSec修改天融信取配置命令
- 关于spring中bean的三种配置方式的比较
- 有return的情况下try catch finally的执行顺序
- C++11之default和delete