第11章 關(guān)聯(lián)容器

練習(xí)11.1:描述map和vector的不同

  • map是關(guān)聯(lián)容器傲茄,可以存放關(guān)鍵字-值毅访,還能排序
  • vector是順序容器,能存放單一的數(shù)據(jù)盘榨,按照放進(jìn)去的順序

練習(xí)11.2:分別給出最適合使用list喻粹、vector、deque草巡、map以及set的例子

  • list 需要在中間進(jìn)行很多的插入和刪除操作守呜。
  • vector 不太需要插入和刪除,需要快速查找
  • deque 頭尾刪除和插入山憨。需要快速查找
  • map 字典
  • set 黑名單

練習(xí)11.4:擴(kuò)展你的程序查乒,忽略大小寫和標(biāo)點(diǎn)。例如郁竟,“example.”也要遞增相同的計(jì)數(shù)器

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <cctype>

using namespace std;

int main()
{
    std::map<std::string, size_t> word_count;
    std::string word;
    while (std::cin >> word)
    {
        transform(word.begin(), word.end(), word.begin(), ::tolower);//如果寫了第7行玛迄,這里直接寫tolower會(huì)報(bào)錯(cuò),
        word.erase(remove_if(word.begin(), word.end(), (int(*)(int))ispunct), word.end());//兩個(gè)方法枪孩,具體在http://www.360doc.com/content/15/0825/17/14679766_494673239.shtml
        ++word_count[word];
    }
    for (const auto &i : word_count)
        std::cout << i.first << " " << i.second << std::endl;

    return 0;
}

練習(xí)11.5:解釋map和set的區(qū)別憔晒。你如何選擇使用哪個(gè)?

  • map存放key,和value
  • set只有key
    練習(xí)11.7:定義一個(gè)map關(guān)鍵字是家庭的姓蔑舞,值是一個(gè)vector拒担,保存家中孩子們的名字。編寫代碼攻询,實(shí)現(xiàn)添加新的家庭以及向已有家庭添加新的孩子
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main()
{
    map<string, vector<string>> familychild = {
                                                {"Joyce", {"James"}}
                                                };
    string firstName;
    string childname;
    while (cin >> firstName)
    {
        cin >> childname;
        familychild[firstName].push_back(childname);
    }
    
    for (const auto &i : familychild)
    {
        cout << i.first << endl;
        for (const auto &j : i.second)
            cout << j << " ";
        cout << endl;        
    }


    return 0;
}

練習(xí)11.12:編寫程序从撼,讀入string和int的序列,將每個(gè)string和int存入一個(gè)pair中钧栖,pair保存在一個(gè)vector中

#include <iostream>
#include <utility>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<pair<string, int>> strInt;
    string word;
    int num;
    pair<string, int> temp;
    while (cin >> word)
    {
        cin >> num;
        temp = make_pair(word, num);
        strInt.push_back(temp);
    }
    for (auto &i : strInt)
    {
        cout << i.first << " " << i.second;
    }

    return 0;
}

練習(xí)11.14:擴(kuò)展你在11.2.1節(jié)練習(xí)中編寫的孩子姓到名的map低零,添加一個(gè)pair的vector,報(bào)錯(cuò)孩子的名和生日拯杠。

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main()
{
    map<string, vector<pair<string, string>>> familyName;
    string name;
    string fname;
    string birthday;

    cout << "Please enter second name" << endl;    
    while (cin >> name)
    {
        cout << "Please enter first name" << endl;
        cin >> fname;
        cout << "Please enter birthday" << endl;
        cin >> birthday;
        familyName[name].push_back(make_pair(fname, birthday));
        cout << "Please enter second name" << endl; 
    }

    for (auto &i : familyName)
    {
        for (auto &j : i.second)
            cout << i.first << " "<< j.first << " " <<j.second << endl;
    }
    

    return 0;
}

練習(xí)11.15:對(duì)一個(gè)int到vector<int>的map掏婶,其mapped_type、key_type和value_type分別是什么潭陪?

  • mapped_type是vector<int>
  • key_type是int
  • value_type是pair<int, vector<int>>

練習(xí)11.20:重寫單詞計(jì)數(shù)程序雄妥,使用insert代替下標(biāo)

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    string word;
    map<string, size_t> words;
    while (cin >> word)
    {
        auto ret = words.insert({word, 1});
        if (!ret.second)
        {
            ++ret.first->second;
        }
        
    }
    for (auto &i : words)
        cout << i.second << " ";

    return 0;
}

練習(xí)11.21:假定word_count是一個(gè)string到size_t的map最蕾,word是一個(gè)string,解釋下面循環(huán)的作用

while (cin >> word)
        ++word_count.insert({word, 0}).first->second;
/*
    這...我能說(shuō)什么老厌,牛逼大哥就是大哥
首先加入新的元素時(shí):
        執(zhí)行插入瘟则,插入成功,然后用insert的返回執(zhí)行后面的first最后找到元素自增枝秤,相當(dāng)于第一次插入醋拧,并且元素是1
當(dāng)存在時(shí):
      執(zhí)行插入,發(fā)現(xiàn)存在了淀弹,但是依舊可以自增丹壕。
*/

練習(xí) 11.31:編寫程序,定義一個(gè)作者以及其作品的multimap薇溃。使用find中一個(gè)元素并用erase刪除它雀费。確保你的程序在元素不在map中時(shí)也能正常運(yùn)行,并且打印

#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    multimap<string, string> authorBook = {
            {"Tome", "C"},
            {"Jerlu", "P"},
            {"Zero", "H"},
            {"Rong", "Hot active"},
            {"Tome", "Hot action"}
    };
    string temp = "Tomde";
    auto entries = authorBook.count(temp);
    auto ret = authorBook.find(temp);
    while (entries)
    {
        ret = authorBook.erase(ret);
        --entries;
    }
    for (auto i : authorBook)
        cout << i.first << " " << i.second << endl;

    return 0;
}

練習(xí)11.35:在buildMap中痊焊,如果進(jìn)行如下改寫盏袄,會(huì)有什么效果?
trans_map[key] = value.substr(1)
改為trans_map.insert({key, value.substr(1)})

  • 第一種:如果有重復(fù)的關(guān)鍵字會(huì)按照最后一個(gè)
  • 改后:即使是重復(fù)的薄啥,也是用的是第一個(gè)

練習(xí)11.38:用unnordered_map重寫單詞計(jì)數(shù)程序與單詞轉(zhuǎn)換程序辕羽。
單詞轉(zhuǎn)換

#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

void word_transform(ifstream &map_file, ifstream &input);
unordered_map<string, string> bulidMap( ifstream &map_file);
const string &
transform(const string &s, const unordered_map<string, string> &m);

int main()
{
    ifstream map_file("1.txt");
    ifstream input("2.txt");
    word_transform(map_file, input);

    return 0;
}


void word_transform(ifstream &map_file, ifstream &input)
{
    auto trans_map = bulidMap(map_file);
    string line;
    while (getline(input, line))
    {
        istringstream stream(line);
        string word;
        bool firstword = true;
        while (stream >> word)
        {
            if (firstword)
                firstword = false;
            else
                cout << " ";
            cout << transform(word, trans_map);
        }
        cout << endl;
    }
    
}

unordered_map<string, string> bulidMap( ifstream &map_file)
{
    unordered_map<string, string> trans_map;
    string key;
    string value;
    while (map_file >> key && getline(map_file, value))
    {
        if (value.size() > 1)
            trans_map[key] = value.substr(1);
        else
            throw runtime_error("no rule for " + key);
    }

    return trans_map;
}

const string &
transform(const string &s, const unordered_map<string, string> &m)
{
    auto ret = m.find(s);
    if (ret != m.cend())
    {
        return ret->second;
    }
    else
        return s;
    
}

單詞計(jì)數(shù)

#include <iostream>
#include <unordered_map>
#include <string>

using namespace std;

int main()
{
    unordered_map<string, size_t> word_count;
    string word;
    while (cin >> word)
    {
        ++word_count[word];
    }
    
    for (auto &i : word_count)
        cout << i.first << " " << i.second << endl;
        
    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末野宜,一起剝皮案震驚了整個(gè)濱河市干花,隨后出現(xiàn)的幾起案子炊林,更是在濱河造成了極大的恐慌蔼夜,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件用狱,死亡現(xiàn)場(chǎng)離奇詭異笔呀,居然都是意外死亡偶器,警方通過(guò)查閱死者的電腦和手機(jī)觉壶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門脑题,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人铜靶,你說(shuō)我怎么就攤上這事叔遂。” “怎么了争剿?”我有些...
    開(kāi)封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵已艰,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我蚕苇,道長(zhǎng)哩掺,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任涩笤,我火速辦了婚禮嚼吞,結(jié)果婚禮上幔嫂,老公的妹妹穿的比我還像新娘。我一直安慰自己誊薄,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布锰茉。 她就那樣靜靜地躺著呢蔫,像睡著了一般。 火紅的嫁衣襯著肌膚如雪飒筑。 梳的紋絲不亂的頭發(fā)上片吊,一...
    開(kāi)封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音协屡,去河邊找鬼俏脊。 笑死,一個(gè)胖子當(dāng)著我的面吹牛肤晓,可吹牛的內(nèi)容都是我干的爷贫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼补憾,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼漫萄!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起盈匾,我...
    開(kāi)封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤腾务,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后削饵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體岩瘦,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年窿撬,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了启昧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡劈伴,死狀恐怖箫津,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情宰啦,我是刑警寧澤苏遥,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站赡模,受9級(jí)特大地震影響田炭,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜漓柑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一教硫、第九天 我趴在偏房一處隱蔽的房頂上張望叨吮。 院中可真熱鬧,春花似錦瞬矩、人聲如沸茶鉴。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)涵叮。三九已至,卻和暖如春伞插,著一層夾襖步出監(jiān)牢的瞬間割粮,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工媚污, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舀瓢,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓耗美,卻偏偏與公主長(zhǎng)得像京髓,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子商架,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容