【发布时间】:2017-10-23 06:37:39
【问题描述】:
我有一个输入:
1 a
2 b
..
我想将它们插入到对向量中,具有复制功能,如下所示:
#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>
int main(void) {
std::vector<std::pair<int, char>> v;
std::copy(std::istream_iterator<std::pair<int, char>>(std::cin), std::istream_iterator<std::pair<int, char>>(), std::back_inserter(v));
for(auto pair: v)
std::cout << pair.first << std::endl;
return 0;
}
但是,这不会编译:error: no match for 'operator>>',因为它可能需要运算符重载。
这是否意味着我必须创建自己的类,继承自 std::vector,然后重载运算符?
我想避免使用我自己的类,而不是标准的向量类。
【问题讨论】:
-
你需要为
<<定义你自己的实现来读取一对like this may be -
哦@P0W,我明白了迭代器的问题,而不是向量的问题!
-
不,我在
std命名空间中做/建议有点犹豫。有更好的选择,可能需要更多的努力。
标签: c++ oop vector stl operator-overloading