Home c++ Type conversion in C++. Int to string

Type conversion in C++. Int to string

Author

Date

Category

What methods can be used to convert from int to string?

On the site http://en.cppreference.com they write about to_string , but my compiler gives an error “‘to_string’ was not declared in this scope”. Although the string header is included, it is also included with +11.

Compiler: MinGW

# include & lt; vector & gt;
#include & lt; set & gt;
#include & lt; iostream & gt;
#include & lt; string & gt;
using namespace std;
int main ()
{
  int n; cin & gt; & gt; n;
  multiset & lt; int & gt; a;
  multiset & lt; int & gt; forCheck;
  vector & lt; string & gt; ans;
  for (int i = 0; i & lt; n; ++ i) {
    int x; cin & gt; & gt; x;
    a.insert (x);
  }
  for (int i = 0; i & lt; n; ++ i)
  {
    if (forCheck.empty ())
    {
      forCheck.insert (* a.begin ());
      a.erase (* a.begin ());
      continue;
    }
    if (* forCheck.begin () & lt; * a.begin ())
    {
      forCheck.insert (* a.begin ());
      a.erase (* a.begin ());
    }
    else
    {
      forCheck.clear ();
    }
    if (forCheck.size () == 3)
    {
      string s = "";
      for (int i: forCheck)
        s + = to_string (i); // here!
      ans.push_back (s);
      forCheck.clear ();
    }
  }
  for (string str: ans) {
    cout & lt; & lt; str;
  }
  return 0;
}

 enter image description here


Answer 1, authority 100%

It remains to assume that the compiler does not implement the to_string function. Well, so it won’t take long and write it yourself, for example, like this:

string to_string (int n)
{
  char buf [40];
  sprintf (buf, "% d", n);
  return buf;
}

Or like this:

string to_string (int n)
{
  ostringstream ss;
  ss & lt; & lt; n;
  return ss.str ();
}

Answer 2, authority 67%

Indeed, in my version of MinGW there was no implementation of to_string: / Upgraded to 15.0 and everything works

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions