The std::transform-like function that returns transformed container

后端 未结 5 929
心在旅途
心在旅途 2020-12-04 21:59

I\'m trying to implement a function similar to std::transform algorithm but instead of taking the output iterator by an argument I want to create and return a c

5条回答
  •  无人及你
    2020-12-04 22:19

    Some remarks

    The following method allows to transform containers of any type from the standard library (there is a problem with std::array, see below). The only requirement for the container is that it should use default std::allocator classes, std::less, std::equal_to and std::hash function objects. So we have 3 groups of containers from the standard library:

    1. Containers with one non-default template type parameter (type of value):

      • std::vector, std::deque, std::list, std::forward_list, [std::valarray]
      • std::queue, std::priority_queue, std::stack
      • std::set, std::unordered_set
    2. Containers with two non-default template type parameters (type of key and type of value):

      • std::map, std::multi_map, std::unordered_map, std::unordered_multimap
    3. Container with two non-default parameters: type parameter (type of value) and non-type parameter (size):

      • std::array

    Implementation

    convert_container helper class convert types of known input container type (InputContainer) and output value type (OutputType) to the type of the output container(typename convert_container::type):

    template 
    struct convert_container;
    
    // conversion for the first group of standard containers
    template