简体   繁体   English

映射插入与对类型

[英]map insert with pair type

While inserting an element in a map of type <int,int> , we have to explicitly mention the types again as pair. <int,int>类型的映射中插入元素时,我们必须再次明确地将类型提及为pair。 Isnt is redundant? 不是多余的吗?

map<int,int> m1;
//m1.insert(1,1); //NOT OK since the pair type <int,int> not mentioned
m1.insert(pair<int,int>(1,1)); //OK

m1 is declared as <int,int> . m1声明为<int,int> Would there be any instance wherein we try to insert any element other than <int,int> eg m1.insert(pair<int,string>(1,"boo")) ? 是否有任何实例,其中我们尝试插入除<int,int>之外的任何元素<int,int> eg m1.insert(pair<int,string>(1,"boo")) If no, then isn't it redundant to write <int,int> again while inserting an element? 如果不是,那么在插入元素时再次写<int,int>是不是多余的?

EDIT 1: 编辑1:

To explain in detail here is a small example: 这里详细解释一个小例子:

template<typename T1,typename T2>
class Test
{
public:
    template<typename T1>
    void fun1()
    {
        cout<<"This is called for T1 templatized fun1"<<endl;
    }
    template <typename T1,typename T2>
    void insert(pair<T1,T2> &obj)
    {
        cout<<obj.first<<" "<<obj.second<<endl;     
    }
};

int main()
{
    Test <int,int>obj; // Even though i have declared obj as type int,int, i am still able to call insert on type int,string
    obj.insert(pair<int,string>(1,"Anurag"));

Here we clearly see that the types with which i created object obj is different than the types with which i called insert() . 在这里我们清楚地看到我创建对象obj的类型与我调用insert()的类型不同。 But i dont understand how would the member function map::insert() make sure that the types are same as those with which the object is created? 但我不明白成员函数map :: insert()如何确保类型与创建对象的类型相同? One way that i thought of was: 我想到的一种方式是:

template <typename T3=T1,typename T4=T2> //where T1 and T2 are class typenames
void insert2(pair<T3,T4>& obj2)
{
    cout<<"Inside insert2 \n";
}

But even this would not be allowed since this is a function template not a class template. 但即使这样也不允许,因为这是一个函数模板而不是类模板。 I tried looking inside the header file of map to see the declaration of insert but got more confused. 我试着查看map的头文件,看看insert的声明但是更加困惑。

insert is not a forwarding function. insert不是转发功能。 Just use brackets to initialize the pair object: 只需使用括号初始化对象:

m1.insert({1, 1});

In C++11, emplace will forward the arguments. 在C ++ 11中, emplace将转发参数。

m1.emplace(1, 1);

Or in C++03, make_pair . 或者在C ++ 03中, make_pair


Regarding your edit: 关于你的编辑:

That's a pretty inaccurate representation of map. 这是一个非常不准确的地图表示。 A more accurate representation would be something like this: 更准确的表示将是这样的:

template <typename Key, typename T,
          class Compare = std::less<Key>,
          class Allocator = std::allocator<std::pair<const Key, T>>>
struct Test
{
    using value_type = std::pair<const Key, T>;
    using iterator   = typename std::allocator_traits<Allocator>::pointer;

    std::pair<iterator, bool> insert( const value_type& value )
    {
        // this calls emplace
    }
};

int main()
{
    Test<int, int> test;
    test.insert(std::pair<int, std::string>(1, "hello"));
}

Which does give a compiler error. 确实给出了编译器错误。 Of course, this is why the convenience function std::make_pair was provided in the first place. 当然,这就是首先提供便利功能std::make_pair原因。

It's redundant, but that't the way it is. 这是多余的,但事实并非如此。 However, you can insert elements without having to respecify <int,int> . 但是,您可以插入元素而无需重新指定<int,int>

This way, compiler is smart enough to figure out template arguments of the make_pair function: 这样,编译器足够聪明,可以找出make_pair函数的模板参数:

m1.insert(make_pair(1,1)); // equivalent o m1.insert(make_pair<int,int>(1,1));

or that way: 或那样:

m1[1] = 1;

The thing is: when you declare an template variable ( pair is a type, so pair<int,int>( 1, 1 ) is a template variable declaration), template parameters must be specified. 问题是:当你声明一个模板变量( pair是一个类型,所以pair<int,int>( 1, 1 )是一个模板变量声明)时,必须指定模板参数。 But, when you call a function ( make_pair is actually a function), template parameters are optional, compiler will try to determine them and it will only complains if it fails to. 但是,当你调用一个函数( make_pair实际上是一个函数)时,模板参数是可选的,编译器将尝试确定它们,并且只有在它失败时它才会抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM