当前位置: 首页 > article >正文

pair类型应用举例

在main.cpp里输入程序如下:

#include <iostream>  //使能cin(),cout();

#include <utility>   //使能pair数据类型;

#include <string>    //使能string字符串;

#include <stdlib.h>  //使能exit();

 

//pair类型可以将两个相同的或不同类型的变量,关联在一起;

//在使用pair数据类型前,要应用utility头文件;

using namespace std;

 

unsigned char product_number;

int main()

{

    pair <string,double> product1 ("白菜",3.25);

//声明pair数据类型的对象product1,并且product1.first="白菜",

//product1.second=3.25;

    pair <string,double> product2;  //声明pair数据类型的对象product2;

    pair <string,double> product3;  //声明pair数据类型的对象product3;

    pair <string,string> product4;  //声明pair数据类型的对象product4;

    product2.first = "豆腐";     //为product2.first赋值;

    product2.second = 1.99;      //为product2.second赋值;

    product3 = make_pair ("排骨",20.50);

//使用make_pair()为pair数据类型的对象product3d赋值;

    product4 = make_pair ("购物袋:   1个","备注: 3种以上商品");

//使用make_pair()为pair数据类型的对象product4赋值;

    product_number=0;

    if(product1.first=="白菜")

    {cout << product1.first << ":  " << product1.second << "元/公斤\n";

       product_number++;

    }

    if(product2.first=="豆腐")

    {cout << product2.first << ":  " << product2.second << "元/公斤\n";

       product_number++;

    }

    if(product3.first=="排骨")

    {cout << product3.first << ":  " << product3.second << "元/公斤\n";

       product_number++;

    }

    if(product_number>=3)

    {cout << product4.first << "\n" << product4.second << "\n";

    }

    exit(0); //退出程序;

}


http://www.kler.cn/news/366042.html

相关文章:

  • uniapp 引入了uview-ui后,打包错误,主包过大解决方案
  • fpga开发环境总结
  • Java应用程序的测试覆盖率之设计与实现(三)-- jacoco cli 客户端
  • HTTP和HTTPS基本概念,主要区别,应用场景
  • 庐阳区2023年信息学竞赛试题
  • 海港[NOIP2016]
  • C++ 算法学习——1.3 Prim算法
  • python操作CSV和excel,如何来做?
  • <项目代码>YOLOv8煤矿输送带异物识别<目标检测>
  • 安装Vue CLI的详细指南
  • 数据采集与数据分析:数据时代的双轮驱动
  • 零基础Java第十期:类和对象(一)
  • Mybatis mapper文件 resultType和resultMap的区别
  • 电脑重做系统后打游戏很卡
  • 循序渐进丨MogDB 与 PostgreSQL 对比测试IPv6
  • Flask-SocketIO 简单示例
  • unity游戏开发之塔防游戏
  • LinkAndroid v0.0.12 发布,手机连接助手,日志查看、投屏设置、多处问题修复
  • 光控资本:养老金融建设提速 高速铜缆市场空间广阔
  • 【工作技术栈】通用的旁路缓存一致性缺陷以及解决方式
  • ERR_PNPM_LINKING_FAILED Error: EPERM: operation not permitted, rename...
  • Scaffold-GS: Structured 3D Gaussians for View-Adaptive Rendering
  • 【python】OpenCV—findContours(4.2)
  • 【Go语言】
  • 简述特征降维的几种方式
  • IDEA中一个窗口打开多个项目-区别于eclipse