2

pair looks like this:

std::vector<std::pair<uint64 /*id*/, std::string /*message*/>

And if I want 3 variables in vector? Can I use pair or what?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jelean Thomas
  • 422
  • 1
  • 7
  • 19

3 Answers3

5

In C++ sometimes I find quite useful to define trivial all-public data-only classes like

struct Event {
    int id = 0;
    std::string msg = "";
    double time = 0.;
};

Surely a bit of typing, but IMO way better than having to use e.second or std::get<1>(e) instead of e.msg everywhere in the code.

Writing is done once, reading many times. Saving writing time at the expense of increasing reading/understanding time is a Very Bad Idea.

The drawback of this approach is that you cannot access the n-th member of the structure in metaprograms, but C++ metaprogramming is terribly weak anyway for a lot of other reasons so if you really need to have non-trivial metacode I'd suggest moving out of C++ and using an external C++ code generator written in a decent language instead of template tricks and hacks.

6502
  • 112,025
  • 15
  • 165
  • 265
3

If I have understood correctly you could use std::tuple declared in header <tuple>. For example

std::vector<std::tuple<uint64, std::string, SomeOtherType>> v;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

If you don't want C++11, you can use

std::pair<uint64, std::pair<std::string, SomeOtherType> >

But this is as wrong as trying to put three values to a pair. Why I consider it wrong? A pair means two values. If you put three or any other number of values in a pair, you are doing something like this.

Community
  • 1
  • 1
Adam Trhon
  • 2,915
  • 1
  • 20
  • 51