C++ vector<std::tuple<XXX, XXX, XXX>>

格羅馬仕發表於2020-11-06

c++ vector套tuple 遍歷

C++ vector<std::tuple<XXX, XXX, XXX> >

std::vector<std::tuple<std::string, std::string, int> > vec;

  1 #include <functional>
  2 #include <iostream>
  4 #include <tuple>
  5 #include <vector>

 44 void test6() {
 45     std::vector<std::tuple<int, double, float> > temp;
 46     temp.push_back(std::make_tuple(1, 1.2, 1.23));
 47     temp.push_back(std::make_tuple(2, 2.2, 2.23));
 48     temp.push_back(std::make_tuple(3, 3.2, 3.23));
 49
 50     for (int i = 0; i < temp.size(); i++) {
 51         int t1 = std::get<0>(temp[i]);
 52         std::cout << t1 << std::endl;
 53
 54         double t2 = std::get<1>(temp[i]);
 55         std::cout << t2 << std::endl;
 56
 57         float t3 = std::get<2>(temp[i]);
 58         std::cout << t3 << std::endl;
 59     }
 60
 61 }
 62 int main() {
 63 	test6();
 64  	return 0;
 65 }

執行結果:
在這裡插入圖片描述

舉個例子, 懂的都懂!!!

結語:
時間: 2020-11-06

相關文章