what library to use for stoi keyword in c++

Safwan Ull Karim picture Safwan Ull Karim · Oct 21, 2015 · Viewed 7.8k times · Source

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

Answer

mindriot picture mindriot · Oct 21, 2015

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.