-1

Why create a function like boost and std minmax that returns a pair, both of the same type. when you could return a vector and have the possibility of access to them with index.

If: vectores need .size()..., pair<> are exactly 2 elements... means less space...

Then why pair return first, and second. and not just a container with index.

  auto result = std::minmax_element (foo.begin(),foo.end());

  std::cout << "min is " << *result.first;
  std::cout << ", at position " << (result.first-foo.begin()) << '\n';
  std::cout << "max is " << *result.second;
  std::cout << ", at position " << (result.second-foo.begin()) << '\n';

think about this, is not the best example. but... remember intelligence is: maximize the possibilities. and this opens possibilities.

  for(uint64_t i = 0; i < 2; ++i) {
     if (i)
        std::cout << "max is ";
     else
        std::cout << "min is ";
     std::cout << *result[i];

     std::cout << ", at position " << (result[i]-foo.begin()) << '\n';
  }
Moises Rojo
  • 371
  • 2
  • 14
  • 2
    Two-element vector is more expensive: it requires a heap allocation. – Igor Tandetnik Apr 01 '18 at 21:50
  • 1
    `std::pair` is just a simple struct with 2 fields. Very low overhead, especially when those fields are POD types – Remy Lebeau Apr 01 '18 at 22:00
  • 1
    `pair` is just that: a pair of elements, typically related in some way. A `vector` is a container, and could have more than `2` elements, so before using `result[1]` you should check its size (regardless of what function documentation says), where in a `pair` both elements are always going to be safe to use. – Tas Apr 01 '18 at 22:04

1 Answers1

2

What you're proposing costs more and doesn't offer anything in return in the general case. In C++ we don't like to pay for what we don't need.

std::vector is designed to allow dynamic resizing. This comes at a significant cost. It uses dynamic memory allocation which is much slower than the "allocation" for automatic variables. Even if the allocation were free, the storage used will be outside the stack compromising your memory's locality and potentially causing extra cache misses. At the very least a conformant implementation needs a pointer to the allocated storage, the size and the capacity so it takes more space than std::pair. Finally, it has worse semantics as there is no way to tell from the type that it contains just two elements.

The benefits would be being able to insert more elements, which is something programmers generally do not feel the need to do to the results of a minmax_element function. If you need to have those elements in a container with other stuff, you can always build from the std::pair, but the general use case should not be weighed down to accommodate for this exceptional case.

And just to clarify, minmax_element returns a pair of iterators, not elements. Regardless of the size of the objects, these are basically two pointers. Cheap to construct and cheap to store.

patatahooligan
  • 3,111
  • 1
  • 18
  • 27
  • None of these arguments apply to `std::array`, which still has the advantage of being able to iterate over the two elements as the OP wants. At that point the only answer I can think of is that iteration over a same-type pair is uncommon, and unfortunately the best that can be done is refactor the loop body into a helper function. – Daniel H Apr 01 '18 at 22:49