重载istream运算符>> C ++

时间:2011-06-26 22:49:02

标签: c++ stream operator-overloading

假设我有一个字符向量,我将其作为字符串推入流中,而不是字符向量,我将如何使用运算符>>取回字符向量?

class C{
    private:
        vector<char> c;

    public:
        C(string str){
          for(int x = 0; x < str.size(); x++)
              c.push_back(str[x]);
        }

        vector<char> data(){
           return c;
        }       
};

ostream operator<<(ostream & stream, C & in){
   for(int x = 0; x < in.data().size(); x++)
      stream << in.data()[x];
   return stream;
}

istream operator>>(istream & stream, C & in){
    // ???
    // what kind of loop?
}

5 个答案:

答案 0 :(得分:3)

我会像这样写你的例子......

#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
#include <sstream>

class C
{
    std::vector<char> mData;

  public:
    // Constructor, note im using the iterator range 
    // vector constructor.
    C(const std::string& str)
    : mData(str.begin(), str.end())
    {
    }

    // Returning data by const reference to avoid 
    // copying a potentially large object.
    const std::vector<char>& data() const
    {
        return mData;
    }

    // declared the input operator as a friend as I need it to
    // access mData - alternatively you could write a "loadFromStream"
    // type function that does the same, declare this as non-friend, and call that.
    friend std::istream& operator>>(std::istream& is, C& c);
};

std::ostream& operator<<(std::ostream& os, const C& c)
{
    // Use an ostream_iterator to handle output of the vector
    // using iterators.
    std::copy(c.data().begin(), 
              c.data().end(), 
              std::ostream_iterator<char>(os, ""));

    return os;
}

std::istream& operator>>(std::istream& is, C& c)
{
    // load the data using the assign function, which
    // clears any data already in the vector, and copies 
    // in the data from the specified iterator range.
    // Here I use istream_iterators, which will read to the end
    // of the stream.  If you dont want to do this, then you could 
    // read what you want into a std::string first and assign that.
    c.mData.assign(std::istream_iterator<char>(is),
                   std::istream_iterator<char>());

    return is;
}

int main()
{
    C c("Hello");

    std::stringstream ss;
    ss << c;

    std::cout << ss.str() << std::endl;

    C d("");
    ss >> d;

    std::cout << d.data().size() << std::endl;

    return 0;
}

答案 1 :(得分:2)

你总是可以从另一个构建一个:

std::vector<char> A = getVector();
std::string       B = getString();

std::vector<char> newB(B.begin(), B.end());
std::string       newA(A.begin(), A.end());

使用它你应该能够编写你的插播操作符,例如

std::string stmp;
stream >> stmp;
std::vector<char> vtmp(stmp.begin(), stmp.end());
c.swap(vtmp);

答案 2 :(得分:1)

做什么&gt;&gt; std::string的确如此,你真的需要使用std::string。请注意,您需要通过引用而不是值传递C,并且运算符应该通过引用返回原始流。另外,我不禁想到使用std::vector<char>代替std::string并不是那么有用(另外,ctor效率低下 - 至少保留str.length()如果你这样做)。

istream& operator>>(istream& s, C& in) {
    std::string tmp;
    s >> tmp;
    // you also need to return reference in in.data() for this to work
    // or declare operator>> as friend and use in.c directly
    in.data().assign(tmp.begin(), tmp.end());
    return s;
}

答案 3 :(得分:1)

首先,您需要将您的流作为引用而不是按值返回。此外,data()应返回对向量的const引用,以便不复制它(如果它是一个大向量,则很重要)。

至于重载&gt;&gt;,我会尝试类似:

istream& operator>>(istream& stream, C& in) {
    for (char c; /* some end condition */;) {
        stream >> c;
        in.c.push_back(c);
    }
}

当然,这种方法需要声明operator>>(istream&, C&)朋友的功能。另一种方法是向公共接口提供等效的append(char)函数。此外,data()应标记为const,因此整个签名将为const vector<char>& data() const,表明它严格来说是一个访问者。

答案 4 :(得分:1)

您可以使用istream迭代器。 它的默认构造函数初始化为流的结尾。

http://www.cplusplus.com/reference/std/iterator/istream_iterator/

您的代码看起来就像。

typedef std::istream_iterator<char> charstream_it;
c = std::vector<char>(charstream_it(stream),  charstream_it());

在构造函数中,您可能应该使用Kerrek SB建议的STL迭代器样式复制构造函数。

C::C(string str):
c(str.begin(), str.end()) {};
相关问题