Zachary Kraus
Zachary Kraus

Reputation: 1101

For C++ strings is using compare method or relational operators better

I am working with a lot of strings in this project and currently in my code I have a lot of if (StringA.compare(StringB) == 0). I am wondering if replacing the comparisons in the if statements with (StringA == StringB) would make the code easier to read? Also, is using the relational operators instead of the compare method preferred by c++ coders? Additionally does using the relational operators take more time to run? And if so, how much extra time?

Upvotes: 1

Views: 100

Answers (1)

パスカル
パスカル

Reputation: 489

Use relational operators with parentheses for stacking operators. Use relational operators for all situations where you do not need the result of compare, which can produce <0,0,0< results depending on the difference. Speed is equivalent.

Upvotes: 1

Related Questions