重载istream运算符>> c++
假设我有一个字符向量,并且我将其作为字符串而不是字符向量推入流中,那么我如何使用运算符>>取回字符向量?
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?
}
Say I have a vector of chars and I pushed it into a stream as a string, rather than a vector of chars, how would i get back the vector of chars using operator>>?
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?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会像这样写你的例子......
I'd write your example like this....
您始终可以从另一个构造一个:
使用它您应该能够编写您的流内运算符,例如
You can always construct one from the other:
Using that you should be able to write your in-stream operator, e.g.
做什么>>对于
std::string
确实如此,您实际上只需要使用std::string
即可。请注意,您需要通过引用而不是值传递C
,并且运算符应通过引用返回原始流。另外,我忍不住认为使用std::vector
而不是std::string
并不是那么有用(另外,ctor 效率低下) — 如果你这样做的话,至少保留str.length()
)。To do what >> for
std::string
does, well, you really just need to usestd::string
. Note that you need to passC
by reference, not by value, and operators should return the original stream by reference. Also, I can't help but think that usingstd::vector<char>
instead ofstd::string
is not really that useful (plus, the ctor is inefficient — at least reservestr.length()
if you're doing this).您可以使用 istream 迭代器。
其默认构造函数初始化为流末尾。
http://www.cplusplus.com/reference/std/iterator/istream_iterator/
你的代码看起来像这样。
在构造函数中,您可能应该按照 Kerrek SB 的建议使用 STL 迭代器样式的复制构造函数。
You can use the istream iterator.
Its default constructor initializes to end of stream.
http://www.cplusplus.com/reference/std/iterator/istream_iterator/
Your code would then look something like.
In your constructor, you should probably use the STL iterator style copy constructor as advised by Kerrek SB.
首先,您需要将流作为引用而不是按值返回。另外,
data()
应该返回对向量的 const 引用,以便它不会被复制(如果它是一个大向量,这一点很重要)。至于重载>>,我会尝试类似的方法:
当然,这种方法需要声明
operator>>(istream&, C&)
一个友元函数。另一种方法是向公共接口提供相当于append(char)
函数的功能。另外,data()
应标记为 const,因此整个签名将是const vector& data() const
明确表明它是严格的访问器。First of all, you'll want to return your streams as references instead of by value. Also,
data()
should return a const reference to the vector, so that it isn't copied (important if it's a large vector).As for overloading >>, I would try something like:
Of course, this approach would require declaring
operator>>(istream&, C&)
a friend function. The alternative is to supply the equivalent of anappend(char)
function to your public interface. Also,data()
should be marked const, so the whole signature would beconst vector<char>& data() const
making it clear that it is strictly an accessor.