gpt4 book ai didi

c++ - 从元组列表中访问单个元组的元素

转载 作者:行者123 更新时间:2023-11-30 02:45:17 25 4
gpt4 key购买 nike

#include <iostream>
#include <fstream>
#include <list>
#include <tuple>

using namespace std;

int main()
{
list<tuple<char,double,double>> XYZ_Widget_Store;

ifstream infile;
infile.open("data.text");

char x; // S, P, R
double a,b;

for(;infile >> x >> a >> b;){
XYZ_Widget_Store.push_back(make_tuple(x,a,b));
}

infile.close();

for(list<int>::iterator it = XYZ_Widget_Store.begin(); it !=
XYZ_Widget_Store.end(); ++it){
cout << *it.get<0> << endl;
}
return 0;
}

假设我的 list 上的第一项包含 tuple ('a',1,1)如何从该元组中获取第一个元素“a”?通常它只是 get<0>(mytuple)但是在列表中很难理解。我想遍历列表并从列表中的每个元素中获取每个第一个元素。 list 的元素本身就是一个 tuple .

最佳答案

如果您打算使用 C++11,您不妨使用其他不错的功能,例如 auto 和 for-each 循环。下面是您可以如何重写最后一个 for 循环:

for (auto& tup : XYZ_Widget_Store) {
cout << get<0>(tup) << endl;
}

关于c++ - 从元组列表中访问单个元组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com