Boolan STL与泛型编程第三周笔记

Posted dana-coder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Boolan STL与泛型编程第三周笔记相关的知识,希望对你有一定的参考价值。

对于本周老师讲的内容,C++ Primer上面有第十一章关联容器的习题可以做。有一些地方还是不懂,搞明白了再写上来。

P422 E11.4
//P422 E11.4
#include<string>
#include<map>
#include<iostream>
using namespace std;

//要在一个容器中删除元素,好的办法是从后往前遍历,被删除元素后面的元素会移动到被删除的位置,
//这样不用像18行那样移动迭代器
string &process(string &s)
{
    //原来我把i的类型写成int, 有warning 有符号/无符号类型不匹配
    for (string::size_type i = 0; i < s.size(); i++)
    {
        if (s[i] >= A && s[i] <= Z)
            s[i] -= (A - a);
        else if (s[i] == , || s[i] == .)
        {
            //这一步本身已经实现了题目的要求,但是要处理",Mac"成为"mac",须在此步后加i--
            //erase()使i到达M,i++跳过M,就无法处理了
            s.erase(i, 1);
            i--;
        }
    }
    return s;
    /* 
    //原来我写的代码
    if (s.at(0) >= ‘A‘ && s.at(0) <= ‘Z‘)
    {
        s.at(0) += ‘A‘ - ‘a‘;//这里写错了,应该是-=
    }
    if (s.find(‘,‘))
    {
        s.erase(‘,‘);
    }
    if (s.find(‘.‘))
    {
        s.erase(‘.‘);
    }
    */
    
}
int main()
{
    string word;
    map<string, size_t> word_count;
    while (cin >> word)
    {
        process(word);
        ++word_count[word];
    }
    for (const auto &s : word_count)
    {
        cout << s.first << " " << s.second << endl;
    }

    system("pause");
    return 0;
}

P424 E11.7

//P424 E11.7
#include<map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;

void add_family(map<string, vector<string>> &families, const string &family)
{
    families[family];
}

void add_child(map<string, vector<string>> &families, const string &family, const string &child)
{
    families[family].push_back(child);
}
void print(map<string, vector<string>> &_families)
{
    
    for (const auto f : _families)
    {
        cout << f.first << ": " << endl;
        for (const auto &f : f.second)            
        {
            cout << f << " "; 
        }
        cout << endl;
        
    }
    
}
int main()
{
    map<string, vector<string>> families;
    /*
    add_family(families, "Joyce");
    add_child(families, "Joyce", "James");
    add_child(families, "Joyce", "Jack");
    add_child(families, "Austen", "Browning");
    add_child(families, "Austen", "Jane");
    */
    
    while (cin)
    {
        cout << "请输入姓:" << endl;
        string family_name;
        cin >> family_name;

        add_family(families, family_name);
        cout << "请输入名:" << endl;
        string first_name;
        

        cin >> first_name;
    }

    print(families);
    cout << endl;

    system("pause");
    return 0;
}

P426 E11.9

//P426 E11.9
#include<list>
#include<map>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
//第二遍做对了,看了答案有了想法
int main()
{
    map<string, list<int>> word_lineno;
    string word;
    int line = 0;
    while (cin >> word)
    {
        word_lineno[word].push_back(line++);
    }

    for (const auto &w : word_lineno)
    {
        cout << w.first << ":" << endl;
        for (const auto &i : w.second)
        {
            cout << i << " ";
        }
        cout << endl;
    }

    system("pause");
    return 0;
}

P428 E11.12

//P428 E11.12
#include <utility>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
//学习新的输入方式
int main()
{
    string s;
    int i;
    vector<pair<string, int>> vec;
    while (cin >> s && cin >> i)//新的输入方式
        //E11.13 vec.push_back({s,v});
        //vec.push_back(pair<string, int>(s,v));
        vec.push_back(make_pair(s, i));

    for (const auto &v : vec)
    {
        cout << v.first << " " << v.second << endl;
    }

    system("pause");
    return 0;
}

P428 E11.14

//P428 E11.14
#include<map>
#include <utility>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
//看注释复习
void add_family(map<string, vector<pair<string, string>>> &families, const string &family)
{
    families[family];
}

void add_child
(map<string, vector<pair<string, string>>> &families, const string &family, const string &child, const string &birth)
{
    families[family].push_back((child, birth));//注意pair的构造方式{},(),make_pair()
}
void print(map<string, vector<pair<string, string>>> &_families)
{
    for (const auto f : _families)
    {
        //我原来一直不知道map里的key怎么输出:map里面是一个pair,pair是一个pair
        cout << f.first << ": " << endl;
        for (const auto &c : f.second)
        {
            cout << c.first << "-" << c.second << " ";
        }
        cout << endl;
    }

}
int main()
{
    map<string, vector<pair<string,string>>> families;
    add_family(families, "Joyce");
    add_child(families, "Joyce", "James", "1890-1-1");
    add_child(families, "Joyce", "Jack", "2980-2-3");
    add_child(families, "Austen", "Browning", "1234-2-3");
    add_child(families, "Austen", "Jane", "1212-3-4");
    print(families);
    cout << endl;

    system("pause");
    return 0;
}

P434 E11.20

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

int main()
{
    string word;
    map<string, int> word_count;
    while (cin >> word)
    {
    //用下标简单些
    //++word_count[word];

    //E11.21这个语句的作用跟我的程序一样
    //++word_count.insert({ word, 0 }).first->second;

    /* 用insert复杂一些
        auto ret = word_count.insert({word, 1 });
        if (!ret.second)
            ++ret.first->second;
    */

    }
    for (const auto &w : word_count)//A map is a COLLECTION of pairs.
    {
        cout << w.first << " " << w.second << endl;
    }

    system("pause");
    return 0;
}

 

以上是关于Boolan STL与泛型编程第三周笔记的主要内容,如果未能解决你的问题,请参考以下文章

Boolan 第三周笔记

[GeekBand] STL与泛型编程

模板与泛型编程

c++标准容器库与泛型编程

c++标准容器库与泛型编程

c++标准容器库与泛型编程