SFML community forums

Help => System => Topic started by: SillySquirrel on November 26, 2014, 07:31:30 pm

Title: What the simplest way to convert digits (int, double, float etc) to sf::String?
Post by: SillySquirrel on November 26, 2014, 07:31:30 pm
Hi guys my question in subject
I use template for it

Code: [Select]
#include <string>
#include <sstream>

template <typename T>
std::string toString(T val)
{
    std::ostringstream oss;
    oss<< val;
    return oss.str();
}

template<typename T>
T fromString(const std::string& s)
{
  std::istringstream iss(s);
  T res;
  iss >> res;
  return res;
}

I think must be easier way
Title: Re: What the simplest way to convert digits (int, double, float etc) to sf::String?
Post by: Jesper Juhl on November 26, 2014, 07:35:01 pm
std::to_string (http://en.cppreference.com/w/cpp/string/basic_string/to_string).

Or, if you have to use C++98 rather than modern C++, then use std::stringstream or boost::lexical_cast or sprintf or similar.

Btw: this has nothing to do with SFML - it's a basic C++ question that is really better asked elsewhere...
Title: Re: What the simplest way to convert digits (int, double, float etc) to sf::String?
Post by: SillySquirrel on November 26, 2014, 07:53:38 pm
i use std::to_string before but gets error to_sting isn't std member on gcc 4.7.1 :(

I thought SFML has some typecasting method

Thx for fast anwser ;)
Title: Re: What the simplest way to convert digits (int, double, float etc) to sf::String?
Post by: Jesper Juhl on November 26, 2014, 07:56:33 pm
Use a modern compiler/std library.
A cast doesn't convert types in such a complex manner as fx int->string - maybe in scripting languages but not in C++ with strong type safety.
Title: Re: What the simplest way to convert digits (int, double, float etc) to sf::String?
Post by: SillySquirrel on November 26, 2014, 10:03:01 pm
I update mingw for version 4.8.1 TDM but problem with std::to_string() not resolves
Here (http://tehsausage.com/mingw-to-string) i found some fix (not work for me actually)

PS -std=c++11 of course

PPS I found the problem. IDE global compile vars set c++0x at the end of command line options
Code: [Select]
mingw32-g++.exe -std=c++11 -Wzero-as-null-pointer-constant -std=c++0x -ID:\SFML\INCLUDE -ID:\SFML\lib -c D:\TEST\main.cpp -o obj\Debug\main.o
LOL