namespace std_ext
Overview
Extensions to the standard libraries. More…
namespace std_ext { // classes template <typename T> class AtomicQueue; class AuthenticationError; class NotImplementedError; class OutOfBounds; class UnhandledConditionError; // global variables const std::string WHITESPACES = " \t\n\r\f\v"; // global functions void StringTo(const std::string& str, float& out); void StringTo(const std::string& str, double& out); void StringTo(const std::string& str, int32_t& out); void StringTo(const std::string& str, int64_t& out); void StringTo(const std::string& str, uint32_t& out); void StringTo(const std::string& str, uint64_t& out); std::wstring ToWString(const std::string& text); std::string ToStdString(const std::wstring& text); size_t CountOccurrences(const std::string& text, const std::string& key_to_search); size_t CountOccurrences(const std::string& text, const std::string& key_to_search, const size_t& off, const size_t& length); std::string Replace(const std::string& text, const std::string& key_to_search, const std::string& replace_text); std::string Replace(const std::string& text, const std::map<std::string, std::string>& replacement_map); template <typename T> void Replace(std::vector<T>& vector, const T old_elem, const T new_elem); std::string BoolToString(const bool value); std::string ToStringAutoPrecision(const double value, int max_digits = 6); template <typename T> std::string ToStringWithPrecision(const T value, const int n = 6); template <typename T> std::string ToStringWithZeroes(const T value, const int n = 6); template <typename T> std::string ToString(const std::vector<T>& container, const std::string& separator = ", "); std::string ToString(const std::vector<std::string>& container, const std::string& separator = ", "); template <typename T> std::string ToStringWithThousandSeparators(const T& input); void MakeUppercase(char* text); void MakeUppercase(std::string& text); std::string Uppercase(const std::string& text); void MakeLowercase(char* text); void MakeLowercase(std::string& text); std::string Lowercase(const std::string& text); bool EndsWith(const std::string& ending, const std::string& text); bool Contains(const std::string& substring, const std::string& text); bool Contains(const char character, const std::string& text); std::string ReplacePatternWithNumber(const std::string& base_string, const std::string& pattern, const int number); template <typename T> bool Contains(const T& element, const std::initializer_list<T>& list); template <typename Type> bool Contains(const Type& element, const std::vector<Type>& vector); template <typename Type> std::optional<size_t> GetIndex(const Type element, const std::vector<Type>& vector); template <typename Type> bool Contains(const Type element, const std::set<Type>& set); template <typename T1, typename T2> bool Contains(const T1 element, const std::unordered_set<T1, T2>& set); template <typename T1, typename T2> bool Contains(const T1 element, const std::map<T1, T2>& map); template <typename T1, typename T2> bool Contains(const T1 element, const std::unordered_map<T1, T2>& map); template <typename Type> bool ContainsCommonElement(const std::vector<Type>& elements1, const std::set<Type>& elements2); template <typename Type> void SortAndRemoveDuplicates(std::vector<Type>& vec); template <typename Type> std::vector<Type> RemoveDuplicates(std::vector<Type>& vector); template <typename Type> bool Erase(const Type& element, std::vector<Type>& vector); template <typename Type> bool Erase(const Type& element, std::set<Type>& set); template <typename IndicesType, typename Type> std::vector<IndicesType> CollectIndices(const std::vector<Type>& vector, const Type value); template <typename T, typename F> void Foreach(const T& container, const F& Function); template <typename T> auto MaxIt(const std::vector<T>& vector); template <typename T> auto MinIt(const std::vector<T>& vector); template <typename T> T Max(const std::vector<T>& vector); template <typename T> T Min(const std::vector<T>& vector); template <typename T> T Max(const T val); template <typename T, typename ... TS> T Max(const T val1, const T val2, const TS ... vals); template <typename T> T Min(const T val); template <typename T, typename... TS> T Min(const T val1, const T val2, const TS ... vals); template <typename T, typename F> auto Max(const std::vector<T>& vector, const F& Function); template <typename T, typename F> auto Min(const std::vector<T>& vector, const F& Function); template <typename T, typename F> auto MaxValue(const std::vector<T>& vector, const F& Function); template <typename T, typename F> auto MinValue(const std::vector<T>& vector, const F& Function); template <typename T, typename F> auto MiddleValue(const std::vector<T>& vector, const F& Function); template <typename T> auto Accumulate(const std::vector<T>& vector, const T start_value); template <typename T> auto Sum(const std::vector<T>& vector); template <typename T> auto Average(const std::vector<T>& vector); template <typename T, typename F> auto Accumulate(const std::vector<T>& vector, const T initial_value, const F& function); template <typename T, typename F> auto Sum(const std::vector<T>& vector, const F& function); template <typename T, typename F> float Average(const std::vector<T>& vector, const F& function); template <typename T> auto Median(const std::vector<T>& vector); template <typename T, typename S> void InsertBack(T& target, const S& source); template <typename T, typename S> void InsertFront(T& target, const S& source); template <typename T, typename S> void InsertTo(T& target, const S& source); template <typename Type> void EmplaceBackUnique(const Type& element, std::vector<Type>& vector); std::string& TrimBegin(std::string& s, const std::string& t = WHITESPACES); std::string TrimBegin(const std::string& s, const std::string& t = WHITESPACES); std::string& TrimEnd(std::string& s, const std::string& t = WHITESPACES); std::string& Trim(std::string& s, const std::string& t = WHITESPACES); std::string EndWithASingleSlash(const std::string& s); std::vector<std::string> Parse(const std::string& text, const std::string& separators = WHITESPACES); template <typename T> void Read(T& val, std::istream& str); template <typename T> T Read(std::istream& str); template <typename T> void Write(const T val, std::ostream& str); void Read(std::string&, std::istream&); void Write(const std::string&, std::ostream&); void ReadString(std::string& text, std::istream& str); std::string ReadString(std::istream& str); void WriteString(const std::string& text, std::ostream& str); template <class C, class T> auto Find(C& container, const T& value); template <class C, class F> auto Transformed(const C& container, F Function); template <class C, class P> auto AllOf(const C& container, const P& Predicate); template <class C, class P> auto AnyOf(const C& container, const P& Predicate); template <class C, class P> auto AnyOfW(C& container, const P& Predicate); template <class C, class P> auto NoneOf(const C& container, const P& Predicate); template <class C, class T> auto Count(const C& container, const T& val); template <class C, class P> auto CountIf(const C& container, const P& Predicate); template <class C, class P> auto FindIf(C& container, const P& Predicate); template <class C, class P> auto RevFindIf(C& container, const P& Predicate); template <class C, class P> auto FindIfPtr(C& container, const P& Predicate); template <class C, class P> auto EraseIf(C& container, const P& Predicate); template <class C, class P> auto SelectIf(const C& container, const P& Predicate); template <class C, class P> std::pair<C, C> Partition(const C& container, const P& Predicate); template <class T, class P> std::vector<std::pair<size_t, size_t>> FindIntervals(const std::vector<T>& vec, const P& Predicate); template <typename T> std::vector<T> Joined(const std::vector<std::vector<T>>& vector_of_vectors); template <typename T, typename F> std::vector<T> JoinTransformed(const std::vector<std::vector<T>>& vector_of_vectors, const F& Function); template <typename F, typename O> auto Bind1(F method, O object); bool IsASCII(const std::string& s); } // namespace std_ext
Detailed Documentation
Extensions to the standard libraries.
Global Functions
std::wstring ToWString(const std::string& text)
Convert single-byte std::string to std::wstring.
This only works if all the characters are single byte, i.e. ASCII or ISO-8859-1. Anything multi-byte will fail miserably, including UTF-8. (https://stackoverflow.com/a/8969776/2621721)
std::string ToStdString(const std::wstring& text)
Convert std::wstring to std::string.
size_t CountOccurrences(const std::string& text, const std::string& key_to_search)
Counts how many of specified key_to_search substrings does text contain.
size_t CountOccurrences(const std::string& text, const std::string& key_to_search, const size_t& off, const size_t& length)
Counts how many of specified key_to_search substrings does text contain in a specific range.
std::string Replace(const std::string& text, const std::string& key_to_search, const std::string& replace_text)
Replaces all occurrences of key_to_search with replace_text in specified text.
std::string Replace(const std::string& text, const std::map<std::string, std::string>& replacement_map)
Replaces all occurrences of key_to_search with replace_text in specified text.
Parameters:
replacement_map |
Contains pairs of <key_to_search, replace_text>. |
template <typename T> void Replace(std::vector<T>& vector, const T old_elem, const T new_elem)
Replaces all occurrences of old_elem in a vector with a new_elem.
std::string BoolToString(const bool value)
Converts bool to string (“true” or “false”).
std::string ToStringAutoPrecision(const double value, int max_digits = 6)
Converts double to string with decimal digits until max_digits are reached.
Parameters:
value |
Value which will be converted to string. |
max_digits |
Value that represents maximum number of decimal digits that will be converted. |
template <typename T> std::string ToStringWithPrecision(const T value, const int n = 6)
Converts type to string with specified number of decimal digits.
template <typename T> std::string ToStringWithZeroes(const T value, const int n = 6)
Converts int to string with zeros at the beginning to match specified number of digits.
template <typename T> std::string ToString(const std::vector<T>& container, const std::string& separator = ", ")
Converts all values in the vector to a string and joins them using specified separator.
std::string ToString(const std::vector<std::string>& container, const std::string& separator = ", ")
Joins all values in a string vector using specified separator.
template <typename T> std::string ToStringWithThousandSeparators(const T& input)
Converts a numeric value to string using thousand separators.
Example: 10235 => “10’235” 1000000000 => 1’000’000’000
void MakeUppercase(char* text)
Converts text into all uppercase.
void MakeUppercase(std::string& text)
Converts text into all uppercase.
std::string Uppercase(const std::string& text)
Converts text into all uppercase.
void MakeLowercase(char* text)
Converts text into all lowercase.
void MakeLowercase(std::string& text)
Converts text into all lowercase.
std::string Lowercase(const std::string& text)
Converts text into all lowercase.
bool EndsWith(const std::string& ending, const std::string& text)
Checks if the text ends with a string.
bool Contains(const std::string& substring, const std::string& text)
Checks if the text contains substring.
bool Contains(const char character, const std::string& text)
Checks if the text contains character.
std::string ReplacePatternWithNumber(const std::string& base_string, const std::string& pattern, const int number)
Replaces a patter in specified string with a number.
Example: (“example-###”, “#”, 2) –> “example-002”
template <typename T> bool Contains(const T& element, const std::initializer_list<T>& list)
Check if the initializer list contains an element.
template <typename Type> bool Contains(const Type& element, const std::vector<Type>& vector)
Check if the vector contains an element.
template <typename Type> std::optional<size_t> GetIndex(const Type element, const std::vector<Type>& vector)
If an element exists in the vector, returns its index.
template <typename Type> bool Contains(const Type element, const std::set<Type>& set)
Check if the set contains an element.
template <typename T1, typename T2> bool Contains(const T1 element, const std::unordered_set<T1, T2>& set)
Check if an unordered_set contains an element.
template <typename T1, typename T2> bool Contains(const T1 element, const std::map<T1, T2>& map)
Check if the mat contains an element.
template <typename T1, typename T2> bool Contains(const T1 element, const std::unordered_map<T1, T2>& map)
Check if an unordered_map contains an element.
template <typename Type> bool ContainsCommonElement(const std::vector<Type>& elements1, const std::set<Type>& elements2)
Check if there is at least a single common element in two vectors.
template <typename Type> void SortAndRemoveDuplicates(std::vector<Type>& vec)
Sorts the array and removes duplicate elements.
template <typename Type> std::vector<Type> RemoveDuplicates(std::vector<Type>& vector)
Returns vector similar to input, but without duplicate elements.
template <typename Type> bool Erase(const Type& element, std::vector<Type>& vector)
Removes an element from the vector. Returns success.
template <typename Type> bool Erase(const Type& element, std::set<Type>& set)
Removes an element with from the set. Returns success.
template <typename IndicesType, typename Type> std::vector<IndicesType> CollectIndices(const std::vector<Type>& vector, const Type value)
Collect all indices of elements in a vector with a specified value.
template <typename T, typename F> void Foreach(const T& container, const F& Function)
Execute a function for every element in the container.
template <typename T> auto MaxIt(const std::vector<T>& vector)
Finds a maximum value in a vector, return an iterator pointing to it.
template <typename T> auto MinIt(const std::vector<T>& vector)
Finds a minimal value in a vector, return an iterator pointing to it.
template <typename T> T Max(const std::vector<T>& vector)
Finds a maximum value in a vector.
template <typename T> T Min(const std::vector<T>& vector)
Finds a minimal value in a vector.
template <typename T> T Max(const T val)
Finds a maximum value from a single element. Boundary case for the variadic expansion.
template <typename T, typename ... TS> T Max(const T val1, const T val2, const TS ... vals)
Finds a maximum value from variadic argument list.
template <typename T> T Min(const T val)
Finds a minimal value from a single element. Boundary case for the variadic expansion.
template <typename T, typename... TS> T Min(const T val1, const T val2, const TS ... vals)
Finds a minimal value from variadic argument list.
template <typename T, typename F> auto Max(const std::vector<T>& vector, const F& Function)
Finds the object in a vector, which when transformed by a function gives a maximum value.
template <typename T, typename F> auto Min(const std::vector<T>& vector, const F& Function)
Finds the object in a vector, which when transformed by a function gives a minimal value.
template <typename T, typename F> auto MaxValue(const std::vector<T>& vector, const F& Function)
Finds a maximum value transformed by a function in a vector.
template <typename T, typename F> auto MinValue(const std::vector<T>& vector, const F& Function)
Finds a minimal value transformed by a function in a vector.
template <typename T, typename F> auto MiddleValue(const std::vector<T>& vector, const F& Function)
Finds a middle (average of min, max) value transformed by a function in a vector.
template <typename T> auto Accumulate(const std::vector<T>& vector, const T start_value)
Wrapper for std::accumulate.
template <typename T> auto Sum(const std::vector<T>& vector)
Sum all elements in a vector.
template <typename T> auto Average(const std::vector<T>& vector)
Compute average element of a vector. In case of empty vector, return 0.
template <typename T, typename F> auto Accumulate(const std::vector<T>& vector, const T initial_value, const F& function)
Wrapper for std::accumulate - https://en.cppreference.com/w/cpp/algorithm/accumulate - values transformed by a function.
This will make a sum of every element of the input vector transformed by a function plus the initial element.
Parameters:
vector |
The input vector (will be transformed by a function). |
initial_value |
The initial_value will be added to the resulting sum. |
function |
The function that will transform every element of the vector. |
template <typename T, typename F> auto Sum(const std::vector<T>& vector, const F& function)
Sum all elements in a vector transformed by a function.
template <typename T, typename F> float Average(const std::vector<T>& vector, const F& function)
Compute average element of a vector transformed by a function. In case of empty vector, return 0.0f.
template <typename T> auto Median(const std::vector<T>& vector)
Compute median value of a vector. In case of empty vector, return 0.
template <typename T, typename S> void InsertBack(T& target, const S& source)
Insert the source vector to the back of target vector.
template <typename T, typename S> void InsertFront(T& target, const S& source)
Insert the source vector to the front of target vector.
template <typename T, typename S> void InsertTo(T& target, const S& source)
Insert the source container to the target container.
template <typename Type> void EmplaceBackUnique(const Type& element, std::vector<Type>& vector)
Emplace back an element if it is unique to the vector.
std::string& TrimBegin(std::string& s, const std::string& t = WHITESPACES)
Trims characters from beginning of string (left).
std::string TrimBegin(const std::string& s, const std::string& t = WHITESPACES)
Trims characters from beginning of string (left).
std::string& TrimEnd(std::string& s, const std::string& t = WHITESPACES)
Trims characters from end of string (right).
std::string& Trim(std::string& s, const std::string& t = WHITESPACES)
Trims from both ends of string (left & right).
std::string EndWithASingleSlash(const std::string& s)
Trim the end of a string to removes forward and backward slashes (/, ) and add one single forward slash /.
std::vector<std::string> Parse(const std::string& text, const std::string& separators = WHITESPACES)
Parses the input text to sub strings by a given list of separators.
template <typename T> void Read(T& val, std::istream& str)
Reads a value from the binary stream, number of bytes red is determined by a template type.
template <typename T> T Read(std::istream& str)
Reads a value from the binary stream, number of bytes red is determined by a template type.
template <typename T> void Write(const T val, std::ostream& str)
Writes a value to the binary stream, bytes written are determined by a template type.
void ReadString(std::string& text, std::istream& str)
Reads a string from the binary stream.
std::string ReadString(std::istream& str)
Reads a value from the binary stream, number of bytes red is determined by a template type.
void WriteString(const std::string& text, std::ostream& str)
Writes a value to the binary stream, number of bytes written is determined by a template type.
template <class C, class T> auto Find(C& container, const T& value)
Simplified wrapper for std::find.
template <class C, class F> auto Transformed(const C& container, F Function)
Simplified wrapper for std::transform.
template <class C, class P> auto AllOf(const C& container, const P& Predicate)
Simplified wrapper for std::all_of.
template <class C, class P> auto AnyOf(const C& container, const P& Predicate)
Simplified wrapper for std::any_of.
template <class C, class P> auto AnyOfW(C& container, const P& Predicate)
Simplified wrapper for std::any_of (writable).
template <class C, class P> auto NoneOf(const C& container, const P& Predicate)
Simplified wrapper for std::none_of.
template <class C, class T> auto Count(const C& container, const T& val)
Simplified wrapper for std::count.
template <class C, class P> auto CountIf(const C& container, const P& Predicate)
Simplified wrapper for std::count_if.
template <class C, class P> auto FindIf(C& container, const P& Predicate)
Simplified wrapper for std::find_if.
template <class C, class P> auto RevFindIf(C& container, const P& Predicate)
Simplified wrapper for reversed std::find_if.
template <class C, class P> auto FindIfPtr(C& container, const P& Predicate)
Simplified wrapper for std::find_if that expects a vector of pointers as input and returns a pointer.
The motivation is instead of returning an iterator to a pointer, to return the pointer itself or null.
Returns:
Return the pointer itself if found, else it returns null pointer.
template <class C, class P> auto EraseIf(C& container, const P& Predicate)
Simplified wrapper for std::remove_if and std::erase.
template <class C, class P> auto SelectIf(const C& container, const P& Predicate)
Copies all elements fulfilling a given condition into a new vector. Uses std::copy_if.
template <class C, class P> std::pair<C, C> Partition(const C& container, const P& Predicate)
Partitions all elements based on a given condition.
Uses std::partition_copy.
Returns:
A pair of container. The first contains all elements that satisfy the condition, the second all elements that do not.
template <class T, class P> std::vector<std::pair<size_t, size_t>> FindIntervals(const std::vector<T>& vec, const P& Predicate)
Finds all intervals on the vector where a given condition holds.
Returns:
A vector of interval indices pairs.
template <typename F, typename O> auto Bind1(F method, O object)
Creates binding for an object method with a single argument.
bool IsASCII(const std::string& s)
Check std::string for non-ASCII characters.