0
I’m writing a little show input a string of integer numbers as a string and as output an array with twice each of the numbers, also as a string. Despite the function atoi
be indicated in that reply, li here that it is better to use the function std::stoi
.
It happens that when I try to transform the string
for int
using std::stoi
, the program returns the following error:
error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type&)’
Below is my code for error replication:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
string var1="1234";
vector <string> empty(4,"a");
for (int i=0;i<4;i++){
int var2=stoi(var1[i]);
empty[i]=2*var2;
}
for (string x : empty){
cout<<x<<endl;
}
return 0;
}
Why this error is occurring and how do I correct?
You are using the function
stoi
wrong way. Try:std::string::size_type sz; int var2=stoi(var1, &sz);
– anonimo