C++ //练习 11.12 编写程序,读入string和int的序列,将每个string和int存入一个pair中,pair保存在一个vector中。

C++ Primer(第5版) 练习 11.12

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

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex11.12.cpp
	> Author: 
	> Mail: 
	> Created Time: Wed 03 Apr 2024 10:11:20 PM CST
 ************************************************************************/

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

int main(){
    vector<pair<string, int>> list;
    string str;
    int num;
    cout<<"Enter string and num:"<<endl;
    while(cin>>str>>num){
        list.push_back(make_pair(str, num));
    }

    cout<<endl;
    cout<<"Result: "<<endl;
    for(const auto l : list){
        cout<<setw(8)<<left<<l.first<<" "<<l.second<<endl;
    }

    return 0;
}
运行结果显示如下
相关推荐
Jerry10 小时前
LeetCode 28. 找出字符串中第一个匹配项的下标
算法
Jerry11 小时前
LeetCode 459. 重复的子字符串
算法
哥不想学算法14 小时前
【C++】字符串字面量拼接
开发语言·c++
海石14 小时前
1500分的题目,确实有实力,不过还是我略胜一筹
算法·leetcode
海石14 小时前
【记忆化搜索】条条大路通AC,走好适合你的那一条,走到后再考虑走得快
算法·leetcode
gugucoding16 小时前
21. 【C语言】打包不同类型:结构体
c语言·开发语言
Brookty16 小时前
【JavaEE】线程安全(一).4:写块串行保安全、CAS
java·开发语言·java-ee·多线程·线程安全
Jerry16 小时前
LeetCode 151. 反转字符串中的单词
算法
F202269748616 小时前
西门子 PLC 与 C# 通信
开发语言·c#