Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

c++ - More than 2 variables in std::pair

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...