c++ - 错误 : ‘i’ does not name a type with auto

标签 c++ vector

<分区>

我是C++新手,这是我的程序

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <functional>  

int main(){

static const double arr[] = {16.0,2.2,77.5,29.0,24.0};
std::vector<double> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );

std::transform(vec.begin(), vec.end(), vec.begin(),bind2nd(std::minus<double>(), 3.0)); 

for (auto i = vec.begin(); i != vec.end(); ++i)
    std::cout << *i << ' ';

}

当我尝试使用 g++ p1.cpp -o p1 进行编译时

p1.cpp: In function ‘int main()’:
p1.cpp:14:11: error: ‘i’ does not name a type
 for (auto i = vec.begin(); i != vec.end(); ++i)

我知道有更多方法可以打印 vector ,但为什么这不起作用?

最佳答案

您应该尝试使用 -std=c++11 。 之后工作正常。

./p1
13 -0.8 74.5 26 21 

关于c++ - 错误 : ‘i’ does not name a type with auto,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041007/

相关文章:

c++ - 从 vector 返回 bool&

C++ 迭代器 vector 结构

c++ - 我们可以使用自定义分配器创建法线 vector<bool> 吗?

c++ - 使用 const_cast 实现移动构造函数

c++ - 增加堆栈大小以使用 alloca()?

c++ - 如何告诉 ld 在哪里可以找到 libc

C++ vector 中的最小元素

c++ - 错误推回对 const vector 元素的引用

c++ - 是否有将 SHA1 散列表示为 C 字符串的标准方法,我如何转换成它?

c++ - 什么时候应该使用 C++ 私有(private)继承?