Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将字符推送到字符串向量时出现意外行为_C++_String_Vector_C++14_Stdvector - Fatal编程技术网

C++ 将字符推送到字符串向量时出现意外行为

C++ 将字符推送到字符串向量时出现意外行为,c++,string,vector,c++14,stdvector,C++,String,Vector,C++14,Stdvector,我试图先将一个字符转换成一个字符串,然后将其插入字符串向量中,但案例1不起作用,但案例2起作用。有人能解释一下这种行为吗。有没有办法将字符直接插入向量 // vector of strings vector lt; std::string gt; out; // EDIT - stack overflow is not letting me post "less than" sign so using lt; and gt; instead // a char

我试图先将一个字符转换成一个字符串,然后将其插入字符串向量中,但案例1不起作用,但案例2起作用。有人能解释一下这种行为吗。有没有办法将字符直接插入向量

    // vector of strings
    vector lt; std::string gt; out; 
    // EDIT - stack overflow is not letting me post "less than" sign so using lt; and gt; instead

    // a character
    char ch;

    // assign some value to ch and push to vector

    // CASE 1: don't work
    out.push_back("" + ch);

    // CASE 2: works
    string str = "";
    out.push_back(str + ch);

PS-我正在使用C++14

此代码编译并运行:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main() {
   // vector of strings

   vector<string> out;

   // a character
   char ch;

   // assign some value to ch and push to vector

   // Case 0: works because a const char * can be coerced into a string
   out.push_back("");

   // Case 1: Does not work 
   // you can't add a const char * and a char.
   // Some compilers coerce the char into an int and add that to the pointer
   // producing undefined behavior (access to un-allocated memory)
   // out.push_back("" + ' ');

   // CASE 1a: Works, but I'm 'cheating' by 
   // explicitly constructing a string.
   out.push_back(string("") + ch);

   // Case 1b: Works using a different string constructor
   // This is probably the best approach:
   out.push_back(string(1, ' '));

   // CASE 2: works
      string str = "";
      out.push_back(str + ch);

   std::cout << "out contains " << out.size() << " entries" << std::endl;

   return 0;
}
术语
并不表示
std::string
实例,而是
const char*
文本,因此,当您向其添加
ch
时,您正在执行指针算术,并且可能会收到编译器警告,提示如下:

警告:数组下标高于数组边界

要将
a
char
作为
std::string
推回
std::vector
只需执行以下操作:

std::vector<std::string> vec;
char ch = 'a';
vec.push_back(std::string()+ch); // construct empty std::string, add ch, push_back
std::向量向量向量机;
char ch='a';
向量推回(std::string()+ch);//构造空std::string,添加ch,推回

您的代码有问题。 以下是类型特征,它将帮助您检查名称是否为模板化类型:

/***Check if type is simple template***/
template <template<class...> class>
constexpr bool is_template_type()
{ return true; }

template <class>
constexpr bool is_template_type()
{ return false; }
导致编译失败,因为无法创建
类模板
的变量。必须指定
std::vector
应存储的
TYPE
。你的题目是:

字符串向量

因此,您应该创建
std::vector
变量


代码的另一个问题是:
“”+ch
。 你认为它有什么作用<代码>“
类型为
char const[1]
ch
类型为
char
char const[1]
可以隐式转换为
char const*
,因为您可以向指针添加整数值,所以它可以编译。但这不是你想要的

这是内存中的

`\0' ? ? ? ? ? ? ...
/\
ch
添加到
“”
时,结果如下:

//ch is 5 for example
`\0' ? ? ? ? ? ? ...
             /\
因此,您创建了带有一些未指定地址的
std::string
。实际上,在C++中访问数组超出界限是未定义的行为。 您应该使用
std::string()+ch
或用户W.F.在其对您的问题的评论中发布的字符串文字语法



另外,不要忘记初始化
ch
,因为使用未指定的值也是未定义的行为。

错误消息应该会告诉您它不起作用的原因。您是否尝试搜索错误?谷歌上有很多搜索结果。发布真正有效的代码。您有语法错误。也许您会对字符串文字感兴趣:@NathanOliver感谢您的响应。。没有错误消息,但向量为空..-4人?真的吗?作品期待两行:)好的观点。我会解决这个问题,但这并不是一个确切的答案。我不知道为什么stackoverflow没有显示字符串部分。
`\0' ? ? ? ? ? ? ...
/\
//ch is 5 for example
`\0' ? ? ? ? ? ? ...
             /\