Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Introducing kdalgorithms

An Algorithms Library for C++14 and Above

// ugly and unreadable algorithm code
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::for_each(vec.cbegin(), vec.cend(), [](int i) { cout << i << " "; });
QVector<QString> result;
for (auto i : vec) {
    result.append(QString::number(i));
}
qDebug() << "transform with loop:" << result;
QString toString(int i)
{
   return QString::number(i);
}

QVector<QString> result;
std::transform(vec.cbegin(), vec.cend(), std::back_inserter(result), toString);
qDebug() << "Transform with std::transform:" << result;
auto toString = [](int i) { return QString::number(i); };
auto result = kdalgorithms::transformed(vec, toString);
qDebug() << "Transform to std::vector:" << result;
std::vector vec{1, 2, 3, 4, 5};
auto greaterThan = [] (int value) {
  return [value] (int test) {
    return test > value;
  };
};
const auto result = kdalgorithms::find_if(vec, greaterThan(2));
if (result)
   std::cout << *result << std::endl;
else
   std::cout << "ahh nothing, right\n";
const auto result = kdalgorithms::find_if(vec, [] (int test) { return test > 2; });
const auto& [alive, dead] =
   kdalgorithms::partitioned<std::unordered_set>(getAllPlayers(),
                                                 &Player::isAlive);
const auto result =
    kdalgorithms::partitioned<std::unordered_set>(getAllPlayers(), &Player::isAlive);
const auto in = result.in;
const auto out = result.out;
const auto& [alive, dead] =
   kdalgorithms::partitioned<std::unordered_set>(
      getAllPlayers(), [] (const Player& player) { return player.isAlive(); });
std::vector<std::string> names{"Jesper", "Anne Helene", "Louise", "Laura", "Santa"};
std::vector<int> ages{52, 49, 11, 8};

auto olderThan = [](int age) {
   return [age](const auto &tuple) { return std::get<1>(tuple) > age; };
};
auto getName = [](const auto &tuple) { return std::get<0>(tuple); };
auto oldPeople = kdalgorithms::filtered_transformed(kdalgorithms::zip(names, ages),
                                                    getName, olderThan(40));
for (nameIt = names.cbegin(), ageIt = ages.cbegin();
     namesIt != names.cend() && agesIt != ages.cend();
     ++namesIt, ++agesIt) {
  ....
}
std::vector<std::tuple<std::string, int>> resultOfZip{
    {"Jesper", 52},
    {"Anne Helene", 49},
    {"Louise", 11},
    {"Laura", 8}
};

About KDAB


4 Comments

25 - Jan - 2023

Name

26 - Jan - 2023

Jesper K. Pedersen

31 - Jan - 2023

Carl Schwan

31 - Jan - 2023

Jesper K. Pedersen

JesperKjaerPedersen

Jesper K. Pedersen

HR Director / COO

Learn Modern C++

Learn more