关闭

Map的插入操作和按照value排序

280人阅读 评论(0) 收藏 举报
分类:

转载:http://blog.csdn.net/ljp1919/article/details/51544496

背景

对比map的插入操作说明和按照value排序。

说明

常见的map插入操作有两种,一种是通过操作符[]进行插入,另一种则是通过insert成员函数进行插入。 
[]操作符进行插入。

[]操作符

示例:

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> map1;
map1[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>]=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Hello"</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

插入2时,先在map1中查找主键为2的项,没发现,然后将一个新的对象插入map1中,键是2,此时的值是一个空字符串,在插入完成后,将字符串赋为”Hello”。 该方法会将每个值都赋为缺省值,然后再赋为需要显示的值,如果元素是类对象,则开销比较大。[]操作,时间复杂度为logn,n为map的容量。 
用insert方法可以避免上述开销。 
此外需要注意的是,用下标访问map中不存在的元素时,将导致向map中添加该下标所指向的新元素,其值会根据类型默认值或默认构造函数进行初始化(如int或初始化为0,string初始化为“”)。

insert操作

insert的时候会检查是否key冲突,不冲突才进行insert操作,否则该insert操作会被忽略。可以用pair来验证是否插入成功。通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。

插入value_type数据

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> mapStudent;
mapStudent.insert(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::value_type (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, “student_one”));
mapStudent.insert(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::value_type (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, “student_two”));

<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::iterator,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span>> ret;
ret = mapStudent.insert ( <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"X man"</span>) );
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (ret.second==<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>) {
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"element '2' already existed"</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" with a value of "</span> << ret.first->second << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\n'</span>;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

插入pair数据

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> mapStudent;
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::iterator it =mapStudent.begin();
mapStudent.insert (it, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_2"</span>));  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// max efficiency inserting</span>
mapStudent.insert (it, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_3"</span>));  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// no max efficiency inserting</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

范围插入

用法void insert (InputIterator first, InputIterator last),以其他map的迭代器作为入参,将该范围内的数据插入到该map中,包括临界值。

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// third insert function version (range insertion):</span>
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> anothermap;
anothermap.insert(mapStudent.begin(),mapStudent.find(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>));</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>

综合,代码如下:

<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// first insert function version (single parameter):</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> mapStudent;
    mapStudent.insert(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::value_type (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_1"</span>));
    mapStudent.insert(<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::value_type (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_7"</span>));

    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::iterator,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span>> ret;
    ret = mapStudent.insert ( <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"X man"</span>) );
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (ret.second==<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">false</span>) {
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"element '2' already existed"</span>;
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" with a value of "</span> << ret.first->second << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\n'</span>;
    }

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// second insert function version (with hint position):</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span>::iterator it = mapStudent.begin();
    mapStudent.insert (it, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_2"</span>));  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// max efficiency inserting</span>
    mapStudent.insert (it, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::pair<<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>>(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"student_3"</span>));  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// no max efficiency inserting</span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// third insert function version (range insertion):</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>,<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>></span> anothermap;
    anothermap.insert(mapStudent.begin(),mapStudent.find(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>));

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// showing contents:</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"mapStudent contains:\n"</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (it=mapStudent.begin(); it!=mapStudent.end(); ++it)
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << it->first << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" => "</span> << it->second << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\n'</span>;

    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"anothermap contains:\n"</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (it=anothermap.begin(); it!=anothermap.end(); ++it)
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>::<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << it->first << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" => "</span> << it->second << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'\n'</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li></ul>

展示如下: 
这里写图片描述

map排序

map内部是根据键进行排序的,如果想要修改其排序对象呢,按照value进行排序呢? 
如果想要采用sort函数,该怎么办?sort函数有个限制,利用sort函数只能对序列容器进行排序,就是线性的(如vector,list,deque)。map也是一个集合容器,它里面存储的元素是pair,但是它不是线性存储的,所以利用sort不能直接和map结合进行排序。如果把map中的元素放到序列容器(如vector)中,然后再对这些元素进行排序呢?要对序列容器中的元素进行排序,也有个必要条件:就是容器中的元素必须是可比较的,也就是实现了<操作的。将map的元素以PAIR放置与vector中,再用sort函数,对其排序,其中需要指定排序方法。这里有两种方式, 比较函数和函数对象的方式。因为我们知道sort的定义如下:

<code class="hljs d has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">template</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> RandomAccessIterator>  
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> sort ( RandomAccessIterator first, RandomAccessIterator last );  

<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">template</span> <<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> RandomAccessIterator, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> Compare>  
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp ); </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

在使用过程中,我们需要自定义Compare。sort函数是在调用时指定的,可以传入函数指针或者函数对象。当传入一个对象,类名()就会调用构造函数生成对象,当传入一个函数指针,就是把上面说的第一种方法的函数名传过来。(注意,函数对象和函数指针的区别。但是,在程序代码中,它的调用方式与函数指针一样,后面加个括号就可以了,可以细看下面的程序。)

<code class="language-C++ hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<map></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<vector></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<string></span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<algorithm></span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> pair<<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>> PAIR;
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//实现对pair按value进行比较方法1:比较函数函数的方式</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> cmp_by_value(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> PAIR& lhs, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> PAIR& rhs) {  
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> lhs.second < rhs.second;  
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//实现对pair按value进行比较方法2:函数对象的方式</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> CmpByValue {  
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">bool</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">operator</span>()(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> PAIR& lhs, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> PAIR& rhs) {  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> lhs.second < rhs.second;  
  }  
};  
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main()
{
    <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">map</span><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">string</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>></span> name_score_map;  
    name_score_map[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"LiMin"</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span>;  
    name_score_map[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"ZiLinMi"</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">79</span>;  
    name_score_map[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"BoB"</span>] = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">92</span>;  
    name_score_map.insert(make_pair(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Bing"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">99</span>));  
    name_score_map.insert(make_pair(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Albert"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">86</span>));  
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//把map中元素转存到vector中   </span>
    <span class="hljs-stl_container" style="box-sizing: border-box;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">vector</span><PAIR></span> name_score_vec(name_score_map.begin(), name_score_map.end());  
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());//指定函数对象的方式,注意这里CmpByValue(),有个括号!</span>
    sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//指定比较函数的方式 </span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i != name_score_vec.size(); ++i)
    {  
        <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << name_score_vec[i].first<<<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"\t"</span><< name_score_vec[i].second<< endl;  
    }  
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
}
</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li></ul>

结果如下: 
两次排序分别对应函数指针和函数对象的方式。 
这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72926次
    • 积分:1845
    • 等级:
    • 排名:千里之外
    • 原创:96篇
    • 转载:107篇
    • 译文:1篇
    • 评论:9条
    最新评论