multimap::insert (STL/CLR)--VS2010

菜鸟学习STL:看到一些教材上对multimap的插入函数是这样描述的:


元素的插入
*//typedef pair<const key,T> value_type;
*pair<iterator,bool> insert(const value_type& v);
*iterator insert(iterator pos,const value_type& v);
*void insert(first,last);

怀着强力的学习欲望对其进行了实现,结果第一种插入方式却怎么也不对,静下心来仔细看了下,应该不是自己的错误,仔细查询了下msdn文档才看出其中端倪,给大家分享下:


multimap::insert (STL/CLR)

Visual Studio 2010
此主题尚未评级 评价此主题

Adds elements.

    iterator insert(value_type val);
    iterator insert(iterator where, value_type val);
    template<typename InIter>
        void insert(InIter first, InIter last);
    void insert(System::Collections::Generic::IEnumerable<value_type>^ right);
first

Beginning of range to insert.

last

End of range to insert.

right

Enumeration to insert.

val

Key value to insert.

where

Where in container to insert (hint only).

Each of the member functions inserts a sequence specified by the remaining operands.

The first member function inserts an element with value val, and returns an iterator that designates the newly inserted element. You use it to insert a single element.

The second member function inserts an element with value val, using where as a hint (to improve performance), and returns an iterator that designates the newly inserted element. You use it to insert a single element which might be adjacent to an element you know.

The third member function inserts the sequence [firstlast). You use it to insert zero or more elements copied from another sequence.

The fourth member function inserts the sequence designated by the right. You use it to insert a sequence described by an enumerator.

Each element insertion takes time proportional to the logarithm of the number of elements in the controlled sequence. Insertion can occur in amortized constant time, however, given a hint that designates an element adjacent to the insertion point.

// cliext_multimap_insert.cpp 
// compile with: /clr 
#include <cliext/map> 
 
typedef cliext::multimap<wchar_t, int> Mymultimap; 
int main() 
    { 
    Mymultimap c1; 
    c1.insert(Mymultimap::make_value(L'a', 1)); 
    c1.insert(Mymultimap::make_value(L'b', 2)); 
    c1.insert(Mymultimap::make_value(L'c', 3)); 
 
// display contents " [a 1] [b 2] [c 3]" 
    for each (Mymultimap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert a single value, unique and duplicate 
    Mymultimap::iterator it = 
        c1.insert(Mymultimap::make_value(L'x', 24)); 
    System::Console::WriteLine("insert([L'x' 24]) = [{0} {1}]", 
        it->first, it->second); 
 
    it = c1.insert(Mymultimap::make_value(L'b', 2)); 
    System::Console::WriteLine("insert([L'b' 2]) = [{0} {1}]", 
        it->first, it->second); 
 
    for each (Mymultimap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert a single value with hint 
    it = c1.insert(c1.begin(), Mymultimap::make_value(L'y', 25)); 
    System::Console::WriteLine("insert(begin(), [L'y' 25]) = [{0} {1}]", 
        it->first, it->second); 
    for each (Mymultimap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert an iterator range 
    Mymultimap c2; 
    it = c1.end(); 
    c2.insert(c1.begin(), --it); 
    for each (Mymultimap::value_type elem in c2) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert an enumeration 
    Mymultimap c3; 
    c3.insert(   // NOTE: cast is not needed 
        (System::Collections::Generic:: 
            IEnumerable<Mymultimap::value_type>^)%c1); 
    for each (Mymultimap::value_type elem in c3) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
    return (0); 
    } 
 
 [a 1] [b 2] [c 3]
insert([L'x' 24]) = [x 24]
insert([L'b' 2]) = [b 2]
 [a 1] [b 2] [b 2] [c 3] [x 24]
insert(begin(), [L'y' 25]) = [y 25]
 [a 1] [b 2] [b 2] [c 3] [x 24] [y 25]
 [a 1] [b 2] [b 2] [c 3] [x 24]
 [a 1] [b 2] [b 2] [c 3] [x 24] [y 25]

Header: <cliext/map>

Namespace: cliext

即: multimap的insert方法返回的是一个迭代器iterator,没有bool值;而map值(iterator,bool)的元素对


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值