C++ C++;字符串和字符串文字比较

C++ C++;字符串和字符串文字比较,c++,string,comparison,C++,String,Comparison,因此,我试图简单地做一个std::string==“string literal”,除了用 std::string str(strCreateFrom, 0, strCreateFrom.find(' ')); 然后查找返回值string::npo现在这两个值都包含字符串“submit”但是==返回false,现在我已经缩小了范围,将其缩小到大小“不同”的事实,即使它们实际上不一样str.size()为7,strlen(“提交”)为6。这就是为什么==失败的原因,我想是的,但我不明白为什么。。

因此,我试图简单地做一个
std::string==“string literal”
,除了用

std::string str(strCreateFrom, 0, strCreateFrom.find(' '));
然后查找返回值
string::npo
现在这两个值都包含字符串
“submit”
但是
==
返回false,现在我已经缩小了范围,将其缩小到大小“不同”的事实,即使它们实际上不一样
str.size()
为7,
strlen(“提交”)
为6。这就是为什么
==
失败的原因,我想是的,但我不明白为什么。。。它是否应该检查dif的最后一个字符是否为
\0
,就像在这种情况下一样

我是否可以绕过这个问题,而不必使用compare并指定要比较或更改字符串的长度

编辑:

编辑:

关于我为什么感到困惑的更多说明,我阅读了basic_string.h标题,看到了以下内容:

/**
   *  @brief  Compare to a C string.
   *  @param s  C string to compare against.
   *  @return  Integer < 0, 0, or > 0.
   *
   *  Returns an integer < 0 if this string is ordered before @a s, 0 if
   *  their values are equivalent, or > 0 if this string is ordered after
   *  @a s.  Determines the effective length rlen of the strings to
   *  compare as the smallest of size() and the length of a string
   *  constructed from @a s.  The function then compares the two strings
   *  by calling traits::compare(data(),s,rlen).  If the result of the
   *  comparison is nonzero returns it, otherwise the shorter one is
   *  ordered first.
  */
  int
  compare(const _CharT* __s) const;
/**
*@与C字符串进行简短比较。
*@param s C字符串进行比较。
*@返回整数<0、0或>0。
*
*如果此字符串在@a s之前排序,则返回一个小于0的整数,如果
*它们的值相等,如果此字符串在
*@a s。确定要删除的字符串的有效长度rlen
*比较大小()和字符串长度中的最小值
*由@a s构成。然后,该函数比较这两个字符串
*通过调用traits::compare(data(),s,rlen)。如果
*比较为非零时返回,否则较短的为
*先点。
*/
int
比较(常数图*)常数;

这是从运算符==调用的,因此我试图找出为什么大小dif很重要。

我不太理解您的问题,可能需要更多详细信息,但您可以使用c比较,它不应该有空终止计数的问题。 您可以使用:

bool same = (0 == strcmp(strLiteral, stdTypeString.c_str());
strncmp还可用于仅比较字符数组中给定数量的字符

或者尝试修复stdstring的创建

您的未解析std::字符串已损坏。它已经在字符串中包含了额外的null,所以您应该看看它是如何创建的。 正如我前面提到的,mystring[mystring.size()-1]是最后一个字符,而不是终止null,因此如果在输出中看到“\0”,则表示null被视为字符串的一部分

尝试追溯已解析的输入,并确保mystring[mystring.size()-1]不是“\0”

要回答您的尺码差异问题: 这两个字符串不相同,文本较短,并且没有空值

  • std::string->c_str()的内存长度=7,内存大小=8
  • 文字[S,u,b,m,i,t,\0]长度为6的内存,内存大小为7
Compare在到达文本中的终止null时停止比较,但它使用存储的std::string的大小,该字符串为7。如果文本终止于6,但std为7,则表示std更大

我认为,如果您执行以下操作,它将返回相同的字符串(因为它将创建一个std字符串,其右侧也有一个额外的null):


std::cout您确定没有任何不可打印的字符吗?请提供一份带有填充字符串的SSCCE,以演示问题。我将编辑我的问题以证明
(int)“提交”[i]
???“那是什么?”比尔兹,这是完全合法的。文本会衰减为一个指针,用于调用索引运算符。您使用的编译器是什么?我在VisualStudio2012中编写了您的简单程序,创建字符串str(“submit”);字符串指令(str,0 str.find(“”));然后做上面的程序,没有看到这个问题。因为OP更新了这个问题,所以两者应该是等价的。字符串中只有一个空值。噢,std::字符串中有一个额外的0,该值包含在大小中,该值为sumbit\0\0,而c值为submit\0。std字符串的创建方式及其包含的额外null有问题。即使如此,我上面的解决方案仍然有效。哦,你说得对。我从来没有想到0在那里意味着字符串太长了一个字符。如果你最终得到了这样的结果,你所需要的只是第一个0,一个(不是最大效率的)可能性是
str=str.c_str()。如果find确实找到了一个空间,那么您对创建的修复就不起作用了。从技术上讲,字符串不是仍然只是提交\0吗?我几乎可以肯定,它不会在结尾添加另一个\0it@madnut无论如何,我不必使该构造函数的默认值为
npos
/**
   *  @brief  Compare to a C string.
   *  @param s  C string to compare against.
   *  @return  Integer < 0, 0, or > 0.
   *
   *  Returns an integer < 0 if this string is ordered before @a s, 0 if
   *  their values are equivalent, or > 0 if this string is ordered after
   *  @a s.  Determines the effective length rlen of the strings to
   *  compare as the smallest of size() and the length of a string
   *  constructed from @a s.  The function then compares the two strings
   *  by calling traits::compare(data(),s,rlen).  If the result of the
   *  comparison is nonzero returns it, otherwise the shorter one is
   *  ordered first.
  */
  int
  compare(const _CharT* __s) const;
bool same = (0 == strcmp(strLiteral, stdTypeString.c_str());
std::cout << (instruction == str("submit", _countof("submit"))) << std::endl;