How to force std::stringstream operator >> to read an entire string?

后端 未结 6 1541
鱼传尺愫
鱼传尺愫 2021-01-05 08:01

How to force std::stringstream operator >> to read an entire string instead of stopping at the first whitespace?

I\'ve got a template class that stores a value read

相关标签:
6条回答
  • 2021-01-05 08:18

    There isn't a way with operator>> that I'm aware of excepted writing your own facet (operator>> stop at first character for which isspace(c, getloc()) is true). But there is a getline function in <string> which has the behaviour you want.

    0 讨论(0)
  • 2021-01-05 08:20

    Where do you want it to stop? If you want to read a whole line you probably need getline function, if you need an entire string stored in the streamstring object your choise is ostringstream::str method.

    0 讨论(0)
  • 2021-01-05 08:26

    As operator >> is not satisfying our requirement when T=string, we can write a specific function for [T=string] case. This may not be the correct solution. But, as a work around have mentioned.

    Please correct me if it won't satisfy your requirement.

    I have written a sample code as below:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    template <class T>
    class Data
    {
        T m_value;
        public:
        void set(const T& val);
        T& get();
    };
    
    template <class T>
    void Data<T>::set(const T& val)
    {
        stringstream ss;
        ss << val;
        ss >> m_value;
    }
    
    void Data<string>::set(const string& val)
    {
        m_value = val;
    }
    
    template <class T>
    T& Data<T>::get()
    {
        return m_value;
    }
    
    int main()
    {
        Data<int> d;
        d.set(10);
        cout << d.get() << endl;
    
        Data<float> f;
        f.set(10.33);
        cout << f.get() << endl;
    
        Data<string> s;
        s.set(string("This is problem"));
        cout << s.get() << endl;
    }
    
    0 讨论(0)
  • 2021-01-05 08:27

    If you can use Boost then use boost::lexical_cast.

    0 讨论(0)
  • 2021-01-05 08:30

    I'm assuming you're instantiating that template with T = std::string. In that case you could use getline:

    getline(ss, m_value, '\0');
    

    However, this assumes you won't accept nul-characters as valid parts of the string.

    Otherwise you can write your own extractor for `T'.

    0 讨论(0)
  • 2021-01-05 08:45

    Here is a solution :

    std::istream & ReadIntoString (std::istream & istr, std::string & str) 
    { 
        std::istreambuf_iterator<char> it(istr), end; 
        std::copy(it, end, std::inserter(str, str.begin())); 
        return istr; 
    } 
    

    (Thanks to the original poster in C++ newsgroup)

    0 讨论(0)
提交回复
热议问题