c ++中字符串中的等号运算符 [英] equality operator in string in c++

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

问题描述

  //  案例1  
char * p = ram;

// 将其与常量文字字符串进行比较
< span class =code-keyword> if (p == ram
{
// condition1
int i = 1 ;
}

// case2
char * q = new char [ 4 ];
memset(q, 0 4 );
memcpy(q, ram 3 );

// 将字符串q与常量字符串进行比较
< span class =code-keyword> if
(q == ram
{
// condition2
int i = 2 ;
}





在上面的C ++代码片段中,我发现在 case1 如果条件评估为真,并且在 case2 时进入 condition1 ,当我分配 q 指针时动态然后如果条件评估为并且它没有进入 condition2 。任何人都可以用等号运算符解释 char * 的这种行为。

解决方案

简单:你正在比较两个指针。如果两者都指向同一个地方,那么它们是相等的。如果他们指向不同的地址,它们就不一样了。



p 指向一个常量字符串, q 指向一个内存位置(在堆栈上),它恰好包含相同的数据,但不在同一个地方。 (如果是的话,那么 q 指向的内容的任何更改都会影响对ram的所有常量引用,这将是一件非常非常糟糕的事情!)



如果要比较字符串内容,请使用 strcmp [ ^ ]代替。

== 如果两者都指向相同的内存地址,则应用于指针。



但是你在未定义行为的阴暗面操作。当定义一个常量(比如ram)时,编译器可以自由地优化或不是所有的重复作为一个相同的常量(所以总是使用相同的地址)



根据编译器类型和选项设置,可能所有ram文字都被认为是同一个东西,只实例化一次,并且指向它的所有指针都指向同一个地址。



或者代码中的部分或全部ram可以单独处理。



if(p ==ram)可能是真或假,具体取决于编译器优化(如果第二个ram被认为是同一个常量,则为真在 char * p =ram



q ==ram 始终为false,因为只要ram停留在哪里, q 指向 new char [4] <返回的块/ code>(你永远不会删除,但这是另一个问题)碰巧包含''r'','''','''','' \0'',因为你在那里复制了它们,但它肯定会是另一个不同的地方。


而不是使用C风格的空终止字符串,你应该使用std :: string类来避免这个问题以及与手动字符串处理相关的许多其他问题。


//Case 1
char * p = "ram" ;

//compare it with a constant literal string
if( p == "ram")
{
  //condition1
  int i= 1;
}

//case2
char* q = new char[4];
memset(q,0,4);
memcpy(q,"ram",3);

//compare string q with constant literal string
if( q == "ram")
{
    //condition2
    int i=2;
}



In the above code snippet in C++, I found that in case1, if condition evaluates to true and it enters the condition1 while in case2 when I was allocating q pointer dynamically then if condition evaluates to false and it does not enter in the condition2. Can anyone please explain this behaviour of char* with equality operator.

解决方案

Simple: you are comparing two pointers. If there are both pointing at the same place, then they are equal. If they point at different addresses, they are not the same.

p is pointing at a constant string, q is pointing at a memory location (on the stack) which happens to contain the same data, but is not in the same place. (If it was, then any change to the content pointed at by q would affect all the constant references to "ram" which would be a very, very bad thing!)

If you want to compare teh string contents, then use strcmp[^] instead.


== applyed to pointers is true if both point to the same memory address.

But you are operating in the dark side of "undefined behavior". When defining a constant (like "ram" is), the compiler is free to optimize or not all of its repetition as a same constant (so always the same address is used)

Depending on compiler type and options settings, may be all "ram" literals are considered the same thing, instantiated only once, and all pointers required to point to it to point to the same address.

Or it may be some or all the "ram" in your code be treated each one independently.

if(p=="ram") may be true or false depending on compiler optimization (will be true if this second "ram" is considered as the same constant as in char* p = "ram".

q=="ram" is always false, because wherever "ram" stays, q points to the block returned by new char[4] (that you never delete, but that''s another problem) that happens to contain ''r'',''a'',''m'',''\0'', because you copied them in there, but it will certainly be another different place.


Instead of using C style null terminated string, you should uses std::string class which avoid that problem and many other problem related to manual string handling.


这篇关于c ++中字符串中的等号运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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