stringstream string to int

后端 未结 4 823
独厮守ぢ
独厮守ぢ 2021-02-05 12:17

The C++ code below does int to string and a string to int conversions. Then, it repeats these steps again. The stringst

4条回答
  •  孤街浪徒
    2021-02-05 12:42

    I'm a C guy most of the day, so I would simply use atoi() do to the conversion:

    #include  // header for atoi()
    
    //stream1 >> i3;  // replace this for the line below
    i3 = atoi(stream1.str().c_str());
    
    cout << "i3-- " << i3 << endl;
    

    What I'm doing is retrieving a std::string from the stringstream, and from that, getting a const char* to be used as argument for atoi(), which converts a C string to an integer.

提交回复
热议问题