C++ Primer 学习笔记 第十一章 关联容器

375 map函数的使用

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
using namespace std;


int main() {
    map<string, size_t> word_count;
    vector<string> vec = {"a", "a", "b", "c", "c", "d"};

    for (auto s:vec){
        ++word_count[s];
    }
    for (const auto &w:word_count){
        cout << w.first << " occours " << w.second << ((w.second>1)? " times": " time") << endl;

    }

    return 0;
    /*
        a occours 2 times
        b occours 1 time
        c occours 2 times
        d occours 1 time
     * */
}

375 加上set去除相关的值

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
using namespace std;


int main() {
    map<string, size_t> word_count;
    vector<string> vec = {"apple", "a", "apple", "strawberry", "a", "an"};
    set<string> exclude = {"a", "an"};

    for (auto s:vec){
        if (exclude.find(s)==exclude.end()) {
            // not found in exclude list
            ++word_count[s];
        }
    }
    for (const auto &w:word_count){
        cout << w.first << " occours " << w.second << ((w.second>1)? " times": " time") << endl;

    }

    return 0;
    /*
    apple occours 2 times
    strawberry occours 1 time
     * */
}

377 set multiset

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
using namespace std;


int main() {
    vector<int> ivec;
    for (vector<int>::size_type i=0; i!=10; ++i){
        ivec.push_back(i);
        ivec.push_back(i);
    }
    set<int> iset(ivec.begin(), ivec.end());
    multiset<int> miset(ivec.begin(), ivec.end());
    cout << ivec.size() << endl; //20
    cout << iset.size() << endl; //10
    cout << miset.size() << endl; //20

    return 0;

}

11.7

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    map<string, vector<string>> family;
    family["liang"] = vector<string>({"jxl", "zyl"});
    family["zhu"] = vector<string>({"jtz", "zyz"});
    ostream_iterator<string> iter(cout, " ");
    for (const auto &w:family){
        cout << "surname: " << w.first << " members: ";
        copy(w.second.begin(), w.second.end(), iter);
        cout << endl;
    }

    return 0;

}

11.12 pair的三种创建方式

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
#include <string>
using namespace std;


int main() {

    vector<string> svec = {"apple", "banana", "pairs", "cherry"};
    vector<int> ivec = {1, 2, 3, 4};
    vector<pair<int, string>> wvec;

    for (int i=0; i!=svec.size(); ++i){
        // method 1
//        wvec.push_back({ivec[i], svec[i]});
        // method 2
//        wvec.emplace_back(ivec[i], svec[i]);
        // method 3
        wvec.push_back(make_pair(ivec[i], svec[i]));
    }

    for (auto item:wvec){
        cout << "first: " << item.first << " second: " << item.second;
        cout << endl;
    }
    cout << endl;
    return 0;

}

11.14

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    map<string, vector<pair<string, string>>> family1;
    family1["liang"].emplace_back("liang", "1222");
    family1["chen"].emplace_back("zhu", "1123");
    for(const auto i:family1){
        cout << i.first;
        for (const auto j:i.second){
            cout << j.first << j.second;
        }
        cout << endl;
    }
    return 0;
}

382 map关联容器的迭代器

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
using namespace std;


int main() {
    map<string, size_t> word_count;
    vector<string> vec = {"apple", "a", "apple", "strawberry", "a", "an"};
    set<string> exclude = {"a", "an"};

    for (auto s:vec){
        if (exclude.find(s)==exclude.end()) {
            // not found in exclude list
            ++word_count[s];
        }
    }
    for (const auto &w:word_count){
        cout << w.first << " occours " << w.second << ((w.second>1)? " times": " time") << endl;

    }
    /*
    apple occours 2 times
    strawberry occours 1 time
     * */
    auto map_it = word_count.begin();
    cout << map_it->first;
    cout << " " << map_it->second << endl;
    ++map_it->second;
    cout << " " << map_it->second << endl;
    cout << (++map_it)->first<< " " << "second: " << (++map_it)->second<<endl;
    return 0;

}

382 遍历关联容器

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    map<string, size_t> word_count;
    set<string> exclude = {"a"};

    vector<string> ivec = {"apple", "banana", "apple", "a"};

    for (auto word:ivec){
        if (exclude.find(word)==exclude.end())
            ++word_count[word];
    }

    auto map_it = word_count.cbegin();
    while(map_it != word_count.cend()) {
        cout << map_it->first << " occours " << map_it->second << " times" << endl;
        ++map_it;
    }

    return 0;

}

11.16

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    map<string, int> mp;
    mp["person"] = 2;
    auto map_iter = mp.begin();
    map_iter->second++;
    cout << map_iter->second << endl;
    return 0;

}

384 关联容器插入元素的方法

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
vector<int> ivec = {2, 4, 6, 8, 2, 4, 6, 8};
set<int> set2;
set2.insert(ivec.cbegin(), ivec.cend());

    ostream_iterator<int> oiter(cout, " ");
    copy(set2.begin(), set2.end(), oiter);
    cout << endl;
    // 2 4 6 8
    set2.insert({1, 3, 5, 7, 1, 3, 5, 7});
    copy(set2.begin(), set2.end(), oiter);
    // 1 2 3 4 5 6 7 8
    cout << endl;

    map<string, size_t> word_count;
    string word = "c++ primer";
    word_count.insert({word, 1});
    word_count.insert({word, 1});
    auto pi = word_count.begin();
    cout << pi->first << " " << pi->second << endl;
    word_count.insert(make_pair(word, 1));
    word_count.insert(pair<string, size_t>(word, 1));
    word_count.insert(map<string, size_t>::value_type(word, 1));

    return 0;

}

385 insert的返回值

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {

    map<string, size_t> word_count;
    string word;
    while(cin >> word){
        auto ret = word_count.insert({word, 1});
        if (!ret.second){
            ++ret.first->second;
        }

        for (auto iter=word_count.begin(); iter!=word_count.end(); ++iter){
            cout << iter->first << ": " << iter->second << endl;
        }
    }
    return 0;

}

386

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {

    map<string, size_t> word_count;
    string word;
    while(cin >> word){
        auto ret = word_count.insert({word, 1});
        if (!ret.second){
            ++ret.first->second;
        }
        for (auto iter=word_count.begin(); iter!=word_count.end(); ++iter){
            cout << iter->first << ": " << iter->second << endl;
        }
    }
    cout << endl;

    multimap<string, string> authors;
    authors.insert({"ben", "start wars"});
    authors.insert({"ben", "x wars"});
    for (auto iter=authors.begin(); iter!=authors.end(); ++iter){
        cout << iter->first << ": " << iter->second << endl;
    }
    return 0;

}

386 关联容器删除元素

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {

    multimap<string, string> authors;
    authors.insert({"ben", "start wars"});
    authors.insert({"ben", "x wars"});
    for (auto iter=authors.begin(); iter!=authors.end(); ++iter){
        cout << iter->first << ": " << iter->second << endl;
    }
    /*
       ben: start wars
       ben: x wars
     * */

    string remove_word = "ben";
    size_t ret = authors.erase(remove_word);
    cout << "number of elements being erased: " << ret << endl; // 2
    if (ret)
        cout << "ok: " << remove_word << " removed\n";
    else
        cout << "oops: " << remove_word << "not found!\n";

    return 0;

}

389 访问元素

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    set<int> iset = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    iset.find(1);
    iset.find(11);
    iset.count(1);
    iset.count(11);

    map<string, int> word_count;
    if (word_count.find("foobar") == word_count.end())
        cout << "foobar is not in the map" << endl;

    return 0;

}

遍历multimap中的重复元素

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    multimap<string, string> authors;
    authors.emplace("libai", "abc");
    authors.emplace("libai", "efg");

    string search_item("libai");
    auto entries = authors.count(search_item);
    cout << "number of libai " << entries << endl;

    auto iter = authors.find(search_item);
    while(entries){
        cout << iter->second << endl;
        ++ iter;
        --entries;

    }
    return 0;
}

391 定位区间,查找关联容器中的元素

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    multimap<string, string> authors;
    authors.emplace("libai", "abc");
    authors.emplace("dufu", "efg");
    authors.emplace("libai", "hij");

    string search_item("libai");
    auto entries = authors.count(search_item);
    
    // lower_bound upper_bound配合定位到元素出现的区间
    for (auto beg=authors.lower_bound(search_item),
            end=authors.upper_bound(search_item); beg!=end; ++beg){
        cout << beg->second << endl;
    }
    // abc
    // hij


    // equal_range直接定位区间
    for (auto pos=authors.equal_range(search_item);pos.first!=pos.second;++pos.first){
        cout << pos.first->second << endl;
    }
    // abc
    // hij


    return 0;
}

11.28

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {

    map<string, int> word_count;
    word_count.insert({"apple", 1});
    word_count.insert({"orange", 1});

    string search_strr = "apple";
    map<string,int>::iterator ret = word_count.find(search_strr);
    cout << ret->first << endl;

    return 0;
}

11.31 map的两种遍历的方式

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
multimap<string, string> authors;
authors.emplace("libai", "abc");
authors.emplace("dufu", "efg");
authors.emplace("libai", "hij");
authors.emplace("baijuyi", "lmn");

string search_item("libai");

map<string,string>::iterator ret = authors.find("libai");

if (!(ret==authors.cend())){
authors.erase(search_item);
}

for (map<string, string>::const_iterator iter=authors.cbegin(); iter!=authors.cend(); ++iter){
cout << iter->first << " " << iter->second << endl;
}

for (pair<string, string> item:authors){
cout << item.first << " " << item.second << endl;

}


return 0;
}

11.32

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;


int main() {
    multimap<string, string> authors;
    authors.emplace("libai", "abc");
    authors.emplace("dufu", "efg");
    authors.emplace("libai", "hij");
    authors.emplace("baijuyi", "lmn");



    for (map<string, string>::const_iterator iter=authors.cbegin(); iter!=authors.cend(); ++iter){
        cout << iter->first << " " << iter->second << endl;
    }

    cout << endl;

    for (pair<string, string> item:authors){
        cout << item.first << " " << item.second << endl;

    }


    return 0;
}

392 关联容器综合练习,读入文本按照规则变换后输出

re.txt

k okay?
y why
r are
u you
pic picture
thk thanks!
18r later

input.txt

where r u
y dont u send me a pic
k thk 18r
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <set>
#include <iterator>
using namespace std;

map<string, string> buildMap(ifstream &map_file){
    map<string, string> trans_map;
    string line;
    while(getline(map_file, line)){
        auto pos = line.find(" ");

        if (!(pos == line.npos)) {
            string key(line.begin(), line.begin() + pos);
            string value(line.begin() + pos + 1, line.end());

            trans_map[key] = value;
        } else{
            throw runtime_error("no rule for " + line);
        }
    }
    return trans_map;
}


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


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


int main() {
    ifstream re("re.txt");
    ifstream input("input.txt");
    ofstream output("output.txt");
    word_transform(re, input, output);
    return 0;
}

394 使用无序容器

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <map>
#include <unordered_map>
#include <set>
#include <iterator>
using namespace std;

int main() {
    unordered_map<string, size_t> word_count;
    string word;
    while(cin>>word){
        ++word_count[word];
        for(const auto &w:word_count){
            cout << w.first << " occurs " << w.second << ((w.second>1)? " times":" time") << endl;
        }

    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值