本文介绍了std :: tuple vs std :: array作为std :: vector的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

  std :: vector< 4_integers> v; 






std :: tuple 解决方案:

  std :: vector< std :: tuple< int,int,int,int> v; 






std :: array 解决方案:

  std :: vector< std :: array& int,4> v; 

为什么?



EDIT(用例):



对不起,以前没有提到。我将使用它如下:

  for(const auto& item:v){
some_function [0],项目[1],项目[2],项目[3]); // or tuple equivalent
}



当然,我需要保存它们,因为计算

解决方案

对于这种特殊情况,我必须不同意意见。对于均匀类型的容器 - 如这里的情况(所有 int s) - 数组

查看与,很清楚后者是一个容器(有迭代器,eg),而前者不是。这意味着标准库的其余部分将更加自然地适用于后者。



如果类型不是同类的,那么就不会有问题 - 它必须 std :: tuple


I have this case:

std::vector<4_integers> v;


What would fit best here?

std::tuple solution:

std::vector<std::tuple<int,int,int,int>> v;


std::array solution:

std::vector<std::array<int,4>> v;

and why?

EDIT (The use case):

Sorry for not mentioning that before. I am going to use it as follow:

for(const auto& item:v){
   some_function(item[0],item[1],item[2],item[3]); // or tuple equivalent
}

Of course I need to keep them stored because computing the 4 integers is not a thing that I want to repeat again and again.

解决方案

For this specific case, I'd have to disagree with the comments. For homogeneous type containers - as is the case here (all ints) - array is superior.

When looking at the interface of std::tuple vs. std::array, it is very clear that the latter is a container (with iterators, e.g.), while the former is not. This means that the rest of the standard library will be much more naturally applicable to the latter.

If the types weren't homogeneous, there wouldn't be a question - it would have to be std::tuple.

这篇关于std :: tuple vs std :: array作为std :: vector的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:15