初始化C+中的列表列表+; 我想在C++中初始化列表(如Python中的)。 假设列表是:['a','b',['c','d']] 新到C++,大部分都在Python中工作,所以不知道该怎么做。
初始化C+中的列表列表+; 我想在C++中初始化列表(如Python中的)。 假设列表是:['a','b',['c','d']] 新到C++,大部分都在Python中工作,所以不知道该怎么做。,c++,C++,std::vector<vector<string>> dp {{'a',b'}}; std::向量dp{{'a',b'}; 我试过这个,但似乎不起作用 no matching function for call to 'std::vector<std::vector<std::__cxx11::basic_string<char> > >::vector(<brace-enclosed initializer list>
std::vector<vector<string>> dp {{'a',b'}};
std::向量dp{{'a',b'};
我试过这个,但似乎不起作用
no matching function for call to 'std::vector<std::vector<std::__cxx11::basic_string<char> > >::vector(<brace-enclosed initializer list>)'|
调用“std::vector::vector()”时没有匹配的函数|
> p>它在C++中看起来相似,但是字符串文字应该被“代码>”<代码> > <代码> > /代码>(这是字符字符)包围。
你想要这样的东西吗
std::vector<std::vector<std::string>> dp = {
{"a", "b"},
{"c", "d"},
{"e","f"}
};
std::vector dp={
{“a”,“b”},
{“c”,“d”},
{“e”,“f”}
};
你需要显示你看到的错误。请花些时间阅读,尤其是。还有。最后,不要忘记如何创建一个。你的C++书对此有何评论?看起来你可以使用。哦,我使用了单引号。谢谢!单引号本身不是错误。例如,代码>矢量DP {{ A′,B },{'c','}};是OK。C++比Python具有更强的类型;元素初始化器的类型需要类似(“可转换”)。到容器元素类型。@MSalters是的,但OP在这种特定情况下需要格外小心。Char可以隐式转换为int,因此,如果您不小心混合了表示法,则它可能会出错而不会产生错误:vector dp{'c',“d}
编译愉快,并生成一个带有99'd'的向量(因为ascii表中的'c'=到99).
hello world
goodbye for now
std::vector<std::vector<std::string>> dp = {
{"a", "b"},
{"c", "d"},
{"e","f"}
};