重载 istream 运算符>> c++
Overloading istream operator>> 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?
}
我会这样写你的例子。。。。
#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
,这表明它严格意义上是一个访问器。
相关文章:
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- ``这个''不能用this-&gt;指针变量
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 使用shared_ptr<字符串>转换为一个无序集合<字符串>
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到