cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::functional Namespace Reference

Namespaces

namespace  detail
 
namespace  detail_impl
 

Classes

class  MemoizedFunction< R(Args...)>
 MemoizedFunction对特定函数签名的特化 / Specialization of MemoizedFunction for specific function signature 仅声明,实现在impl中 / Declaration only, definition in impl. More...
 

Functions

template<typename G , typename F >
CPP_TOOLBOX_EXPORT auto compose (G &&g, F &&f)
 将两个函数组合成一个函数 / Composes two functions into a single function
 
template<typename F1 , typename... FRest>
CPP_TOOLBOX_EXPORT auto compose (F1 &&f1, FRest &&... rest)
 将多个函数组合成一个函数 / Composes multiple functions into a single function
 
CPP_TOOLBOX_EXPORT auto compose ()
 空的compose函数,会抛出错误 / Empty compose function that throws an error
 
template<typename F , typename Arg1 >
CPP_TOOLBOX_EXPORT auto bind_first (F &&f, Arg1 &&arg1)
 绑定函数的第一个参数 / Binds the first argument of a function
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto map (const std::optional< T > &opt, F &&f) -> std::optional< std::invoke_result_t< F, const T & > >
 在optional值上映射函数 / Maps a function over an optional value
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto map (std::optional< T > &&opt, F &&f) -> std::optional< std::invoke_result_t< F, T && > >
 在optional右值上映射函数 / Maps a function over an optional rvalue
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto flatMap (const std::optional< T > &opt, F &&f) -> std::invoke_result_t< F, const T & >
 在optional值上平面映射函数 / Flat maps a function over an optional value
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto flatMap (std::optional< T > &&opt, F &&f) -> std::invoke_result_t< F, T && >
 在optional右值上平面映射函数 / Flat maps a function over an optional rvalue
 
template<typename T , typename U >
CPP_TOOLBOX_EXPORT auto orElse (const std::optional< T > &opt, U &&default_value) -> T
 返回包含的值或默认值 / Returns the contained value or a default
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto orElseGet (const std::optional< T > &opt, F &&default_func) -> T
 返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default
 
template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto filter (const std::optional< T > &opt, P &&p) -> std::optional< T >
 基于谓词过滤optional / Filters an optional based on a predicate
 
template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto filter (std::optional< T > &&opt, P &&p) -> std::optional< T >
 基于谓词过滤optional右值 / Filters an optional rvalue based on a predicate
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto match (const std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto match (std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 对非const左值variant使用访问器函数进行匹配 / Match variant with visitor functions for non-const lvalue variant
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto match (std::variant< Ts... > &&var, Fs &&... visitors) -> decltype(auto)
 对右值variant使用访问器函数进行匹配 / Match variant with visitor functions for rvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto map (const std::variant< Ts... > &var, F &&f) -> ResultVariant
 对variant中保存的值应用函数并在新variant中返回结果 / Apply a function to the value held by a variant and return result in a new variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto map (std::variant< Ts... > &var, F &&f) -> ResultVariant
 对非const左值variant中保存的值应用函数 / Apply a function to the value held by a non-const lvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto map (std::variant< Ts... > &&var, F &&f) -> ResultVariant
 对右值variant中保存的值应用函数 / Apply a function to the value held by an rvalue variant
 
template<typename Container , typename Func >
CPP_TOOLBOX_EXPORT auto map (const Container &input, Func &&f) -> std::vector< std::invoke_result_t< Func, typename Container::const_reference > >
 对容器中的每个元素应用函数并返回结果向量 / Apply a function to each element in a container and return results in a vector
 
template<typename Container , typename Predicate >
CPP_TOOLBOX_EXPORT auto filter (const Container &input, Predicate &&p) -> std::vector< typename Container::value_type >
 过滤满足谓词的容器元素 / Filter elements from a container that satisfy a predicate
 
template<typename Container , typename T , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto reduce (const Container &input, T identity, BinaryOp &&op) -> T
 使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value
 
template<typename Container , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto reduce (const Container &input, BinaryOp &&op) -> typename Container::value_type
 使用二元操作归约非空容器元素 / Reduce non-empty container elements using a binary operation
 
template<typename... Containers>
CPP_TOOLBOX_EXPORT auto zip (const Containers &... containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >
 将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples
 
template<typename ContainerKeys , typename ContainerValues , typename Key = typename std::decay_t<ContainerKeys>::value_type, typename Value = typename std::decay_t<ContainerValues>::value_type, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Alloc = std::allocator<std::pair<const Key, Value>>>
CPP_TOOLBOX_EXPORT auto zip_to_unordered_map (const ContainerKeys &keys, const ContainerValues &values) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >
 将两个序列压缩成无序映射 / Zip two sequences into an unordered_map
 
template<typename Signature , typename Func >
CPP_TOOLBOX_EXPORT auto memoize (Func &&f)
 创建带显式签名的记忆化函数 / Create a memoized function with explicit signature
 
template<typename R , typename... Args, typename Func >
CPP_TOOLBOX_EXPORT auto memoize_explicit (Func &&f) -> std::function< R(Args...)>
 创建带显式返回和参数类型的记忆化函数 / Create a memoized function with explicit return and argument types
 
template<typename G , typename F >
auto compose (G &&g, F &&f)
 将两个函数组合成一个函数 / Composes two functions into a single function
 
template<typename F1 , typename... FRest>
auto compose (F1 &&f1, FRest &&... rest)
 将多个函数组合成一个函数 / Composes multiple functions into a single function
 
template<typename F , typename Arg1 >
auto bind_first (F &&f, Arg1 &&arg1)
 绑定函数的第一个参数 / Binds the first argument of a function
 
template<typename T , typename F >
auto map (const std::optional< T > &opt, F &&f) -> std::optional< std::invoke_result_t< F, const T & > >
 在optional值上映射函数 / Maps a function over an optional value
 
template<typename T , typename F >
auto map (std::optional< T > &&opt, F &&f) -> std::optional< std::invoke_result_t< F, T && > >
 在optional右值上映射函数 / Maps a function over an optional rvalue
 
template<typename T , typename F >
auto flatMap (const std::optional< T > &opt, F &&f) -> std::invoke_result_t< F, const T & >
 在optional值上平面映射函数 / Flat maps a function over an optional value
 
template<typename T , typename F >
auto flatMap (std::optional< T > &&opt, F &&f) -> std::invoke_result_t< F, T && >
 在optional右值上平面映射函数 / Flat maps a function over an optional rvalue
 
template<typename T , typename U >
auto orElse (const std::optional< T > &opt, U &&default_value) -> T
 返回包含的值或默认值 / Returns the contained value or a default
 
template<typename T , typename F >
auto orElseGet (const std::optional< T > &opt, F &&default_func) -> T
 返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default
 
template<typename T , typename P >
auto filter (const std::optional< T > &opt, P &&p) -> std::optional< T >
 基于谓词过滤optional / Filters an optional based on a predicate
 
template<typename T , typename P >
auto filter (std::optional< T > &&opt, P &&p) -> std::optional< T >
 基于谓词过滤optional右值 / Filters an optional rvalue based on a predicate
 
template<typename... Ts, typename... Fs>
auto match (const std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions
 
template<typename... Ts, typename... Fs>
auto match (std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 对非const左值variant使用访问器函数进行匹配 / Match variant with visitor functions for non-const lvalue variant
 
template<typename... Ts, typename... Fs>
auto match (std::variant< Ts... > &&var, Fs &&... visitors) -> decltype(auto)
 对右值variant使用访问器函数进行匹配 / Match variant with visitor functions for rvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
auto map (const std::variant< Ts... > &var, F &&f) -> ResultVariant
 对variant中保存的值应用函数并在新variant中返回结果 / Apply a function to the value held by a variant and return result in a new variant
 
template<typename ResultVariant , typename... Ts, typename F >
auto map (std::variant< Ts... > &var, F &&f) -> ResultVariant
 对非const左值variant中保存的值应用函数 / Apply a function to the value held by a non-const lvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
auto map (std::variant< Ts... > &&var, F &&f) -> ResultVariant
 对右值variant中保存的值应用函数 / Apply a function to the value held by an rvalue variant
 
template<typename Container , typename Func >
auto map (const Container &input, Func &&f) -> std::vector< std::invoke_result_t< Func, typename Container::const_reference > >
 对容器中的每个元素应用函数并返回结果向量 / Apply a function to each element in a container and return results in a vector
 
template<typename Container , typename Predicate >
auto filter (const Container &input, Predicate &&p) -> std::vector< typename Container::value_type >
 过滤满足谓词的容器元素 / Filter elements from a container that satisfy a predicate
 
template<typename Container , typename T , typename BinaryOp >
auto reduce (const Container &input, T identity, BinaryOp &&op) -> T
 使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value
 
template<typename Container , typename BinaryOp >
auto reduce (const Container &input, BinaryOp &&op) -> typename Container::value_type
 使用二元操作归约非空容器元素 / Reduce non-empty container elements using a binary operation
 
template<typename... Containers>
auto zip (const Containers &... containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >
 将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples
 
template<typename ContainerKeys , typename ContainerValues , typename Key , typename Value , typename Hash , typename KeyEqual , typename Alloc >
auto zip_to_unordered_map (const ContainerKeys &keys, const ContainerValues &values) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >
 将两个序列压缩成无序映射 / Zip two sequences into an unordered_map
 
template<typename Signature , typename Func >
auto memoize (Func &&f)
 创建带显式签名的记忆化函数 / Create a memoized function with explicit signature
 
template<typename R , typename... Args, typename Func >
auto memoize_explicit (Func &&f) -> std::function< R(Args...)>
 创建带显式返回和参数类型的记忆化函数 / Create a memoized function with explicit return and argument types
 

Variables

template<typename Signature >
class CPP_TOOLBOX_EXPORT MemoizedFunction
 缓存函数结果的记忆化函数类 / Memoized function class that caches function results
 

Function Documentation

◆ bind_first() [1/2]

template<typename F , typename Arg1 >
CPP_TOOLBOX_EXPORT auto toolbox::functional::bind_first ( F &&  f,
Arg1 &&  arg1 
)

绑定函数的第一个参数 / Binds the first argument of a function

Template Parameters
F函数类型 / Function type
Arg1第一个参数的类型 / Type of first argument
Parameters
f要绑定的函数 / Function to bind
arg1要绑定为第一个参数的值 / Value to bind as first argument
Returns
第一个参数被绑定的函数 / Function with first argument bound
// 绑定第一个参数的示例 / Example of binding first argument
auto divide = [](int x, int y) { return x / y; };
auto divide_10_by = bind_first(divide, 10);
int result = divide_10_by(2); // 返回5 / Returns 5
CPP_TOOLBOX_EXPORT auto bind_first(F &&f, Arg1 &&arg1)
绑定函数的第一个参数 / Binds the first argument of a function
Definition functional_impl.hpp:142

◆ bind_first() [2/2]

template<typename F , typename Arg1 >
auto toolbox::functional::bind_first ( F &&  f,
Arg1 &&  arg1 
)

绑定函数的第一个参数 / Binds the first argument of a function

Template Parameters
F函数类型 / Function type
Arg1第一个参数的类型 / Type of first argument
Parameters
f要绑定的函数 / Function to bind
arg1要绑定为第一个参数的值 / Value to bind as first argument
Returns
第一个参数被绑定的函数 / Function with first argument bound
// 绑定第一个参数的示例 / Example of binding first argument
auto divide = [](int x, int y) { return x / y; };
auto divide_10_by = bind_first(divide, 10);
int result = divide_10_by(2); // 返回5 / Returns 5

◆ compose() [1/5]

auto toolbox::functional::compose ( )
inline

空的compose函数,会抛出错误 / Empty compose function that throws an error

Exceptions
std::logic_error当被调用时 / when called
try {
auto empty = compose();
} catch(const std::logic_error& e) {
// 异常被捕获 / Exception caught
}
CPP_TOOLBOX_EXPORT auto compose()
空的compose函数,会抛出错误 / Empty compose function that throws an error
Definition functional_impl.hpp:136

◆ compose() [2/5]

template<typename F1 , typename... FRest>
CPP_TOOLBOX_EXPORT auto toolbox::functional::compose ( F1 &&  f1,
FRest &&...  rest 
)

将多个函数组合成一个函数 / Composes multiple functions into a single function

Template Parameters
F1第一个(最外层)函数类型 / Type of first (outermost) function
FRest剩余函数类型 / Types of remaining functions
Parameters
f1第一个函数 / First function
rest剩余函数 / Remaining functions
Returns
从右到左应用函数的函数 / A function that applies functions from right to left
// 多函数组合示例 / Example of multiple function composition
auto add_one = [](int x) { return x + 1; };
auto multiply_two = [](int x) { return x * 2; };
auto square = [](int x) { return x * x; };
auto composed = compose(square, multiply_two, add_one);
int result = composed(5); // 返回144: ((5 + 1) * 2)^2 / Returns 144: ((5 + 1)
* 2)^2

◆ compose() [3/5]

template<typename F1 , typename... FRest>
auto toolbox::functional::compose ( F1 &&  f1,
FRest &&...  rest 
)

将多个函数组合成一个函数 / Composes multiple functions into a single function

Template Parameters
F1第一个(最外层)函数类型 / Type of first (outermost) function
FRest剩余函数类型 / Types of remaining functions
Parameters
f1第一个函数 / First function
rest剩余函数 / Remaining functions
Returns
从右到左应用函数的函数 / A function that applies functions from right to left
// 多函数组合示例 / Example of multiple function composition
auto add_one = [](int x) { return x + 1; };
auto multiply_two = [](int x) { return x * 2; };
auto square = [](int x) { return x * x; };
auto composed = compose(square, multiply_two, add_one);
int result = composed(5); // 返回144: ((5 + 1) * 2)^2 / Returns 144: ((5 + 1)
* 2)^2

◆ compose() [4/5]

template<typename G , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::compose ( G &&  g,
F &&  f 
)

将两个函数组合成一个函数 / Composes two functions into a single function

Template Parameters
G外层函数类型 / Type of outer function
F内层函数类型 / Type of inner function
Parameters
g外层函数 / Outer function
f内层函数 / Inner function
Returns
先应用f再应用g的函数 / A function that applies f then g
// 函数组合示例 / Example of function composition
auto add_one = [](int x) { return x + 1; };
auto multiply_two = [](int x) { return x * 2; };
auto composed = compose(multiply_two, add_one);
int result = composed(5); // 返回12: ((5 + 1) * 2) / Returns 12: ((5 + 1) *
2)

◆ compose() [5/5]

template<typename G , typename F >
auto toolbox::functional::compose ( G &&  g,
F &&  f 
)

将两个函数组合成一个函数 / Composes two functions into a single function

Template Parameters
G外层函数类型 / Type of outer function
F内层函数类型 / Type of inner function
Parameters
g外层函数 / Outer function
f内层函数 / Inner function
Returns
先应用f再应用g的函数 / A function that applies f then g
// 函数组合示例 / Example of function composition
auto add_one = [](int x) { return x + 1; };
auto multiply_two = [](int x) { return x * 2; };
auto composed = compose(multiply_two, add_one);
int result = composed(5); // 返回12: ((5 + 1) * 2) / Returns 12: ((5 + 1) *
2)

◆ filter() [1/6]

template<typename Container , typename Predicate >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter ( const Container &  input,
Predicate &&  p 
) -> std::vector< typename Container::value_type >

过滤满足谓词的容器元素 / Filter elements from a container that satisfy a predicate

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
Predicate谓词函数类型 / Predicate function type
Parameters
input输入容器 / Input container
p返回bool的谓词函数 / Predicate function returning bool
Returns
包含满足谓词p的元素的向量 / Vector containing elements that satisfy predicate p
// 过滤偶数 / Filter even numbers
std::vector<int> nums = {1, 2, 3, 4, 5};
auto evens = filter(nums, [](int x) { return x % 2 == 0; });
// 返回 {2, 4} / Returns {2, 4}
// 过滤长字符串 / Filter long strings
std::list<std::string> strs = {"a", "bb", "ccc"};
auto long_strs = filter(strs, [](const auto& s) { return s.length() > 1; });
// 返回 {"bb", "ccc"} / Returns {"bb", "ccc"}
CPP_TOOLBOX_EXPORT auto filter(const std::optional< T > &opt, P &&p) -> std::optional< T >
基于谓词过滤optional / Filters an optional based on a predicate
Definition functional_impl.hpp:242

◆ filter() [2/6]

template<typename Container , typename Predicate >
auto toolbox::functional::filter ( const Container &  input,
Predicate &&  p 
) -> std::vector< typename Container::value_type >

过滤满足谓词的容器元素 / Filter elements from a container that satisfy a predicate

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
Predicate谓词函数类型 / Predicate function type
Parameters
input输入容器 / Input container
p返回bool的谓词函数 / Predicate function returning bool
Returns
包含满足谓词p的元素的向量 / Vector containing elements that satisfy predicate p
// 过滤偶数 / Filter even numbers
std::vector<int> nums = {1, 2, 3, 4, 5};
auto evens = filter(nums, [](int x) { return x % 2 == 0; });
// 返回 {2, 4} / Returns {2, 4}
// 过滤长字符串 / Filter long strings
std::list<std::string> strs = {"a", "bb", "ccc"};
auto long_strs = filter(strs, [](const auto& s) { return s.length() > 1; });
// 返回 {"bb", "ccc"} / Returns {"bb", "ccc"}

◆ filter() [3/6]

template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter ( const std::optional< T > &  opt,
P &&  p 
) -> std::optional< T >

基于谓词过滤optional / Filters an optional based on a predicate

Template Parameters
Toptional中包含的类型 / Type contained in optional
P谓词类型 / Predicate type
Parameters
opt要过滤的optional / Optional to filter
p要应用的谓词 / Predicate to apply
Returns
如果谓词返回true则为optional,否则为空optional / Optional if predicate returns true, empty optional otherwise
// 过滤optional的示例 / Example of filtering optional
std::optional<int> opt(42);
auto is_even = [](int x) { return x % 2 == 0; };
auto result = filter(opt, is_even); // 包含42 / Contains 42

◆ filter() [4/6]

template<typename T , typename P >
auto toolbox::functional::filter ( const std::optional< T > &  opt,
P &&  p 
) -> std::optional< T >

基于谓词过滤optional / Filters an optional based on a predicate

Template Parameters
Toptional中包含的类型 / Type contained in optional
P谓词类型 / Predicate type
Parameters
opt要过滤的optional / Optional to filter
p要应用的谓词 / Predicate to apply
Returns
如果谓词返回true则为optional,否则为空optional / Optional if predicate returns true, empty optional otherwise
// 过滤optional的示例 / Example of filtering optional
std::optional<int> opt(42);
auto is_even = [](int x) { return x % 2 == 0; };
auto result = filter(opt, is_even); // 包含42 / Contains 42

◆ filter() [5/6]

template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter ( std::optional< T > &&  opt,
P &&  p 
) -> std::optional< T >

基于谓词过滤optional右值 / Filters an optional rvalue based on a predicate

Template Parameters
Toptional中包含的类型 / Type contained in optional
P谓词类型 / Predicate type
Parameters
opt要过滤的optional右值 / Optional rvalue to filter
p要应用的谓词 / Predicate to apply
Returns
如果谓词返回true则为optional,否则为空optional / Optional if predicate returns true, empty optional otherwise
// 过滤optional右值的示例 / Example of filtering optional rvalue
auto result = filter(std::optional<int>(42), [](int x) { return x % 2 == 0;
}); // 包含42 / Contains 42

◆ filter() [6/6]

template<typename T , typename P >
auto toolbox::functional::filter ( std::optional< T > &&  opt,
P &&  p 
) -> std::optional< T >

基于谓词过滤optional右值 / Filters an optional rvalue based on a predicate

Template Parameters
Toptional中包含的类型 / Type contained in optional
P谓词类型 / Predicate type
Parameters
opt要过滤的optional右值 / Optional rvalue to filter
p要应用的谓词 / Predicate to apply
Returns
如果谓词返回true则为optional,否则为空optional / Optional if predicate returns true, empty optional otherwise
// 过滤optional右值的示例 / Example of filtering optional rvalue
auto result = filter(std::optional<int>(42), [](int x) { return x % 2 == 0;
}); // 包含42 / Contains 42

◆ flatMap() [1/4]

template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::flatMap ( const std::optional< T > &  opt,
F &&  f 
) -> std::invoke_result_t< F, const T & >

在optional值上平面映射函数 / Flat maps a function over an optional value

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回optional的函数类型 / Function type returning optional
Parameters
opt要平面映射的optional / Optional to flat map over
f要应用的函数 / Function to apply
Returns
如果opt有值则为f的结果,否则为空optional / Result of f if opt has value, empty optional otherwise
// 在optional上平面映射的示例 / Example of flat mapping over optional
std::optional<int> opt(5);
auto maybe_double = [](int x) -> std::optional<int> {
return x < 10 ? std::optional(x * 2) : std::nullopt;
};
auto result = flatMap(opt, maybe_double); // 包含10 / Contains 10
CPP_TOOLBOX_EXPORT auto flatMap(const std::optional< T > &opt, F &&f) -> std::invoke_result_t< F, const T & >
在optional值上平面映射函数 / Flat maps a function over an optional value
Definition functional_impl.hpp:182

◆ flatMap() [2/4]

template<typename T , typename F >
auto toolbox::functional::flatMap ( const std::optional< T > &  opt,
F &&  f 
) -> std::invoke_result_t< F, const T & >

在optional值上平面映射函数 / Flat maps a function over an optional value

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回optional的函数类型 / Function type returning optional
Parameters
opt要平面映射的optional / Optional to flat map over
f要应用的函数 / Function to apply
Returns
如果opt有值则为f的结果,否则为空optional / Result of f if opt has value, empty optional otherwise
// 在optional上平面映射的示例 / Example of flat mapping over optional
std::optional<int> opt(5);
auto maybe_double = [](int x) -> std::optional<int> {
return x < 10 ? std::optional(x * 2) : std::nullopt;
};
auto result = flatMap(opt, maybe_double); // 包含10 / Contains 10

◆ flatMap() [3/4]

template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::flatMap ( std::optional< T > &&  opt,
F &&  f 
) -> std::invoke_result_t< F, T && >

在optional右值上平面映射函数 / Flat maps a function over an optional rvalue

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回optional的函数类型 / Function type returning optional
Parameters
opt要平面映射的optional右值 / Optional rvalue to flat map over
f要应用的函数 / Function to apply
Returns
如果opt有值则为f的结果,否则为空optional / Result of f if opt has value, empty optional otherwise
// 在optional右值上平面映射的示例 / Example of flat mapping over optional
rvalue auto result = flatMap(std::optional<int>(5),
[](int x) -> std::optional<int> {
return x < 10 ? std::optional(x * 2) : std::nullopt;
}); // 包含10 / Contains 10

◆ flatMap() [4/4]

template<typename T , typename F >
auto toolbox::functional::flatMap ( std::optional< T > &&  opt,
F &&  f 
) -> std::invoke_result_t< F, T && >

在optional右值上平面映射函数 / Flat maps a function over an optional rvalue

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回optional的函数类型 / Function type returning optional
Parameters
opt要平面映射的optional右值 / Optional rvalue to flat map over
f要应用的函数 / Function to apply
Returns
如果opt有值则为f的结果,否则为空optional / Result of f if opt has value, empty optional otherwise
// 在optional右值上平面映射的示例 / Example of flat mapping over optional
rvalue auto result = flatMap(std::optional<int>(5),
[](int x) -> std::optional<int> {
return x < 10 ? std::optional(x * 2) : std::nullopt;
}); // 包含10 / Contains 10

◆ map() [1/12]

template<typename Container , typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( const Container &  input,
Func &&  f 
) -> std::vector< std::invoke_result_t< Func, typename Container::const_reference > >

对容器中的每个元素应用函数并返回结果向量 / Apply a function to each element in a container and return results in a vector

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
Func要应用到元素的函数类型 / Function type to apply to elements
Parameters
input输入容器 / Input container
f要应用到每个元素的函数 / Function to apply to each element
Returns
包含对每个元素应用f的结果的向量 / Vector containing results of applying f to each element
// 基本用法 / Basic usage
std::vector<int> nums = {1, 2, 3};
auto squares = map(nums, [](int x) { return x * x; });
// 返回 {1, 4, 9} / Returns {1, 4, 9}
// 字符串长度映射 / String length mapping
std::list<std::string> strs = {"a", "bb", "ccc"};
auto lengths = map(strs, [](const auto& s) { return s.length(); });
// 返回 {1, 2, 3} / Returns {1, 2, 3}
CPP_TOOLBOX_EXPORT auto map(const std::optional< T > &opt, F &&f) -> std::optional< std::invoke_result_t< F, const T & > >
在optional值上映射函数 / Maps a function over an optional value
Definition functional_impl.hpp:153

◆ map() [2/12]

template<typename Container , typename Func >
auto toolbox::functional::map ( const Container &  input,
Func &&  f 
) -> std::vector< std::invoke_result_t< Func, typename Container::const_reference > >

对容器中的每个元素应用函数并返回结果向量 / Apply a function to each element in a container and return results in a vector

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
Func要应用到元素的函数类型 / Function type to apply to elements
Parameters
input输入容器 / Input container
f要应用到每个元素的函数 / Function to apply to each element
Returns
包含对每个元素应用f的结果的向量 / Vector containing results of applying f to each element
// 基本用法 / Basic usage
std::vector<int> nums = {1, 2, 3};
auto squares = map(nums, [](int x) { return x * x; });
// 返回 {1, 4, 9} / Returns {1, 4, 9}
// 字符串长度映射 / String length mapping
std::list<std::string> strs = {"a", "bb", "ccc"};
auto lengths = map(strs, [](const auto& s) { return s.length(); });
// 返回 {1, 2, 3} / Returns {1, 2, 3}

◆ map() [3/12]

template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( const std::optional< T > &  opt,
F &&  f 
) -> std::optional< std::invoke_result_t< F, const T & > >

在optional值上映射函数 / Maps a function over an optional value

Template Parameters
Toptional中包含的类型 / Type contained in optional
F函数类型 / Function type
Parameters
opt要映射的optional / Optional to map over
f要应用的函数 / Function to apply
Returns
如果opt有值则包含f的结果的optional,否则为空optional / Optional containing result of f if opt has value, empty optional otherwise
// 在optional上映射的示例 / Example of mapping over optional
std::optional<int> opt(5);
auto times_two = [](int x) { return x * 2; };
auto result = map(opt, times_two); // 包含10 / Contains 10

◆ map() [4/12]

template<typename T , typename F >
auto toolbox::functional::map ( const std::optional< T > &  opt,
F &&  f 
) -> std::optional< std::invoke_result_t< F, const T & > >

在optional值上映射函数 / Maps a function over an optional value

Template Parameters
Toptional中包含的类型 / Type contained in optional
F函数类型 / Function type
Parameters
opt要映射的optional / Optional to map over
f要应用的函数 / Function to apply
Returns
如果opt有值则包含f的结果的optional,否则为空optional / Optional containing result of f if opt has value, empty optional otherwise
// 在optional上映射的示例 / Example of mapping over optional
std::optional<int> opt(5);
auto times_two = [](int x) { return x * 2; };
auto result = map(opt, times_two); // 包含10 / Contains 10

◆ map() [5/12]

template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( const std::variant< Ts... > &  var,
F &&  f 
) -> ResultVariant

对variant中保存的值应用函数并在新variant中返回结果 / Apply a function to the value held by a variant and return result in a new variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
Exceptions
std::bad_variant_access如果var是valueless_by_exception / if var is valueless_by_exception
f抛出的任何异常/ Any exception thrown by f
std::bad_variant_access如果f的结果不能构造任何ResultVariant类型 / if f's result cannot construct any ResultVariant type
// 在variant上映射的示例 / Example of mapping over variant
std::variant<int, std::string> v = 42;
auto result = map<std::variant<double, size_t>>(v,
[](const auto& x) -> std::variant<double, size_t> {
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, int>) {
return x * 1.5; // int -> double
} else {
return x.length(); // string -> size_t
}
}
); // 返回包含63.0的variant / Returns variant containing 63.0

◆ map() [6/12]

template<typename ResultVariant , typename... Ts, typename F >
auto toolbox::functional::map ( const std::variant< Ts... > &  var,
F &&  f 
) -> ResultVariant

对variant中保存的值应用函数并在新variant中返回结果 / Apply a function to the value held by a variant and return result in a new variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
Exceptions
std::bad_variant_access如果var是valueless_by_exception / if var is valueless_by_exception
f抛出的任何异常/ Any exception thrown by f
std::bad_variant_access如果f的结果不能构造任何ResultVariant类型 / if f's result cannot construct any ResultVariant type
// 在variant上映射的示例 / Example of mapping over variant
std::variant<int, std::string> v = 42;
auto result = map<std::variant<double, size_t>>(v,
[](const auto& x) -> std::variant<double, size_t> {
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, int>) {
return x * 1.5; // int -> double
} else {
return x.length(); // string -> size_t
}
}
); // 返回包含63.0的variant / Returns variant containing 63.0

◆ map() [7/12]

template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( std::optional< T > &&  opt,
F &&  f 
) -> std::optional< std::invoke_result_t< F, T && > >

在optional右值上映射函数 / Maps a function over an optional rvalue

Template Parameters
Toptional中包含的类型 / Type contained in optional
F函数类型 / Function type
Parameters
opt要映射的optional右值 / Optional rvalue to map over
f要应用的函数 / Function to apply
Returns
如果opt有值则包含f的结果的optional,否则为空optional / Optional containing result of f if opt has value, empty optional otherwise
// 在optional右值上映射的示例 / Example of mapping over optional rvalue
auto result = map(std::optional<int>(5), [](int x) { return x * 2; }); //
包含10 / Contains 10

◆ map() [8/12]

template<typename T , typename F >
auto toolbox::functional::map ( std::optional< T > &&  opt,
F &&  f 
) -> std::optional< std::invoke_result_t< F, T && > >

在optional右值上映射函数 / Maps a function over an optional rvalue

Template Parameters
Toptional中包含的类型 / Type contained in optional
F函数类型 / Function type
Parameters
opt要映射的optional右值 / Optional rvalue to map over
f要应用的函数 / Function to apply
Returns
如果opt有值则包含f的结果的optional,否则为空optional / Optional containing result of f if opt has value, empty optional otherwise
// 在optional右值上映射的示例 / Example of mapping over optional rvalue
auto result = map(std::optional<int>(5), [](int x) { return x * 2; }); //
包含10 / Contains 10

◆ map() [9/12]

template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( std::variant< Ts... > &&  var,
F &&  f 
) -> ResultVariant

对右值variant中保存的值应用函数 / Apply a function to the value held by an rvalue variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
// 在右值variant上映射的示例 / Example of mapping over rvalue variant
auto result = map<std::variant<std::string>>(
std::variant<int, std::string>(42),
[](auto&& x) { return std::to_string(x); }
); // 返回包含"42"的variant / Returns variant containing "42"

◆ map() [10/12]

template<typename ResultVariant , typename... Ts, typename F >
auto toolbox::functional::map ( std::variant< Ts... > &&  var,
F &&  f 
) -> ResultVariant

对右值variant中保存的值应用函数 / Apply a function to the value held by an rvalue variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
// 在右值variant上映射的示例 / Example of mapping over rvalue variant
auto result = map<std::variant<std::string>>(
std::variant<int, std::string>(42),
[](auto&& x) { return std::to_string(x); }
); // 返回包含"42"的variant / Returns variant containing "42"

◆ map() [11/12]

template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map ( std::variant< Ts... > &  var,
F &&  f 
) -> ResultVariant

对非const左值variant中保存的值应用函数 / Apply a function to the value held by a non-const lvalue variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
// 在左值variant上映射的示例 / Example of mapping over lvalue variant
std::variant<int, std::string> v = 42;
auto result = map<std::variant<double>>(v,
[](int& x) { x *= 2; return x * 1.5; }
); // 修改v并返回包含126.0的variant / Modifies v and returns variant
containing 126.0

◆ map() [12/12]

template<typename ResultVariant , typename... Ts, typename F >
auto toolbox::functional::map ( std::variant< Ts... > &  var,
F &&  f 
) -> ResultVariant

对非const左值variant中保存的值应用函数 / Apply a function to the value held by a non-const lvalue variant

Template Parameters
ResultVariant要返回的目标variant类型 / The target variant type to return
Ts输入variant中的类型 / Types in input variant
F要应用的函数类型 / Function type to apply
Parameters
var输入variant / Input variant
f要应用到variant值的函数 / Function to apply to variant value
Returns
包含f的结果的ResultVariant / ResultVariant containing f's result
// 在左值variant上映射的示例 / Example of mapping over lvalue variant
std::variant<int, std::string> v = 42;
auto result = map<std::variant<double>>(v,
[](int& x) { x *= 2; return x * 1.5; }
); // 修改v并返回包含126.0的variant / Modifies v and returns variant
containing 126.0

◆ match() [1/6]

template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match ( const std::variant< Ts... > &  var,
Fs &&...  visitors 
) -> decltype(auto)

使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// variant模式匹配的示例 / Example of pattern matching on variant
std::variant<int, std::string> v = 42;
auto result = match(v,
[](int i) { return i * 2; },
[](const std::string& s) { return s.length(); }
); // 返回84 / Returns 84
CPP_TOOLBOX_EXPORT auto match(const std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions
Definition functional_impl.hpp:286

◆ match() [2/6]

template<typename... Ts, typename... Fs>
auto toolbox::functional::match ( const std::variant< Ts... > &  var,
Fs &&...  visitors 
) -> decltype(auto)

使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// variant模式匹配的示例 / Example of pattern matching on variant
std::variant<int, std::string> v = 42;
auto result = match(v,
[](int i) { return i * 2; },
[](const std::string& s) { return s.length(); }
); // 返回84 / Returns 84

◆ match() [3/6]

template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match ( std::variant< Ts... > &&  var,
Fs &&...  visitors 
) -> decltype(auto)

对右值variant使用访问器函数进行匹配 / Match variant with visitor functions for rvalue variant

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// 对右值variant进行模式匹配的示例 / Example of pattern matching on rvalue
variant auto result = match(std::variant<int, std::string>(42),
[](int&& i) { return i * 2; },
[](std::string&& s) { return s + "!"; }
); // 返回84 / Returns 84

◆ match() [4/6]

template<typename... Ts, typename... Fs>
auto toolbox::functional::match ( std::variant< Ts... > &&  var,
Fs &&...  visitors 
) -> decltype(auto)

对右值variant使用访问器函数进行匹配 / Match variant with visitor functions for rvalue variant

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// 对右值variant进行模式匹配的示例 / Example of pattern matching on rvalue
variant auto result = match(std::variant<int, std::string>(42),
[](int&& i) { return i * 2; },
[](std::string&& s) { return s + "!"; }
); // 返回84 / Returns 84

◆ match() [5/6]

template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match ( std::variant< Ts... > &  var,
Fs &&...  visitors 
) -> decltype(auto)

对非const左值variant使用访问器函数进行匹配 / Match variant with visitor functions for non-const lvalue variant

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// 对左值variant进行模式匹配的示例 / Example of pattern matching on lvalue
variant std::variant<int, std::string> v = 42; auto result = match(v,
[](int& i) { i *= 2; return i; },
[](std::string& s) { s += "!"; return s.length(); }
); // 修改v并返回84 / Modifies v and returns 84

◆ match() [6/6]

template<typename... Ts, typename... Fs>
auto toolbox::functional::match ( std::variant< Ts... > &  var,
Fs &&...  visitors 
) -> decltype(auto)

对非const左值variant使用访问器函数进行匹配 / Match variant with visitor functions for non-const lvalue variant

Template Parameters
Tsvariant中的类型 / Types in variant
Fs访问器函数类型 / Visitor function types
Parameters
var要匹配的variant / Variant to match on
visitors每个类型的访问器函数 / Visitor functions for each type
Returns
匹配的访问器的结果 / Result of matched visitor
// 对左值variant进行模式匹配的示例 / Example of pattern matching on lvalue
variant std::variant<int, std::string> v = 42; auto result = match(v,
[](int& i) { i *= 2; return i; },
[](std::string& s) { s += "!"; return s.length(); }
); // 修改v并返回84 / Modifies v and returns 84

◆ memoize() [1/2]

template<typename Signature , typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::memoize ( Func &&  f)

创建带显式签名的记忆化函数 / Create a memoized function with explicit signature

Template Parameters
Signature函数签名类型 / Function signature type
Func函数对象类型 / Function object type
Parameters
f要记忆化的函数 / Function to memoize
Returns
MemoizedFunction对象 / MemoizedFunction object
// 带显式签名的记忆化函数 / Memoized function with explicit signature
auto memoized_add = memoize<int(int,int)>([](int a, int b) {
return a + b;
});
int result1 = memoized_add(2, 3); // 计算并缓存 / Computes and caches
int result2 = memoized_add(2, 3); // 返回缓存结果 / Returns cached result

◆ memoize() [2/2]

template<typename Signature , typename Func >
auto toolbox::functional::memoize ( Func &&  f)

创建带显式签名的记忆化函数 / Create a memoized function with explicit signature

Template Parameters
Signature函数签名类型 / Function signature type
Func函数对象类型 / Function object type
Parameters
f要记忆化的函数 / Function to memoize
Returns
MemoizedFunction对象 / MemoizedFunction object
// 带显式签名的记忆化函数 / Memoized function with explicit signature
auto memoized_add = memoize<int(int,int)>([](int a, int b) {
return a + b;
});
int result1 = memoized_add(2, 3); // 计算并缓存 / Computes and caches
int result2 = memoized_add(2, 3); // 返回缓存结果 / Returns cached result

◆ memoize_explicit() [1/2]

template<typename R , typename... Args, typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::memoize_explicit ( Func &&  f) -> std::function< R(Args...)>

创建带显式返回和参数类型的记忆化函数 / Create a memoized function with explicit return and argument types

Template Parameters
R返回类型 / Return type
Args参数类型 / Argument types
Func函数对象类型 / Function object type
Parameters
f要记忆化的函数 / Function to memoize
Returns
带记忆化的std::function / std::function with memoization
// 记忆化斐波那契函数 / Memoized fibonacci function
auto fib = memoize_explicit<int, int>([&](int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
});
int result = fib(10); // 高效计算斐波那契数 / Efficiently computes fibonacci
Note
使用共享状态的线程安全实现 / Thread-safe implementation using shared state
参数经过decay后必须能形成有效的映射键 / Arguments must form valid map key after decay
返回类型必须可复制 / Return type must be copyable

◆ memoize_explicit() [2/2]

template<typename R , typename... Args, typename Func >
auto toolbox::functional::memoize_explicit ( Func &&  f) -> std::function< R(Args...)>

创建带显式返回和参数类型的记忆化函数 / Create a memoized function with explicit return and argument types

Template Parameters
R返回类型 / Return type
Args参数类型 / Argument types
Func函数对象类型 / Function object type
Parameters
f要记忆化的函数 / Function to memoize
Returns
带记忆化的std::function / std::function with memoization
// 记忆化斐波那契函数 / Memoized fibonacci function
auto fib = memoize_explicit<int, int>([&](int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
});
int result = fib(10); // 高效计算斐波那契数 / Efficiently computes fibonacci
Note
使用共享状态的线程安全实现 / Thread-safe implementation using shared state
参数经过decay后必须能形成有效的映射键 / Arguments must form valid map key after decay
返回类型必须可复制 / Return type must be copyable

◆ orElse() [1/2]

template<typename T , typename U >
CPP_TOOLBOX_EXPORT auto toolbox::functional::orElse ( const std::optional< T > &  opt,
U &&  default_value 
) -> T

返回包含的值或默认值 / Returns the contained value or a default

Template Parameters
Toptional中包含的类型 / Type contained in optional
U默认值类型 / Type of default value
Parameters
opt要获取值的optional / Optional to get value from
default_value如果optional为空时返回的值 / Value to return if optional is empty
Returns
包含的值或默认值 / Contained value or default
// 提供默认值的示例 / Example of providing default value
std::optional<int> opt;
int result = orElse(opt, 42); // 返回42 / Returns 42
CPP_TOOLBOX_EXPORT auto orElse(const std::optional< T > &opt, U &&default_value) -> T
返回包含的值或默认值 / Returns the contained value or a default
Definition functional_impl.hpp:214

◆ orElse() [2/2]

template<typename T , typename U >
auto toolbox::functional::orElse ( const std::optional< T > &  opt,
U &&  default_value 
) -> T

返回包含的值或默认值 / Returns the contained value or a default

Template Parameters
Toptional中包含的类型 / Type contained in optional
U默认值类型 / Type of default value
Parameters
opt要获取值的optional / Optional to get value from
default_value如果optional为空时返回的值 / Value to return if optional is empty
Returns
包含的值或默认值 / Contained value or default
// 提供默认值的示例 / Example of providing default value
std::optional<int> opt;
int result = orElse(opt, 42); // 返回42 / Returns 42

◆ orElseGet() [1/2]

template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::orElseGet ( const std::optional< T > &  opt,
F &&  default_func 
) -> T

返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回默认值的函数类型 / Function type returning default value
Parameters
opt要获取值的optional / Optional to get value from
default_func用于获取默认值的函数 / Function to call for default value
Returns
包含的值或default_func的结果 / Contained value or result of default_func
// 通过函数提供默认值的示例 / Example of providing default via function
std::optional<int> opt;
auto get_default = []() { return 42; };
int result = orElseGet(opt, get_default); // 返回42 / Returns 42
CPP_TOOLBOX_EXPORT auto orElseGet(const std::optional< T > &opt, F &&default_func) -> T
返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default
Definition functional_impl.hpp:224

◆ orElseGet() [2/2]

template<typename T , typename F >
auto toolbox::functional::orElseGet ( const std::optional< T > &  opt,
F &&  default_func 
) -> T

返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default

Template Parameters
Toptional中包含的类型 / Type contained in optional
F返回默认值的函数类型 / Function type returning default value
Parameters
opt要获取值的optional / Optional to get value from
default_func用于获取默认值的函数 / Function to call for default value
Returns
包含的值或default_func的结果 / Contained value or result of default_func
// 通过函数提供默认值的示例 / Example of providing default via function
std::optional<int> opt;
auto get_default = []() { return 42; };
int result = orElseGet(opt, get_default); // 返回42 / Returns 42

◆ reduce() [1/4]

template<typename Container , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto toolbox::functional::reduce ( const Container &  input,
BinaryOp &&  op 
) -> typename Container::value_type

使用二元操作归约非空容器元素 / Reduce non-empty container elements using a binary operation

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
BinaryOp二元操作函数类型 / Binary operation function type
Parameters
input输入容器(必须非空) / Input container (must be non-empty)
op组合元素的二元操作 / Binary operation to combine elements
Returns
归约的结果 / Result of reduction
Exceptions
std::invalid_argument如果输入容器为空 / if input container is empty
// 求和 / Sum numbers
std::vector<int> nums = {1, 2, 3, 4};
auto sum = reduce(nums, std::plus<int>());
// 返回 10 / Returns 10
// 连接字符串 / Concatenate strings
std::list<std::string> strs = {"a", "b", "c"};
auto concat = reduce(strs, std::plus<std::string>());
// 返回 "abc" / Returns "abc"
// 空容器会抛出异常 / Empty container throws
std::vector<int> empty;
auto result = reduce(empty, std::plus<int>());
// 抛出 std::invalid_argument / throws std::invalid_argument
CPP_TOOLBOX_EXPORT auto reduce(const Container &input, T identity, BinaryOp &&op) -> T
使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value
Definition functional_impl.hpp:424

◆ reduce() [2/4]

template<typename Container , typename BinaryOp >
auto toolbox::functional::reduce ( const Container &  input,
BinaryOp &&  op 
) -> typename Container::value_type

使用二元操作归约非空容器元素 / Reduce non-empty container elements using a binary operation

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
BinaryOp二元操作函数类型 / Binary operation function type
Parameters
input输入容器(必须非空) / Input container (must be non-empty)
op组合元素的二元操作 / Binary operation to combine elements
Returns
归约的结果 / Result of reduction
Exceptions
std::invalid_argument如果输入容器为空 / if input container is empty
// 求和 / Sum numbers
std::vector<int> nums = {1, 2, 3, 4};
auto sum = reduce(nums, std::plus<int>());
// 返回 10 / Returns 10
// 连接字符串 / Concatenate strings
std::list<std::string> strs = {"a", "b", "c"};
auto concat = reduce(strs, std::plus<std::string>());
// 返回 "abc" / Returns "abc"
// 空容器会抛出异常 / Empty container throws
std::vector<int> empty;
auto result = reduce(empty, std::plus<int>());
// 抛出 std::invalid_argument / throws std::invalid_argument

◆ reduce() [3/4]

template<typename Container , typename T , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto toolbox::functional::reduce ( const Container &  input,
identity,
BinaryOp &&  op 
) -> T

使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
T初始值和结果类型 / Initial value and result type
BinaryOp二元操作函数类型 / Binary operation function type
Parameters
input输入容器 / Input container
identity初始值(单位元) / Initial value (identity element)
op组合元素的二元操作 / Binary operation to combine elements
Returns
归约的结果 / Result of reduction
// 求和 / Sum numbers
std::vector<int> nums = {1, 2, 3, 4};
auto sum = reduce(nums, 0, std::plus<int>());
// 返回 10 / Returns 10
// 求积 / Calculate product
auto product = reduce(nums, 1, std::multiplies<int>());
// 返回 24 / Returns 24
// 连接字符串 / Concatenate strings
std::list<std::string> strs = {"a", "b", "c"};
auto concat = reduce(strs, std::string(), std::plus<std::string>());
// 返回 "abc" / Returns "abc"

◆ reduce() [4/4]

template<typename Container , typename T , typename BinaryOp >
auto toolbox::functional::reduce ( const Container &  input,
identity,
BinaryOp &&  op 
) -> T

使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value

Template Parameters
Container输入容器类型,必须提供begin()和end()迭代器 / Input container type, must provide begin() and end() iterators
T初始值和结果类型 / Initial value and result type
BinaryOp二元操作函数类型 / Binary operation function type
Parameters
input输入容器 / Input container
identity初始值(单位元) / Initial value (identity element)
op组合元素的二元操作 / Binary operation to combine elements
Returns
归约的结果 / Result of reduction
// 求和 / Sum numbers
std::vector<int> nums = {1, 2, 3, 4};
auto sum = reduce(nums, 0, std::plus<int>());
// 返回 10 / Returns 10
// 求积 / Calculate product
auto product = reduce(nums, 1, std::multiplies<int>());
// 返回 24 / Returns 24
// 连接字符串 / Concatenate strings
std::list<std::string> strs = {"a", "b", "c"};
auto concat = reduce(strs, std::string(), std::plus<std::string>());
// 返回 "abc" / Returns "abc"

◆ zip() [1/2]

template<typename... Containers>
CPP_TOOLBOX_EXPORT auto toolbox::functional::zip ( const Containers &...  containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >

将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples

Template Parameters
Containers容器类型,必须提供size()和begin()/end() / Container types, must provide size() and begin()/end()
Parameters
containers输入容器 / Input containers
Returns
包含每个容器元素引用的元组向量 / Vector of tuples containing references to elements from each container
// 压缩不同类型的容器 / Zip containers of different types
std::vector<int> nums = {1, 2, 3};
std::vector<std::string> strs = {"a", "b", "c"};
std::array<double> dbls = {1.1, 2.2, 3.3};
auto zipped = zip(nums, strs, dbls);
// 返回元组向量 / Returns vector of tuples:
// {
// {1, "a", 1.1},
// {2, "b", 2.2},
// {3, "c", 3.3}
// }
CPP_TOOLBOX_EXPORT auto zip(const Containers &... containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >
将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples
Definition functional_impl.hpp:452
Note
结果长度由最短的输入容器决定 / The length of result is determined by the shortest input container

◆ zip() [2/2]

template<typename... Containers>
auto toolbox::functional::zip ( const Containers &...  containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >

将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples

Template Parameters
Containers容器类型,必须提供size()和begin()/end() / Container types, must provide size() and begin()/end()
Parameters
containers输入容器 / Input containers
Returns
包含每个容器元素引用的元组向量 / Vector of tuples containing references to elements from each container
// 压缩不同类型的容器 / Zip containers of different types
std::vector<int> nums = {1, 2, 3};
std::vector<std::string> strs = {"a", "b", "c"};
std::array<double> dbls = {1.1, 2.2, 3.3};
auto zipped = zip(nums, strs, dbls);
// 返回元组向量 / Returns vector of tuples:
// {
// {1, "a", 1.1},
// {2, "b", 2.2},
// {3, "c", 3.3}
// }
Note
结果长度由最短的输入容器决定 / The length of result is determined by the shortest input container

◆ zip_to_unordered_map() [1/2]

template<typename ContainerKeys , typename ContainerValues , typename Key = typename std::decay_t<ContainerKeys>::value_type, typename Value = typename std::decay_t<ContainerValues>::value_type, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Alloc = std::allocator<std::pair<const Key, Value>>>
CPP_TOOLBOX_EXPORT auto toolbox::functional::zip_to_unordered_map ( const ContainerKeys &  keys,
const ContainerValues &  values 
) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >

将两个序列压缩成无序映射 / Zip two sequences into an unordered_map

Template Parameters
ContainerKeys键容器类型 / Key container type
ContainerValues值容器类型 / Value container type
Key映射键类型(默认:ContainerKeys::value_type) / Map key type (default: ContainerKeys::value_type)
Value映射值类型(默认:ContainerValues::value_type) / Map value type (default: ContainerValues::value_type)
Hash键哈希函数类型 / Key hash function type
KeyEqual键相等比较函数类型 / Key equality comparison function type
Alloc映射分配器类型 / Map allocator type
Parameters
keys键容器 / Container of keys
values值容器 / Container of values
Returns
包含键值对的无序映射 / Unordered map containing key-value pairs
// 基本用法 / Basic usage
std::vector<std::string> keys = {"a", "b", "c"};
std::vector<int> values = {1, 2, 3};
auto map = zip_to_unordered_map(keys, values);
// 返回无序映射 / Returns unordered_map:
// {
// {"a", 1},
// {"b", 2},
// {"c", 3}
// }
CPP_TOOLBOX_EXPORT auto zip_to_unordered_map(const ContainerKeys &keys, const ContainerValues &values) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >
将两个序列压缩成无序映射 / Zip two sequences into an unordered_map
Definition functional_impl.hpp:490
Note
如果键容器有重复,只使用第一次出现的键 / If keys container has duplicates, only the first occurrence is used
键值对数量由较短的容器决定 / The number of pairs is determined by the shorter container

◆ zip_to_unordered_map() [2/2]

template<typename ContainerKeys , typename ContainerValues , typename Key , typename Value , typename Hash , typename KeyEqual , typename Alloc >
auto toolbox::functional::zip_to_unordered_map ( const ContainerKeys &  keys,
const ContainerValues &  values 
) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >

将两个序列压缩成无序映射 / Zip two sequences into an unordered_map

Template Parameters
ContainerKeys键容器类型 / Key container type
ContainerValues值容器类型 / Value container type
Key映射键类型(默认:ContainerKeys::value_type) / Map key type (default: ContainerKeys::value_type)
Value映射值类型(默认:ContainerValues::value_type) / Map value type (default: ContainerValues::value_type)
Hash键哈希函数类型 / Key hash function type
KeyEqual键相等比较函数类型 / Key equality comparison function type
Alloc映射分配器类型 / Map allocator type
Parameters
keys键容器 / Container of keys
values值容器 / Container of values
Returns
包含键值对的无序映射 / Unordered map containing key-value pairs
// 基本用法 / Basic usage
std::vector<std::string> keys = {"a", "b", "c"};
std::vector<int> values = {1, 2, 3};
auto map = zip_to_unordered_map(keys, values);
// 返回无序映射 / Returns unordered_map:
// {
// {"a", 1},
// {"b", 2},
// {"c", 3}
// }
Note
如果键容器有重复,只使用第一次出现的键 / If keys container has duplicates, only the first occurrence is used
键值对数量由较短的容器决定 / The number of pairs is determined by the shorter container

Variable Documentation

◆ MemoizedFunction

template<typename Signature >
class CPP_TOOLBOX_EXPORT toolbox::functional::MemoizedFunction

缓存函数结果的记忆化函数类 / Memoized function class that caches function results

Template Parameters
Signature函数签名类型(如 int(int, std::string)) / Function signature type (e.g., int(int, std::string))
// 创建记忆化阶乘函数 / Create memoized factorial function
MemoizedFunction<int(int)> fact([](int n) {
if (n <= 1) return 1;
return n * fact(n-1);
});
int result1 = fact(5); // 计算并缓存 / Computes and caches
int result2 = fact(5); // 返回缓存结果 / Returns cached result
class CPP_TOOLBOX_EXPORT MemoizedFunction
缓存函数结果的记忆化函数类 / Memoized function class that caches function results
Definition functional.hpp:820
Note
线程安全实现 / Thread-safe implementation
函数参数必须可复制且能形成有效的映射键 / Function arguments must be copyable and form valid map key
返回类型必须可复制 / Return type must be copyable