Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Compare two sf::String  (Read 3895 times)

0 Members and 1 Guest are viewing this topic.

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Compare two sf::String
« on: September 02, 2009, 03:09:25 am »
I've tried the following to no avail:
Code: [Select]

sf::String strCompare("text");
if (myText.GetText() == strCompare.GetText())
{
    ...
}

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Compare two sf::String
« Reply #1 on: September 02, 2009, 05:41:55 am »
Nevermind, solved by casting the sf::String to a std::string then comparing with a string literal:
Code: [Select]

if (static_cast<std::string>(myString.GetText()) == "String to compare.")
{
    ...
}

christoph

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • http://www.christoph-egger.org
Compare two sf::String
« Reply #2 on: September 02, 2009, 09:52:31 am »
std::strcmp() from <cstring> would have solved that quite as well

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Compare two sf::String
« Reply #3 on: September 02, 2009, 07:01:56 pm »
Quote from: "christoph"
std::strcmp() from <cstring> would have solved that quite as well
Yes. Or you can do :
Code: [Select]
if (std::string(myString.GetText()) == "String to compare.") This last one is my favorite.
SFML / OS X developer

christoph

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • http://www.christoph-egger.org
Compare two sf::String
« Reply #4 on: September 02, 2009, 10:54:03 pm »
Quote from: "Hiura"
Yes. Or you can do :
Code: [Select]
if (std::string(myString.GetText()) == "String to compare.") This last one is my favorite.


Well there's not much (if any) difference in the working of this compared to the static_case and == for strings quite likely just calls strcmp() on the c_str() parts again so casting to string is likely to be a waste of resources ;)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Compare two sf::String
« Reply #5 on: September 02, 2009, 11:19:44 pm »
Quote
a waste of resources

Isn't it negligible ?  :wink:
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Compare two sf::String
« Reply #6 on: September 04, 2009, 05:05:44 pm »
Quote from: "christoph"
Well there's not much (if any) difference in the working of this compared to the static_case and == for strings quite likely just calls strcmp() on the c_str() parts again so casting to string is likely to be a waste of resources ;)
And creating a temporary std::string on which you call c_str() is less waste of resources?

Very likely, the direct operator== comparison is as fast or even faster than the comparision of c-strings. For example, if the strings have different length, the evaluation can be done in O(1). On the contrary, strcmp() requires O(n) in every case. Besides, it is possible that a c_str() call implies additional operations like adding a null termination (std::string isn't required to store a null-terminated c-string).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything