0

my question is pretty simple but I can't seem to find it out. I want to know what libary to include when using stoi. I was using atoi and it works fine with

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

but I get "stoi not declared" when I run with stoi. Thanks

valiano
  • 16,433
  • 7
  • 64
  • 79
Safwan Ull Karim
  • 652
  • 5
  • 10
  • 20

1 Answers1

1

You need to #include <string> and use a compiler that understands C++11. Minimal example:

#include <string>
#include <cassert>

int main()
{
  std::string example = "1234";
  int i = std::stoi(example);
  assert(i == 1234);
  return 0;
}

Compile, for example, with g++ -std=c++11.

mindriot
  • 5,413
  • 1
  • 25
  • 34