lundi 4 mai 2015

Extract int from string c++

I have a problem in regards of extracting signed int from string in c++. Assuming that i have a string of images1234, how can i extract the 1234 from the string without knowing the position of the last non numeric character in C++.

FYI, i have try stringstream as well as lexical_cast as suggested by others through the post but stringstream returns 0 while lexical_cast stopped working.

int main()
{
    string virtuallive("Images1234");
    //stringstream output(virtuallive.c_str());
    //int i = stoi(virtuallive);
    //stringstream output(virtuallive);
    int i;
    i = boost::lexical_cast<int>(virtuallive.c_str());
    //output >> i;
    cout << i << endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire