map 小模板~~~ 寫的不好 繼續添加

來源:互聯網
上載者:User

標籤:os   io   資料   for   ar   size   ios   ef   

#include<map>
#include<string.h>
#include<iostream>
using namespace std;

int main()
{
    ///map插入
    map<string,int> mp;   ///<key值 val值>
    mp["a"]=1;
    mp["b"]=2;
    mp["c"]=3;
    map<string,int>::iterator it;
    for(it=mp.begin(); it!=mp.end(); ++it)
        cout<<"key: "<<it->first <<" value: "<<it->second<<endl;
    return   0;

    ///mapde大小
    int len=mp.size();

    ///資料清空與判空
    mp.clear();
    mp.empty();   ///if空了 返回true     else return false

    ///map的刪除 erase函數
    for (iter = mm.begin(); iter != mm.end();) ///如果沒有else部分 iter刪除後之後的for迴圈裡就會亂了
    {
        if ( iter->second == something )
            mm.erase( iter++ );
        else
            iter++;   // Use Pre Increment for efficiency.

    }
    ///等同於
    for (iter = mm.begin(); iter != mm.end();)
    {
        if ( iter->second == something )
        {
            map<string, string>::iterator iter_erase;
            iter_erase = iter++ //先儲存後++再刪除
            mm.erase( iter_erase );
        }

        else
            iter++;   // Use Pre Increment for efficiency.
    }
    
    ///map排序
 
    map<int,Student> stu_map;///升序排列
    ///如果要降序  則
    map<int,Student> stu_map;改為
    map<int,Student, greater<int> > stu_map;
    ///以及
    map<int,Student>::iterator iter;改為
    map<int,Student, greater<int> >::iteratoriter;
    即可。
    其實,map<int,Student> stu_map;這是一種預設的情況,它和
    map<int,Student, less<int> > stu_map;是一樣的。

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.