From 31ab69904294bcbc0f38e4d401fcca620f7c6f61 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 29 Aug 2021 17:16:04 +0300 Subject: [PATCH] Add a shortcut for transforming a container in-place --- source/core/algorithm.h | 6 ++++++ source/strings/utils.cpp | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/source/core/algorithm.h b/source/core/algorithm.h index b79114e..268bf7b 100644 --- a/source/core/algorithm.h +++ b/source/core/algorithm.h @@ -171,6 +171,12 @@ inline void stable_sort_member(Container &cont, T Container::value_type::*mp) stable_sort(cont, MemberCompare(mp)); } +template +inline void transform(Container &cont, Predicate pred) +{ + transform(cont.begin(), cont.end(), cont.begin(), pred); +} + } // namespace Msp #endif diff --git a/source/strings/utils.cpp b/source/strings/utils.cpp index 6b659d0..eb2142d 100644 --- a/source/strings/utils.cpp +++ b/source/strings/utils.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include "utils.h" using namespace std; @@ -63,14 +63,14 @@ int strcasecmp(const string &s1, const string &s2) string tolower(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), [](char c){ return std::tolower(c); }); + transform(result, [](char c){ return std::tolower(c); }); return result; } string toupper(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), [](char c){ return std::toupper(c); }); + transform(result, [](char c){ return std::toupper(c); }); return result; } -- 2.43.0