Safwan Ull Karim
Safwan Ull Karim

Reputation: 662

what library to use for stoi keyword in c++

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

Upvotes: 0

Views: 18156

Answers (1)

mindriot
mindriot

Reputation: 5678

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.

Upvotes: 1

Related Questions