C++ Primer 11.2.3节编程练习


编写程序,读入string和int的序列,将每个string和int存入一个pair中,pair保存在一个vector中

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main()
{
    pair sipair;
    vector> sipvector;
    string str;
    int num;
    while(cin >> str >> num){
        sipair.first = str;
        sipair.second = num;
        sipvector.push_back(sipair);
    }
    for(auto i: sipvector){
        cout << i.first << " " << i.second;
        cout << endl;
    }
    return 0;
}

在上一题的程序中,至少有三种创建pair的方法,编写此程序的三个版本,分别采用不同的方法创建pair

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;
using namespace placeholders;

int main()
{
    pair sipair;
    vector> sipvector;
    string str;
    int num;
    while(cin >> str >> num){
        //pair sipair(str, num); 第一种
        //pair sipair = {str, num}; 第二种
        //sipair = make_pair(str, num); 第三种
        sipair.first = str;
        sipair.second = num;
        sipvector.push_back(sipair);
    }
    for(auto i: sipvector){
        cout << i.first << " " << i.second;
        cout << endl;
    }
    return 0;
}

扩展你在11.2.1节练习中编写的孩子姓名的map,添加一个pair的vector,保存孩子的名和生日

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main()
{
    map> family;
    vector> name_birthday;
    pair nb;
    cout << "New family or new child? f/c" << endl;
    string option, family_name, birthday;
    while(cin >> option){
        if(option=="f"){
            cout << "Please enter family name:" << endl;
            cin >> family_name;
            family[family_name];
        }else if(option=="c"){
            cout << "Which family?" << endl;
            cin >> family_name;
            string child_name;
            cout << "Please enter child's name:" << endl;
            cin >> child_name;
            family[family_name].push_back(child_name);
            nb.first = child_name;
            cout << "Please enter child's birthday:" << endl;
            cin >> birthday;
            nb.second = birthday;
            name_birthday.push_back(nb);
        }else{
            cerr << "Please enter correct option!" << endl;
        }
        cout << "New family or new child? f/c" << endl;
    }
    for(auto i=family.begin();i!=family.end();++i){
        cout << (*i).first << " Family members: ";
        for(auto j=(*i).second.begin();j!=(*i).second.end();++j){
            cout << *j << " ";
        }
        cout << endl;
    }
    for(auto i: name_birthday){
        cout << i.first << "'s birthday is: " << i.second << endl;
    }
    cout << endl;
    return 0;
}

文章作者: Qin Jiahe
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Qin Jiahe !
评论
 上一篇
MAMP无法打开Apache服务器 MAMP无法打开Apache服务器
最近装了一个MAMP打算学一下MySQL,结果打开发现Apache服务器不启动,折腾了一番后发现在D:\MAMP\logs下的日志文件apache_error.log里的报错: [Sat Jul 17 00:04:40 2021] [war
2021-07-17 Qin Jiahe
下一篇 
C++ Primer 11.2.1节编程练习 C++ Primer 11.2.1节编程练习
练习11.7 定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子(们)的名,编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子#include #include #include #include #include #
2020-07-22
  目录