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

C++中tuple数据结构使用

目录

  • 1 基础知识
  • 2 模板
  • 3 工程化

1 基础知识

tuple是元组,一旦定义则不可修改,它可以存储一组不同类型的数据。它定义在头文件#include <tuple>中。

(一)
tuple变量的定义。

tuple<int, float, string> a = {1, 2.0, "three"};
tuple<int, float, string> b(1, 2.0, "three");
tuple<int, float, string> c = make_tuple(1, 2.0, "three");

(二)
tuple变量的访问。

int a1 = get<0>(a);
float a2 = get<1>(a);
string a3 = get<2>(a);

int b1;
float b2;
string b3;
tie(b1, b2, b3) =  b;

2 模板

#include <iostream>
#include <tuple>
#include <string>

using namespace std;

int main() {
    tuple<int, float, string> a = {1, 2.0, "three"};
    tuple<int, float, string> b(1, 2.0, "three");
    tuple<int, float, string> c = make_tuple(1, 2.0, "three");
    
    int a1 = get<0>(a);
    float a2 = get<1>(a);
    string a3 = get<2>(a);   
    cout << "a1 = " << a1 << ", a2 = " << a2 << ", a3 = " << a3 << endl;
    
    int b1;
    float b2;
    string b3;
    tie(b1, b2, b3) =  b; 
    cout << "b1 = " << b1 << ", b2 = " << b2 << ", b3 = " << b3 << endl;
    
    return 0;
}

上述代码输出,

a1 = 1, a2 = 2, a3 = three
b1 = 1, b2 = 2, b3 = three

3 工程化

暂无。。。

相关文章:

  • 编译中的 CMP0148 警告
  • 基于STC12C5A60S2系列1T 8051单片机的数模芯片DAC0832实现数模转换应用
  • Python---函数的作用,定义,使用步骤(调用步骤)
  • 《C++避坑神器·二十二》VS能正常运行程序,但运行exe程序无响应解决办法
  • Spring条件注解@Conditoinal+ Profile环境切换应用@Profile
  • SQL之开窗函数
  • Git常用操作-MD
  • 基于pytest-bdd的项目目录结构和命名规范
  • Jmeter 性能测试基础!
  • Vertx-EventBus篇
  • 如何通过数据治理来提升业务价值——业务场景治理
  • AI视频检索丨历史视频标签化,助力重要事件高效溯源
  • oracle 不支持的字符集 orai18n.jar ZHS16GBK 异常问题解决
  • IDEA写mybatis程序,java.io.IOException:Could not find resource mybatis-config.xml
  • 聊一聊前端面临的安全威胁与解决对策
  • 使用 webpack 打包 express 应用
  • 什么是Vue的前端微服务架构(Micro Frontends)?
  • AR贴纸特效SDK,无缝贴合的虚拟体验
  • 2.5k的ChatGPT-Java版SDK升级1.1.2-beta0支持GPT-4V、Dall-e-3模型、ToolCalls、微调Job、TTS...
  • Docker启动SRS流媒体服务器
  • 湖北宜化拟斥资超32亿加价回购“弃子”,布局上游煤炭业务
  • 董军在第六届联合国维和部长级会议上作大会发言
  • 最高法、证监会:常态化开展证券纠纷代表人诉讼,降低投资者维权成本
  • 李公明︱一周书记:当前科学观中的盲点、危机与……人类命运
  • 上海团队在医学顶刊连发两文,率先提出“证据污染”循证概念
  • 商务部召开外贸企业圆桌会:全力为外贸企业纾困解难,提供更多支持