There is a line like: "40 1234567 12"

You need to parse the second line (i.e. 1234567)

How to do it?

    2 answers 2

    Disclaimer: all fool protection is disabled, we believe that the format is exactly as shown.

    For example: :)

     string s = "40 1234567 12", get; istringstream in(s); s >> get; s >> get; 

    Or:

     s = s.substr(s.find(' ')+1); s = s.substr(0,s.find(' ')); 
    • only, considering that after the first word there may well be more than one space, remove the +1 and add the receipt of the string from the first non-space: s = s.substr (s.find_first_not_of ('')); - AR Hovsepyan
    • @ARHovsepyan I repeat: " Disclaimer: all fool protection is disabled, we believe that the format is exactly as shown. " And one space is shown ... - Harry
    • Harry, I see, just gave out information for Skymotion - AR Hovsepyan

    If you need to bring not only the second word, you can do this:

     #include <iostream> #include <string> #include <vector> #include <sstream> #include <iterator> using namespace std; int main() { string s; getline(cin, s); // вводим строку istringstream iss(s); // записываем все слова в вектор vector <string> a { istream_iterator<string>{iss}, istream_iterator<string>{} }; cout << a[1] << endl; // выводим второе слово return 0; }