C ++ begin()和end()

时间:2015-06-30 13:05:14

标签: c++ gcc g++

我正在阅读C++ Primer 5th Edition本书。不时,作者使用函数beginend

例如:

int ia[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

int (*p)[4] = begin(ia);

然而,我收到错误:

error: ‘begin’ was not declared in this scope

我正在运行gcc 4.9.2,并使用以下命令进行编译:

g++ -std=c++11 main.cpp

3 个答案:

答案 0 :(得分:11)

作者可能有using namespace std;using std::begin;这样的声明。您需要输入std::begin而不需要其中一个。您可能还需要#include<iterator>

答案 1 :(得分:4)

您需要将其包装在std范围内,函数为std::begin

int (*p)[3] = std::begin(ia);

可能在文本中,作者在代码顶部有一个using指令。

using namespace std;

我希望discourage you使用后一种方法。

答案 2 :(得分:2)

您必须包含标头<iterator>,其中声明了函数begin

#include <iterator>

如果你没有包含using指令

using namespace std;

然后您必须使用begin

的限定名称
int ( *p )[4] = std::begin( ia );

auto p = std::begin( ia );

事实陈述

int ( *p )[4] = std::begin( ia );

相当于

int ( *p )[4] = ia;

和表达式std::end( ia )相当于ia + 3

相关问题