带指针的ostream_iterator [英] ostream_iterator with pointers
本文介绍了带指针的ostream_iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让
Foo.hpp:
class Foo
{
public:
void print() const;
protected:
vector<Bar<string, int>*> bar_;
};
void Foo::print() const
{
copy(bar_.begin(), bar_.end(), ostream_iterator<Bar<string, int>*>(cout, "\n"));
}
Bar.hpp:
class Bar
{
public:
template <typename K, typename U>
friend ostream& operator<<(ostream&, const Bar<K, U>&);
}
template <typename Key, typename T>
ostream& operator<<(ostream& out, const Bar<Key, T>& bar)
{
return out << "FOOBAR";
}
其中bar_是Foo的属性, 。假设bar_有一个元素,然后输出:
Where bar_ is an attribute of Foo and is a vector of pointers to Bar elements. Assume bar_ has one element, then the output of :
Foo foo;
foo.print();
是bar_中元素的地址,而不是FOOBAR。如果我不使用指针,输出将是FOOBAR我想要实现这里是打印FOOBAR的每个指针的bar _。
is the address of the element in bar_, instead of "FOOBAR". If I wouldn't use pointers, the output would be "FOOBAR" What I'm trying to achieve here is to print "FOOBAR" for each pointer of bar_.
推荐答案
尝试添加:
template <typename Key, typename T>
ostream& operator<<(ostream& out, Bar<Key, T>* bar)
{
return out << *bar;
}
这篇关于带指针的ostream_iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文