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;
}
运行结果显示如下
相关推荐
Dxy12393102162 小时前
Python 使用正则表达式将多个空格替换为一个空格
开发语言·python·正则表达式
故事和你913 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__3 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__4 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
techdashen4 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰4 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君4 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社4 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
王老师青少年编程5 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程5 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果