如何将地图转换为集合 [英] how to convert map into set

查看:65
本文介绍了如何将地图转换为集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将地图转换为集合时遇到了一些问题 我有一个带有该成员数据的"Chanson"对象:

i got some issues trying to convert my map into a set I got a "Chanson" object with this member data :

std::map<std::string,Artiste*> m_interpretes;

这是我将*Artiste添加到地图中的方式:

Here is how i add my *Artiste to my map :

void Chanson::addArtiste(Artiste* a) throw (ExceptionArtiste, ExceptionLangueIncompatible)
{
    if(a!=NULL)
    {
        if(a->getLangue() == this->getLangue())
        {
            m_interpretes.insert(pair<string, Artiste*>(a->getNom(), a));
            //m_interpretes[a->getNom()] = a;
        }
        else
        {
            throw ExceptionLangueIncompatible(a,this);
        }
    }
}




set<Artiste*> Chanson::getArtistes() const
{
    //set<Artiste*> machin;
    return set<Artiste*> (m_interpretes.begin(), m_interpretes.end());
}

由于此功能,我收到此错误:

i got this error due to this function :

错误C2664:'std :: pair< _Ty1,_Ty2> std :: set< _Kty> :: insert(Artiste *&):无法从参数1转换为std :: pair< _Ty1, _Ty2> zh_Artiste *&&' c:\ program files(x86)\ Microsoft Visual Studio 11.0 \ vc \ include \ set 179 1

Error C2664: 'std::pair<_Ty1,_Ty2> std::set<_Kty>::insert(Artiste *&&) : impossible de convertir le paramètre 1 de const std::pair<_Ty1,_Ty2> en 'Artiste *&&' c:\program files (x86)\microsoft visual studio 11.0\vc\include\set 179 1

有什么想法要解决吗?

推荐答案

您要使用的std::set构造函数将尝试从传递范围的所有内容构造一个元素:

The std::set constructor you are trying to use will try to construct an element from everything the range you pass it:

return set<Artiste*> (m_interpretes.begin(), m_interpretes.end());

但是该范围的元素类型是

But the element type of that range is

std::pair<const std::string, Artiste*>

绝对不能转换为Artiste*,这就是为什么您收到有关无法转换的错误的原因.不过,您可以手动进行操作:

which is definitely not convertible to Artiste*, which is why you are getting that error about not being able to convert. You could just do it manually though:

std::set<Artiste*> s;
for (const auto& pair : m_interpretes) {
    s.insert(pair.second);
}

这篇关于如何将地图转换为集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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