Integer for String

Asked

Viewed 880 times

4

In QT Creator 5, how do I convert Integer to string? In Visual Studio 2012 there was the command:

std::to_string(10);

But it doesn’t work on QT. I tried to:

#include <QCoreApplication>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
  QCoreApplication a(argc, argv);
  std::cout << std::to_string(10);
  return a.exec();
}

That is the mistake:

G:\PROJETOS\CPP\untitled1\main.cpp:7: error: 'to_string' is not a member of 'std'
   std::cout << std::to_string(10);
                ^
  • You want to convert to std::string or QString? Put the code you are trying to do, the problem may be another. And put what error or problem occurred.

  • For Std::string.

  • Give me more details to complement the answer. Enter the whole code. http://answall.com/help/mcve

  • 1

    Why use std::string, you are trying to modify an existing code?

  • The code you posted doesn’t have what you asked for. And it’s not complete.

  • There. That’s the way it is, there’s nothing else.

  • 1

    If you are going to write on the console and use no Qt feature, why are you using QCoreApplication? Which compiler is being used? Is it Mingw? Which version?

  • It’s just an example. I need to use on this occasion.

  • 2

    To enable C++11 in Qtcreator add CONFIG += C++11 in the project configuration file (the .pro). This if the compiler supports C++11.

  • 1

    I don’t know what the ultimate purpose of the code is, but usually if you’re going to use Qt, a common option is to use the types available in Qcore, mainly to get peace of mind when porting to various platforms and/or compilers.

Show 5 more comments

3 answers

3


I just took a test and it worked:

#include <string>

int main() {
  std::string s = std::to_string(10);
}

If it doesn’t work you’re probably using an old compiler. I strongly advise you to update it.

If you want to break a branch temporarily you can use a function of your own:

#include<sstream>
template <typename T> std::string to_string(T value) {
  //create an output string stream
  std::ostringstream os;
  //throw the value into the string stream
  os << value ;
  //convert the string stream into a string and return
  return os.str() ;
}

I put in the Github for future reference.

Source: response in the OS.

Compile with your application and you won’t have any problems.

3

To use std::to_string it is necessary C++11

But you can do it another way using sstream

An example

#include <iostream>
#include <sstream>

int main(int argc, char *argv[])
{
    std::stringstream ss;
    ss << 10;

    std::string minhanovastring = ss.str();
    std::cout << minhanovastring;

    return a.exec();
}

2

Use the class function: QString::number();. Shown as follows:

#include <QTextStream>
#include <QString>

QTextStream out(stdout);

int main(){

    int num = 10;

    QString s = QString::number(num);

    out << s << endl;

    return 0;
}

QString is better at integrating with Qt, and has more features. It also becomes better at different encodings. The only drawback is that for projects not Qt, the std::string is the standard.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.