vendredi 5 janvier 2018

Convert string element to integer (C++11)

I am trying to convert string element to integer using stoi function in C++11 and using it as parameter to pow function, like this:

#include <cstdlib>
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    string s = "1 2 3 4 5";

    //Print the number's square
    for(int i = 0; i < s.length(); i += 2)
    {
        cout << pow(stoi(s[i])) << endl;
    }
}

But, i got an error like this:

error: no matching function for call to 
'stoi(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)'
cout << pow(stoi(s[i])) << endl;

Anyone know what is the problem with my code?

Aucun commentaire:

Enregistrer un commentaire