gcc支持cbegin和cend方法
gcc support for cbegin and cend method
哪个版本的gcc编译器支持容器的免费const_iterator
方法,例如:cbegin
、cend
、crbegin
、crend
。我使用的gcc 4.6.1具有由-std=c++0x
标志启用的c++0x功能,但这些方法在我的代码中无法解析。
不幸的是,标准中没有免费的函数cbegin
/cend
,也没有任何反向版本。不过,您总是可以使用强制转换来获得常量迭代器:
auto it = std::begin(static_cast<T const &>(x));
使用<type_traits>
中的std::add_const
,如果你非常需要的话,你甚至应该能够安装一些相当通用的东西。
容器成员函数cbegin
/crbegin
等都是C++11的一部分,GCC已经支持了一段时间;很可能是从4.3开始的(当C++11支持刚开始的时候)。GCC 4.6明确支持这些;如果您遇到问题,请发布麻烦的代码。
我的实验表明,默认情况下"cbegin
"不可用(来自类似的<vector>
<map>
STL容器)。除非指定--std=c++0x
或--std=c++11
以下是我对GCC 4.8的经验:
http://ascii.io/a/4539
如果你的编译器不能支持那些--std
开关(就像我在一些跨平台工具链上所做的那样),你可以只使用这样的东西:
for (std::vector<string>::const_iterator it = vec.begin(), ite = vec.end();
it != ite; ++it) {
/* ... ... */
}
这适用于不支持C++11的"自动"关键字功能的GCC版本。
适用于我(GCC 4.6.0):
#include <vector>
int main() {
std::vector<int> vec;
auto it = vec.cbegin();
// int& val = *it; // gives compiler error, as expected
}
成员函数也出现在stl_vector.h
中。
你能添加自己的吗?
template< class C >
auto cbegin( C& c ) -> decltype(c.cbegin());
template< class C >
auto cbegin( const C& c ) -> decltype(c.cbegin())
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 为什么 std::span 缺少 cbegin 和 cend 方法?
- gcc支持cbegin和cend方法