C ++字符串和字符串文字比较 [英] C++ string and string literal comparison

查看:88
本文介绍了C ++字符串和字符串文字比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想简单地做一个 std :: string =="string-literal" ,除了我用

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 str(strCreateFrom, 0, strCreateFrom.find(' '));

并查找返回 string :: npos ,现在这两个都包含字符串"submit" ,但是 == 返回false,现在我有了将其缩小为大小实际上是不同"的事实. str.size()为7,而 strlen("submit")为6.这就是为什么 == 失败的原因,我认为是但是我不明白为什么...不应该检查dif的最后一个字符是否为 \ 0 ,就像在这种情况下一样?

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. 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?

无论如何,我可以不必使用compare并指定要比较或更改我的字符串的长度来解决这个问题吗?

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?

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;

打印

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

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

For more clarification as to why I'm confused I read the basic_string.h header and saw this:

/**
   *  @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;

哪个是从operator ==中调用的,所以我试图找出为什么大小差异很重要.

Which is called from operator== so I am trying to find out why the size dif matters.

推荐答案

我不太了解您的问题,可能需要更多详细信息,但是您可以使用c比较,这应该不会出现空终止计数的问题.您可以使用:

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. You could use:

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

strncmp还可以用于比较char数组中给定数量的chars

strncmp also can be used to compare only a given number of chars in a char array

或者尝试修复stdstring的创建

Or try to fix the creation of the stdstring

您未解析的std :: string已经坏了.它已经在字符串中包含额外的null,因此您应该看看它是如何创建的.就像我之前提到过的那样,mystring [mystring.size()-1]是最后一个字符而不是终止null,因此,如果像在输出中那样在其中看到一个'\ 0',则意味着null被视为字符串的一部分.

Your unparsed std::string is already bad. It already contains the extra null in the string, so what you should look at is how it is being created. 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]不是'\ 0'.

Try to trace back your parsed input and keep making sure that mystring[mystring.size() -1] is not '\0'.

要回答您的尺寸差异问题:这两个字符串不同,字面量较短,并且没有null.

To answer your size diff question: The two strings are not the same the literal is shorter and doesn't have a null.

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

当比较到达字面量的终止null时,比较将停止比较,但是它将存储的大小用于std :: string,这是7,因为该字面量以6结尾,但std为7,它将表示std较大

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字符串):

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):

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

PS:这是在获取char *并从中取出std :: string时常犯的错误,通常仅使用数组大小​​本身,但是其中包括终止零,无论如何std :: string都会添加.我相信在您的某处输入中会发生类似的事情,如果在任何地方添加-1,一切都会按预期进行.

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. 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.

这篇关于C ++字符串和字符串文字比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆