C++ 模板参数中容器迭代器的显式声明
我有一个模板,其中我将输出操作符声明为朋友:C++ 模板参数中容器迭代器的显式声明,c++,templates,c++11,C++,Templates,C++11,我有一个模板,其中我将输出操作符声明为朋友: template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont=std::vector> class VehiclesContainer { public: VehiclesContainer(std::initializer_list<T> l):containe
template <typename T, template <typename ELEM, typename ALLOC=std::allocator<ELEM> > class Cont=std::vector>
class VehiclesContainer {
public:
VehiclesContainer(std::initializer_list<T> l):container(l){};
virtual ~VehiclesContainer(){};
template
<typename U, template <typename ELEM2, typename ALLOC=std::allocator<ELEM2> > class Cont2>
friend std::ostream& operator<<(std::ostream& out, const VehiclesContainer<U,Cont2>& obj);
private:
Cont<T> container;
};
模板
二级车辆及货柜{
公众:
VehicleContainer(std::initializer_list l):容器(l){};
virtual~VehiclesContainer(){};
模板
friend std::ostream&operator您需要使用常量迭代器
,参数obj
是常量
@dyp您是对的,谢谢。
template
<typename T,template <typename ELEM,typename ALOC=std::allocator<ELEM> > class Cont>
std::ostream& operator<<(std::ostream& out,const VehiclesContainer<T,Cont>& obj){
for(auto it=obj.container.begin(); it!=obj.container.end(); ++it)
out << *it << " ";
return out;
}