-1

I am getting error of type casting from basic string type to integer type and I do not know how to resolve it in the case of vector string and vector of integer type.

below given image shows the error

s1[j]+=int(magazine[i]);

this is the error

Nic3500
  • 8,144
  • 10
  • 29
  • 40

1 Answers1

2

You can't cast a string to integer.However,You can use stoi() to get the string numeric value and then assign it to an integer:

#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    string str1 = "45"; 
    string str2 = "3.14159"; 
    string str3 = "31337 geek"; 

    int myint1 = stoi(str1); 
    int myint2 = stoi(str2); 
    int myint3 = stoi(str3); 

    cout << "stoi(\"" << str1 << "\") is "
         << myint1 << '\n'; 
    cout << "stoi(\"" << str2 << "\") is "
         << myint2 << '\n'; 
    cout << "stoi(\"" << str3 << "\") is "
         << myint3 << '\n'; 

    return 0; 
} 




Output:

stoi("45") is 45
stoi("3.14159") is 3
stoi("31337 geek") is 31337

There are other methods too like using stringstream which you might want to look at.Hope it helps.