重载 istream 运算符>> c++

Overloading istream operator>> c++

本文关键字:gt c++ istream 运算符 重载      更新时间:2023-10-16

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

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?
}

我会这样写你的例子。。。。

#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;
}

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

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);

要为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;
}

您可以使用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()) {};

首先,您希望将流作为引用而不是按值返回。此外,data()应该返回一个对向量的常量引用,这样它就不会被复制(如果它是一个大向量,这一点很重要)。

至于过载>>,我会尝试这样的方法:

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,这表明它严格意义上是一个访问器。