Beispiel #1
0
int main()
{
    std::vector<int> vec = {19, 45, 32, 10, 0, 90, 15, 1, 7, 5, 6, 69};
    for (auto i : sorted(vec)) {
        std::cout << i << '\n';
    }

    const std::vector<int> cvec(vec);
    for (auto i : sorted(cvec)) {
        std::cout << i << '\n';
    }

    std::cout << "Sort by first character only\n";
    std::vector<std::string> svec = {"hello", "everyone", "thanks", "for",
                                     "having", "me", "here", "today"};
    for (auto s : sorted(svec,
                [] (const std::string & s1, const std::string & s2) {
                    return s1[0] < s2[0]; })) {
        std::cout << s << '\n';
    }
        

    for (auto i : sorted(
                std::vector<int>{19, 45, 32, 10, 0, 90, 15, 1, 7, 5, 6, 69})) {
        std::cout << i << '\n';
    }
    return 0;
}
Beispiel #2
0
#include <unordered_set>
#include <vector>
#include <array>
#include <string>
#include <utility>

#include "helpers.hpp"
#include "catch.hpp"

using iter::sorted;

using Vec = const std::vector<int>;

TEST_CASE("sorted: iterates through a vector in sorted order", "[sorted]") {
  Vec ns = {4, 0, 5, 1, 6, 7, 9, 3, 2, 8};
  auto s = sorted(ns);
  Vec v(std::begin(s), std::end(s));
  Vec vc = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  REQUIRE(v == vc);
}

TEST_CASE("sorted: can modify elements through sorted", "[sorted]") {
  std::vector<int> ns(3, 9);
  for (auto&& n : sorted(ns)) {
    n = -1;
  }
  Vec vc(3, -1);
  REQUIRE(ns == vc);
}

TEST_CASE("sorted: can iterate over unordered container", "[sorted]") {