recently i solved a problem which needed some answers on given pairs (each one of them) and i did it on sorted pairs and only normal way to get my answer (on unsorted pairs) was to create pair <pair<int,int>,int> p[2e5]. third int was for the index of unsorted pairs. so by sorting the pairs ,index automatically followed it and eventually i did it but code got messy and "time consuming" like p[i].first.second. Is there a better way of doing this?
I suggest using
struct
.use
struct
orarray<int, 3>
Use
array<int, 3>
,vector<int>
or ownstruct
.Guess
array<int, 3>
is the best solution. Just createarray<int, 3> p[(int)2e5]
and get elements byp[i][j]
.struct
is also a good solution, If you need to sort, you should make comparators for it.vector<int>
is also ok, but you need to initialize it manually.You can also use
tuple<int, int, int>
, but i'd never use it because ofstd::get<i>(tuple)
.thank you
Can you explain the part "i'd never use it because of std::get(tuple)" ?
It’s very annoying to type x= get<2>(myTuple) rather than x = a[2].
sandbag is right. Writing
get<0>(tuple)
is annoying. Why should I write this function with uncommon template, when I can just access element by[0]
inarray<int, 3>
orvector<int>
or by defining own elements instruct
. I'd like to have clearer code when I write it.But you can use structured binding.
class with public attributes
orstructure
work successfully in this situationtuple<int, int, int>
also works, though the convention ofget<i>(p)
($$$0 \le i \le 2$$$) might be quite off-key compared tostruct
orpair
.At least for C++17 and above (maybe C++14 as well, I didn't test), you can always do something like this and save the hassle: