std :: stringstream和str方法

时间:2022-06-08 02:21:24

I noticed something when trying to use the stringstream object. Here is a useless example to explain this:

我在尝试使用stringstream对象时注意到了一些事情。这是一个无用的例子来解释这个:

stringstream ss ;
ss << "my string" ;
cout << ss.str() << endl ;

Is not equivalent to

不等于

cout << (stringstream() << "my string").str() << endl ;

This leads to a compilation error complaining that ‘class std::basic_ostream’ has no member named ‘str’.

这会导致编译错误,抱怨'class std :: basic_ostream'没有名为'str'的成员。

I can't easily explain this. This is not critical for my application but I'm pretty sure this is hiding a c++ trick interesting to be understood.

我不能轻易解释这一点。这对我的应用程序并不重要,但我很确定这隐藏了一个有趣的c ++技巧。

Note: i'm using gcc with c++14

注意:我正在使用gcc和c ++ 14

2 个解决方案

#1


The operator<< is not defined for std::stringstream, but for its base class std::ostream, hence it does not return a reference to the std::stringstream and therefore the method str() (which is a method of std::stringstream) is not found.

运算符< <没有为std :: stringstream定义,但是对于它的基类std ostream,因此它不返回对std stringstream的引用,因此它返回方法str()(这是std的方法)找不到:: stringstream)。< p>

Technically, the above is actually std::basic_stringstream<CharT,Traits> and std::basic_ostream<CharT,Traits>, but you get the idea :)

从技术上讲,上面实际上是std :: basic_stringstream 和std :: basic_ostream ,但是你明白了:) ,traits> ,traits>

#2


The problem with this:

这个问题:

cout << (stringstream() << "my string").str() << endl;

It is that the expression stringstream() << "my string" returns a std::ostream& object, not a std::stringstream.

表达式stringstream()<<“my string”返回std :: ostream&object,而不是std :: stringstream。

You can rectify this by defining some appropriate overloads:

您可以通过定义一些适当的重载来纠正这个问题:

template<typename Type>
std::stringstream& operator<<(std::stringstream& ss, const Type& type)
{
    static_cast<std::ostream&>(ss) << type;
    return ss;
}

template<typename Type>
std::stringstream& operator<<(std::stringstream&& ss, const Type& type)
{
    static_cast<std::ostream&>(ss) << type;
    return ss;
}

template<typename Type>
std::stringstream& operator>>(std::stringstream& ss, Type& type)
{
    static_cast<std::istream&>(ss) >> type;
    return ss;
}

template<typename Type>
std::stringstream& operator>>(std::stringstream&& ss, Type& type)
{
    static_cast<std::istream&>(ss) >> type;
    return ss;
}

Now, when you use the << and >> operators on a std::stringstream object, it will call these overloads and they will return a std::stringstream& so you can use its interface directly.

现在,当你在std :: stringstream对象上使用< <和> >运算符时,它将调用这些重载,它们将返回一个std :: stringstream,因此你可以直接使用它的接口。

#1


The operator<< is not defined for std::stringstream, but for its base class std::ostream, hence it does not return a reference to the std::stringstream and therefore the method str() (which is a method of std::stringstream) is not found.

运算符< <没有为std :: stringstream定义,但是对于它的基类std ostream,因此它不返回对std stringstream的引用,因此它返回方法str()(这是std的方法)找不到:: stringstream)。< p>

Technically, the above is actually std::basic_stringstream<CharT,Traits> and std::basic_ostream<CharT,Traits>, but you get the idea :)

从技术上讲,上面实际上是std :: basic_stringstream 和std :: basic_ostream ,但是你明白了:) ,traits> ,traits>

#2


The problem with this:

这个问题:

cout << (stringstream() << "my string").str() << endl;

It is that the expression stringstream() << "my string" returns a std::ostream& object, not a std::stringstream.

表达式stringstream()<<“my string”返回std :: ostream&object,而不是std :: stringstream。

You can rectify this by defining some appropriate overloads:

您可以通过定义一些适当的重载来纠正这个问题:

template<typename Type>
std::stringstream& operator<<(std::stringstream& ss, const Type& type)
{
    static_cast<std::ostream&>(ss) << type;
    return ss;
}

template<typename Type>
std::stringstream& operator<<(std::stringstream&& ss, const Type& type)
{
    static_cast<std::ostream&>(ss) << type;
    return ss;
}

template<typename Type>
std::stringstream& operator>>(std::stringstream& ss, Type& type)
{
    static_cast<std::istream&>(ss) >> type;
    return ss;
}

template<typename Type>
std::stringstream& operator>>(std::stringstream&& ss, Type& type)
{
    static_cast<std::istream&>(ss) >> type;
    return ss;
}

Now, when you use the << and >> operators on a std::stringstream object, it will call these overloads and they will return a std::stringstream& so you can use its interface directly.

现在,当你在std :: stringstream对象上使用< <和> >运算符时,它将调用这些重载,它们将返回一个std :: stringstream,因此你可以直接使用它的接口。