Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 应操作员>&燃气轮机;清理集装箱?_C++_Refactoring_Iostream - Fatal编程技术网

C++ 应操作员>&燃气轮机;清理集装箱?

C++ 应操作员>&燃气轮机;清理集装箱?,c++,refactoring,iostream,C++,Refactoring,Iostream,我正在重构一些从文件中读取整数容器的代码。我已将通用代码移到流提取器中,如下所示: std::istream &operator>>(std::istream &in, std::vector<int> &list); std::istream &operator>>(std::istream &in, std::map<int, std::vector<int>> &graph); s

我正在重构一些从文件中读取整数容器的代码。我已将通用代码移到流提取器中,如下所示:

std::istream &operator>>(std::istream &in, std::vector<int> &list);
std::istream &operator>>(std::istream &in, std::map<int, std::vector<int>> &graph);
std::istream&operator>>(std::istream&in,std::vector&list);
std::istream&operator>>(std::istream&in,std::map&graph);
我的问题是,这些函数是应该在写入之前清除容器,还是只是向容器插入/附加数据。(在我的特殊情况下,这并不重要,但为了代码重用的利益,我想知道。)


在这些情况下,C++有一个约定吗?我试图根据最小意外的原则进行编码。

标准库提供的提取操作符全部替换目标变量的内容


包括
std::string
,这是一种容器类型,就像您的示例一样。

这些容器以前在其他地方使用过吗?如果是的话,他们可能应该被清除。不,没有-我的意见是,如果你不去清除他们可能想要保存在容器中的数据,对调用方来说,这是最不令人惊讶的。而且,我认为这些操作符太笼统了,我不会实现这些。请阅读:它明确警告,只有在语义绝对清晰而不阅读代码/文档的情况下,您才会重载运算符。位移动流之所以属于这一类,是因为标准库做了它,所以要特别注意它做了什么。这正是我所想的,但我不确定它是否只适用于简单的数据类型。我忘了绳子。