简体   繁体   中英

Vector of pairs or pair of vectors?

In my code I'm dealing with a set of measurements (a vector of float), in which each element has two associated uncertainties (say +up/-down). Let's say I want to dump on screen these values, something like

Loop over the vector of the measurements
{
   cout << i-th central value << " +" << i-th up uncertainty << " / -" << i-th down uncertainty << end; 
}

What's the most efficient/elegant way to do it?

1) Using a pair of vectors

vector<float> central; //central measurement
pair<vector<float>, vector<float>> errors; //errors
for( int i = 0; i < central.size ; i++ )
{
    cout << central.at(i) << " +" << errors.first.at(i) << " / -" << errors.second.at(i) << endl;
}

2) Using a vector of pairs:

vector<float> central; //central measurement
vector<pair<float,float>> errors; //errors
for( int i = 0; i < central.size ; i++ )
{
    cout << central.at(i) << " +" << errors.at(i).first << " / -" << errors.at(i).second << endl;
}

3) Two separate vectors:

vector<float> central; //central measurement
vector<float> errUp; //errors up
vector<float> errDown; //errors down
for( int i = 0; i < central.size ; i++ )
{
    cout << central.at(i) << " +" << errUp.at(i) << " / -" << errDown.at(i) << endl;
}

This:

in which each element has two associated uncertainties (say +up/-down)

says to me you have an object containing your three elements (value + up/down). As such, I would create that object and store them in a single vector.

Storing using multiple vectors means you have to keep them in sync (add to both transactionally etc.). That's at best troublesome and you would have to encapsulate those vectors to be safe.

How about a vector of triples?

#include <array>
#include <vector>

using Measurement = std::array<float, 3>;

std::vector<Measurement> data;

I would make a class or struct called Measurement for single responsibility. You may not need two values for uncertainty up and down. Most of the time these are the same regardless of the sign, which means that one uncertainty value may do just fine.

struct Measurement
{
  union
  {
    float values[3];
    struct { float value, uncertaintyUp, uncertaintyDown; };
  };
}

Using a union will give you the readability you want, and the flexibility of the [] operator.

main()
{
  Measurement x;
  x.value = 1.0f;
  x.uncertaintyUp = 0.1f;
  x.uncertaintyDown = -0.1f;

  for(int i = 0; i < 3; i++)
  {
    cout << x.values[i] << " ";
  }
}

From here it would be simple to use a vector of Measurement structs. If you can use one container for your data it is always best. If you think about it, all the data in all three vectors would be the same all the time for count and capacity, there is no need for the extra overhead; one vector saves space-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM