C++ 开始()和结束()
C++ begin() and end()
我正在阅读 C++ Primer 5th Edition
这本书。偶尔,作者使用函数 begin
和 end
.
例如:
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
作者可能有using namespace std;
或using std::begin;
这样的声明。您需要输入 std::begin
而没有其中之一。您可能还需要 #include<iterator>
.
需要包裹在std
范围内,函数为std::begin
int (*p)[3] = std::begin(ia);
可能在文中,作者在代码顶部有一个 using
指令。
using namespace std;
我会 discourage you 使用后一种方法。
您必须在声明函数 begin
的地方包含 header <iterator>
#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
我正在阅读 C++ Primer 5th Edition
这本书。偶尔,作者使用函数 begin
和 end
.
例如:
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
作者可能有using namespace std;
或using std::begin;
这样的声明。您需要输入 std::begin
而没有其中之一。您可能还需要 #include<iterator>
.
需要包裹在std
范围内,函数为std::begin
int (*p)[3] = std::begin(ia);
可能在文中,作者在代码顶部有一个 using
指令。
using namespace std;
我会 discourage you 使用后一种方法。
您必须在声明函数 begin
的地方包含 header <iterator>
#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