#include <iostream>#include <vector>#include <string>using namespace std;int main(){vector<string> msg {"Hello", "C++", "World", "!"};for (const string & word : msg){cout << word << " ";}cout << endl;}
如果要对以上代码进行编译的话,推荐使用g++编译器,g++ hello.cpp 这个命令可以把这个源文件进行编译生成一个可执行文件。
代码第9行,对msg变量进行初始化的时候,使用了C++11 标准。
g++默认的标准要比这个低,所以单纯使用g++ hello.cpp 可能会编译出错(我没报错)。若使编译不报错,最好要指定C++标准,比如使用g++ hello.cpp --std=c++11 ,应该会编译成功,并且生成可执行文件,默认的可执行文件名为 a.out。如果想要改变可执行文件的名称,可以采用如下命令:g++ hello.cpp --std=c+=11 -o hello ,这时就会生成名为hello的可执行文件。
