istringstream 8-

:

#include <iostream>
#include <sstream>
#include <inttypes.h>

using namespace std;

int main (void) {
    istringstream iss("123 42");
    int8_t x;
    while (iss >> x) {
        cout << x << endl;
    }
    return 0;
}  

      

:

1
2
3
4
2

      

:

123
42

      

iss >> (int)x

( char

), " : ('istringstream' (aka 'basic_istringstream') 'int')" (clang) " : 'operator → " (g++).

8- , -?

+3




4


. char - (char, char unsigned char) , . int8_t, , typedef , .

:



  • .
  • C-style casts, .
  • EOF , .
+1




8- ; signed char

, IOStreams ASCII- char

.

, , wrap int8_t

, - ( , / ).



( iss >> (int)x

, , , lvalues, , .)

+2




, int8_t

(-, ) : typedef char int8_t;

. , iostreams char

, , .

, , , :

class my_int8_t {
    // ...
};

      

operator>>

operator<<

( , ).

, ( ), , :

std::copy(std::istream_iterator<my_int8_t>(std::cin), 
          std::istream_iterator<my_int8_t>(),
          std::ostream_iterator<my_int8_t>(std::cout, "\n"));

      

, .

+2




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

typedef unsigned char uint8_t;
class exstringstream : public stringstream
{
public:
    exstringstream(const string& s)
        :stringstream(s)
    {

    }
    friend exstringstream& operator >> (exstringstream&, uint8_t& t); 
};

exstringstream& operator >> (exstringstream& ss, uint8_t& t)
{
    unsigned int val;
    stringstream& s = ss;
    s >> val;

    t = static_cast<uint8_t>(val);

    return ss;
}

int main()
{
    string str("123 45");
    exstringstream ss(str);

    uint8_t a, b;
    ss >> a >> b;
    cout << a << " " << b << endl;
    return 0;
}

      

+1









All Articles