C++ 在类中初始化变量

C++ 在类中初始化变量,c++,string,vector,C++,String,Vector,我需要以这种方式初始化类中的向量 vector<string> test("hello","world"); vector<string> test = ("hello","world") 向量测试(“你好”,“世界”); 但当我这样做时,编译器会将其识别为一个函数,并给出如下错误 错误:字符串常量等前应有标识符 当我这样做的时候 vector<string> test("hello","world"); vector<string> t

我需要以这种方式初始化类中的向量

vector<string> test("hello","world");
vector<string> test = ("hello","world") 
向量测试(“你好”,“世界”); 但当我这样做时,编译器会将其识别为一个函数,并给出如下错误 错误:字符串常量等前应有标识符

当我这样做的时候

vector<string> test("hello","world");
vector<string> test = ("hello","world") 
向量测试=(“你好”,“世界”)
没关系。。有什么方法可以在
向量测试(“xx”)
中实现吗?

在std::vector中没有这样的构造函数可以让您这样初始化它。第二个例子的计算结果是
“world”
(这就是
操作符所做的),这就是向量中的结果

如果要在声明时初始化向量,请使用初始值设定项列表:

vector<string> test = {"hello", "world"};
向量测试={“你好”,“世界”}; 确保在C++-11模式下构建源代码以使其正常工作。如果没有与C++-11兼容的编译器,则必须在之后将值添加到向量:

vector<string> test;
test.push_back("hello");
test.push_back("world");
向量测试;
测试。推回(“你好”);
测试。推回(“世界”);

std::vector中没有这样的构造函数允许您这样初始化它。第二个例子的计算结果是
“world”
(这就是
操作符所做的),这就是向量中的结果

如果要在声明时初始化向量,请使用初始值设定项列表:

vector<string> test = {"hello", "world"};
向量测试={“你好”,“世界”}; 确保在C++-11模式下构建源代码以使其正常工作。如果没有与C++-11兼容的编译器,则必须在之后将值添加到向量:

vector<string> test;
test.push_back("hello");
test.push_back("world");
向量测试;
测试。推回(“你好”);
测试。推回(“世界”);

你确定,
向量测试=(“你好”,“世界”)
做你认为它做的事吗?你确定,
向量测试=(“你好”,“世界”)
做你认为它做的事吗?或者只是
字符串arr[]={“你好”,“世界”}
向量测试(arr,arr+2)string arr[]={“hello”,“world”}
向量测试(arr,arr+2)