c++ - how to emplace_back(pair) efficiently? -
i have
using namespace std; // convenience in question vector<pair<array<int,3>, int>> foo; and want emplace_back element pair::first holding {i,j,k} , pair::second holding q. way compiling rather clumsy
foo.emplace_back(piecewise_construct, forward_as_tuple(i,j,k), forward_as_tuple(q)); is efficient (i.e. guaranteed tuples optimised away)? or there way guaranteed efficient?
(i tried
foo.emplace_back(std::initializer_list<int>{i,j,k}, q); but no avail gcc 4.8.1). know can avoid problem defining
struct element : std::pair<std::array<int,3>,int> { element(int i, int j, int k, int q) { first={i,j,k}; second=q; } }; vector<element> foo; foo.emplace_back(i,j,k,q); but i'd prefer without such code.
std::array<t, n> doesn't have constructor taking std::initializer_list<u> u, not u = t. don't mistake fact can initialised braced-init-list presence of such constructor.
instead, create array.
foo.emplace_back(std::array<int,3>{i,j,k}, q); there hardly guarantees efficiency, , not guaranteed more efficient tuple approach. in practice, shouldn't make of difference, expect implementations optimise both forms of emplace_back same machine code. if care, , doubt that, measure it.
Comments
Post a Comment