Differences between C string and compare

0 votes

I just read some recommendations on using

std::string s = get_string();
std::string t = another_string();

if( !s.compare(t) ) 
{

instead of

if( s == t )
{

I nearly always choose the final one since it feels more natural and readable to me. 

I had no idea there was a separate comparison function. 

To be more specific, I assumed == would invoke comparison ().

What are the distinctions? 

In what circumstances should one approach be preferred over another?

I'm just thinking of scenarios when I need to know if one string has the same value as another.

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
382 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

0 votes
0 answers

What are the differences between struct and class in C++?

I now want to understand the distinctions ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
507 views
0 votes
1 answer

C++ code file extension? What is the difference between .cc and .cpp [closed]

GNU GCC recognizes all of the following ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,505 views
0 votes
1 answer

Difference between Turbo C++ and Borland C++ compiler [closed]

I will try my best to respond, ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,165 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
408 views
0 votes
1 answer

What is the difference between public, private, and protected inheritance in C++?

To begin answering that question, let me characterise member accessors in my own terms.  If you already know this, proceed to the section "next:". I'm aware of three types of accessors: public, protected, and private. Let: class Base { public: ...READ MORE

answered Jul 11, 2022 in C++ by Damon
• 4,960 points
747 views
0 votes
0 answers

Difference between for loop and the range based loop in C++

The distinction between a for loop and ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
592 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
527 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,386 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
688 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
538 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP