重载<<同时用于地图和多图 [英] overloading << for map and multimap simultaneously

查看:58
本文介绍了重载<<同时用于地图和多图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重载了运算符<<对于STL地图成功:


模板< typename T1,typename T2ostream&运算符<< (ostream&

o,地图< T1,T2& m)

{

//代码

}


代码就像魅力一样。现在,我想要

multimap的相同功能。由于他们的界面对于手头的问题是相同的,我想要使集合名称成为这样的模板参数:


模板< typename MAP,typename T1,typename T2ostream&运营商

<< (ostream& o,MAP< T1,T2& m)

{

//代码

}

这样的编译器错误:语法错误:在''之前缺少'''''<''


thx,

ozi 。

I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}

the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
want to make collection name a template parameter like this:

template <typename MAP, typename T1, typename T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code
}

The compiler errors like this: syntax error : missing '')'' before ''<''

thx,
ozi.

推荐答案

ozizus写道:
ozizus wrote:

我重载了运算符< <对于STL地图成功:


模板< typename T1,typename T2ostream&运算符<< (ostream&

o,地图< T1,T2& m)

{

//代码

}


代码就像魅力一样。现在,我想要

multimap的相同功能。由于他们的界面对于手头的问题是相同的,我想要使集合名称成为这样的模板参数:


模板< typename MAP,typename T1,typename T2ostream&运营商

<< (ostream& o,MAP< T1,T2& m)

{

//代码

}

这样的编译器错误:语法错误:缺少'')'''''<'''
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}

the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
want to make collection name a template parameter like this:

template <typename MAP, typename T1, typename T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code
}

The compiler errors like this: syntax error : missing '')'' before ''<''



编译器无法从中推断出所有三个你给出的论点。

此外,如果''MAP''是一个模板(正如你在声明第二个参数时使用模板

语法所暗示的那样),那么它''不是'typename'

参数,它是'template< class,classclass MAP"论点。尝试


模板<模板<类,类class MAP,typename T1,typename T2>

ostream&运算符<< (...


但它很可能无法正常工作。我建议删掉T1

和T2参数并且只做


模板< class MAPostream& operator<<(ostream& o,MAP const& m)


(你确实想拥有输出对象'' const'',顺便说一下。


V

-

请删除大写''A'的时候通过电子邮件回复

我没有回复最热门的回复,请不要问

The compiler cannot deduce all three from the argument you give.
Besides, if ''MAP'' is a template (as you imply by using the template
syntax when declaring the second argument), then it''s not a "typename"
argument, it''s a "template <class,classclass MAP" argument. Try

template<template<class, classclass MAP, typename T1, typename T2>
ostream& operator << (...

but it is most likely not going to work. I recommend dropping the T1
and T2 arguments and just do

template<class MAPostream& operator <<( ostream& o, MAP const& m)

(you do want to have the output object ''const'', by the way).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


ozizus写道:
ozizus wrote:

我成功重载了运算符<< for STL map:


template< typename T1,typename T2ostream& operator< <(ostream&

o,map< T1,T2& m)

{

//代码

}


代码就像魅力一样。现在,我想要

multimap的相同功能。因为他们的界面对于han的问题是相同的d,我

想要使集合名称成为这样的模板参数:


模板< typename MAP,typename T1,typename T2ostream&运营商

<< (ostream& o,MAP< T1,T2& m)

{

//代码

}

编译器错误如下:语法错误:缺少'')'''''''''
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}

the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
want to make collection name a template parameter like this:

template <typename MAP, typename T1, typename T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code
}

The compiler errors like this: syntax error : missing '')'' before ''<''



a)从技术上讲,你想要类似的东西:


模板< template< class,class class MAP,typename T1,typename T2>


阅读模板模板参数(与typename模板相反

参数) 。


b)这里使用模板模板参数是一个BadIdea(tm),因为你的
模板会匹配太多东西,例如,它会匹配一个对< S,T取代。在

这种情况​​下,你的代码将不起作用。


因此:你应该为多图片做另一个模板。


c)您可能希望让代码知道地图模板中的分配器和比较

typename参数。即,


模板< typename S,typename T,typename A,typename C>

std :: ostream&运营商LT;< (std :: ostream& ostr,

const std :: multimap< S,T,A,C& v);


Best


Kai-Uwe Bux

a) technically, you want something like:

template < template <class, class class MAP, typename T1, typename T2 >

Read up on template template parameters (as opposed to typename template
parameters).

b) Using a template template parameter here, is a BadIdea(tm) since your
template would match too many things, e.g., it would match a pair<S,T>. In
that case, your code will not work.

Thus: you should just do another template for multimaps.

c) You may want to make your code aware of the Allocator and Comparison
typename parameters in the map template. I.e.,

template< typename S, typename T, typename A, typename C >
std::ostream & operator<< ( std::ostream & ostr,
const std::multimap< S, T, A, C & v );

Best

Kai-Uwe Bux


7月29日下午4:00,ozizus< o ... @ ce.yildiz。 edu.trwrote:
On Jul 29, 4:00 pm, ozizus <o...@ce.yildiz.edu.trwrote:

我重载了运算符<<对于STL地图成功:


模板< typename T1,typename T2ostream&运算符<< (ostream&

o,地图< T1,T2& m)

{

//代码


}


代码就像魅力一样。现在,我想要

multimap的相同功能。由于他们的界面对于手头的问题是相同的,我想要使集合名称成为这样的模板参数:


模板< typename MAP,typename T1,typename T2ostream&运营商

<< (ostream& o,MAP< T1,T2& m)

{

//代码


}


这样的编译器错误:语法错误:在''之前缺少'''''<''


thx,

ozi。
I overloaded operator << for STL map successfully:

template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code

}

the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
want to make collection name a template parameter like this:

template <typename MAP, typename T1, typename T2ostream & operator
<< (ostream & o, MAP<T1,T2& m)
{
//code

}

The compiler errors like this: syntax error : missing '')'' before ''<''

thx,
ozi.



你可以写简单:


模板< typename MapT>

ostream&运算符<< (ostream& o,const MapT& m){

typedef typename MapT :: Key_type T1;

typedef typename MapT :: mapped_type T2;

//代码


返回o;

};

如果您的编译器支持概念,事情会更容易:


自动概念map_like< MapT> {//什么(多)地图看起来像。

typedef typename MapT :: key_type key_type;

typedef typename MapT :: mapped_type mapped_type;

//等:


};


模板< map_like MapT> //参数类型必须像地图一样

ostream&运算符<< (ostream& o,const MapT& m){

//代码


返回o;

};

问候,

FM。

you can simply write:

template<typename MapT>
ostream & operator << (ostream & o, const MapT& m){
typedef typename MapT::Key_type T1;
typedef typename MapT::mapped_type T2;
//code

return o;
};
if your compiler supports concepts ,things will be easier :

auto concept map_like <MapT>{//what a (multi)map looks like.
typedef typename MapT::key_type key_type;
typedef typename MapT::mapped_type mapped_type;
//etc:

};

template<map_like MapT>//argument type must behave like a map
ostream & operator << (ostream & o, const MapT& m){
//code

return o;
};
regards,
FM.


这篇关于重载&lt;&lt;同时用于地图和多图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆