简体   繁体   English

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

[英]C++ string and string literal comparison

So I am trying to simply do a std::string == "string-literal" which would work just fine, except that I am creating my string with 所以我试图简单地做一个std::string == "string-literal" ,除了我用

std::string str(strCreateFrom, 0, strCreateFrom.find(' '));

and find returns string::npos now both of these contain the string "submit" however == returns false, now I have narrowed this down to the fact that the sizes are "different" even though they really aren't. 并且find返回string::npos现在这两个都包含字符串"submit"但是==返回false,现在我将其缩小为大小实际上是“不同”的事实。 str.size() is 7 and strlen("submit") is 6. Is this why == is failing, I assume it is but I don't see why... shouldn't it check to see if the last char of dif is \\0 as is the case in this situation? str.size()为7而strlen("submit")为6。这就是为什么==失败,我认为是,但是我不明白为什么...不应该检查是否最后一个字符在这种情况下,dif的值为\\0

And is there anyway that I can get around this without having to using compare and specify the length to compare or change my string? 而且无论如何,我不必使用compare并指定长度来比较或更改我的字符串就可以解决此问题?

Edit: 编辑:

std::string instruction(unparsed, 0, unparsed.find(' '));
boost::algorithm::to_lower(instruction);

for(int i = 0; i < instruction.size(); i++){
    std::cout << "create from " << (int) unparsed[i] << std::endl;
    std::cout << "instruction " <<  (int) instruction[i] << std::endl;
    std::cout << "literal " << (int) "submit"[i] << std::endl;
}

std::cout << (instruction == "submit") << std::endl;

prints 版画

create from 83
instruction 115
literal 115
create from 117
instruction 117
literal 117
create from 98
instruction 98
literal 98
create from 77
instruction 109
literal 109
create from 105
instruction 105
literal 105
create from 116
instruction 116
literal 116
create from 0
instruction 0
literal 0

0

EDIT: 编辑:

For more clarification as to why I'm confused I read the basic_string.h header and saw this: 有关我为什么感到困惑的更多说明,我阅读了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;

Which is called from operator== so I am trying to find out why the size dif matters. 这是从operator ==调用的,所以我试图找出为什么尺寸dif很重要。

I didn't quite understand your question more details may be needed, but you can use the c compare which shouldn't have issues with null termination counting. 我不太了解您的问题,可能需要更多详细信息,但是您可以使用c比较,这应该不会出现空终止计数的问题。 You could use: 您可以使用:

bool same = (0 == strcmp(strLiteral, stdTypeString.c_str());

strncmp also can be used to compare only a given number of chars in a char array strncmp还可以用于比较char数组中给定数量的char

Or try to fix the creation of the stdstring 或者尝试修复stdstring的创建

Your unparsed std::string is already bad. 您未解析的std :: string已经很糟糕。 It already contains the extra null in the string, so what you should look at is how it is being created. 它已经在字符串中包含额外的null,因此您应该看看它是如何创建的。 Like I mentioned before mystring[mystring.size() -1] is the last character not the terminating null so if you see a '\\0' there like you do in your output it means the null is treated like part of the string. 就像我之前提到过的那样,mystring [mystring.size()-1]是最后一个字符而不是终止null,因此,如果像在输出中一样看到一个'\\ 0',则意味着null被视为字符串的一部分。

Try to trace back your parsed input and keep making sure that mystring[mystring.size() -1] is not '\\0'. 尝试回溯已解析的输入,并确保mystring [mystring.size()-1]不是'\\ 0'。

To answer your size diff question: The two strings are not the same the literal is shorter and doesn't have a null. 回答您的大小差异问题:两个字符串不同,字面量较短,并且没有null。

  • Memory of std::string->c_str() [S,u,b,m,i,t,\\0,\\0] length = 7, memory size = 8; std :: string-> c_str()[S,u,b,m,i,t,\\ 0,\\ 0]的内存长度= 7,内存大小= 8;
  • Memory of literal [S,u,b,m,i,t,\\0] length = 6, memory size = 7; 文字[S,u,b,m,i,t,\\ 0]的内存长度= 6,内存大小= 7;

Compare stops comparing when it reaches the the terminating null in the literal but it uses the stored size for the std::string which is 7 seeing that literal terminated at 6 but the std is size 7 it will say that std is larger. 比较在到达字面量的终止null时停止比较,但是它使用std :: string的存储大小,即7看到字面量以6结尾,但是std为7,则表示std较大。

I think if you do the following it will return that the strings are the same (because it will create an std string with an extra null on the right side as well): 我认为,如果您执行以下操作,则返回的字符串将是相同的(因为它还会创建一个在右侧也带有额外null的std字符串):

std::cout << (instruction == str("submit", _countof("submit"))) << std::endl;

PS: This is a common error made when taking a char* and making an std::string out of it, frequently just the array size itself is used, but that includes the terminating zero which std::string will add anyway. PS:这是在提取char *并制成std :: string时常犯的错误,通常仅使用数组大小​​本身,但是其中包括终止零,而std :: string仍会添加零。 I believe that something like this is happening to your input somewhere and if you get add a -1 wherever that is everything will work as expected. 我相信这样的事情发生在您某处的输入上,如果在任何地方添加-1,一切都会按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM