c++stringcomparisonconst-string

How to compare a string to a const string in C++


Let's say I have the following poc code:

const string& a = "hello";
string b = "l";

if(a.at(2) == b)
{
 // do stuff
} 

I understand that there is no operator "==" that matches these operands. And, the way to fix it is by converting the value of the variable a into 'hello' (instead of double quotes) as char.

However, what if I have no choice but performing the comparison as shown in the code. Is it possible? Could you please provide any guidance or suggestions you might have on this issue.

Your responses are appreciated.


Solution

  • const string& a = "hello";
    string b = "l";
    
    if (a[2] == b[0])
    {
        // do stuff
    }
    

    a.at(2) is not string. when do b to b[0] problem solving.