C++ 向量字符串推断为什么类型?
我越来越熟悉矢量的使用(阅读),它显示了以下代码作为示例:C++ 向量字符串推断为什么类型?,c++,string,vector,type-deduction,C++,String,Vector,Type Deduction,我越来越熟悉矢量的使用(阅读),它显示了以下代码作为示例: // as with std::array, the type can be omitted since C++17 std::vector array4 { 9, 7, 5, 3, 1 }; // deduced to std::vector<int> (即标准::字符串) 不,字符串文字与类std::string无关。从技术上讲,如果vector对此有专门的推导指南,则可能会出现这种情况,但事实并非如此 …煤焦* 不,
// as with std::array, the type can be omitted since C++17
std::vector array4 { 9, 7, 5, 3, 1 }; // deduced to std::vector<int>
(即标准::字符串)
不,字符串文字与类std::string
无关。从技术上讲,如果vector对此有专门的推导指南,则可能会出现这种情况,但事实并非如此
…煤焦*
不,字符串文字是常量
包含的类型将被推断为
const char*
只是想添加字符串文本的类型const char[N]
,其中N是包括终止符在内的字符数,向量的列表初始值设定项构造函数接受值而不是引用,因此文本衰减到指针常量char*
如果需要std::strings的向量,可以使用literal运算符“s
:
std::vector strArray {"Hello"s, "world"s, "!!!"s};
与
std::vector<std::string> strArray {"Hello", "world", "!!!"};
std::vector strArray{“你好”,“世界”,“!!!”;
std::vector<std::string> strArray {"Hello", "world", "!!!"};