从整数数组初始化向量时出错

Error during the Initializing a vector from an arraty of ints

目前正在阅读 Stanley B. Lippman、Josée Lajoie、Barbara E. Moo 合着的《C Primer 5th Addition》一书。在阅读这本书时,我遇到了一行,

we can use an array to initialize a vector. To do so, we specify the address of the first element and one past the last element that we wish to copy

用于解释上述语句的代码部分是,

int int_arr[] = {0, 1, 2, 3, 4, 5}; vector<int> ivec(begin(int_arr), end(int_arr));

在我的编译器中使用以下代码后,它会在开始和结束部分抛出错误。 错误如下,

rough1.cpp: In function 'int main()':
rough1.cpp:12:22: error: 'begin' was not declared in this scope
 vector<int> ivec(begin(int_arr), end(int_arr));
                  ^~~~~

我的问题是,

According to the book, the following code must not throw an error, but upon using it does throws an error. Is there something wrong with my compiler? Or, is it an older technique that is no longer used? What should I exactly do to initialize a vector from an array of ints?

beginend 在命名空间 std 中定义。所以你必须在那个命名空间的范围内。这就是我们写 std:: 时所做的。

为了解决你的问题,

begin 替换为 std::begin。同样将 end 替换为 std::end