Acclerated C ++: can I write a program that sorts either a list or a vector using the same command?

I understand that the std :: sort function requires the use of random access iterators, and lists have bidirectional iterators. There is a question in this question:

Sort List Using the STL Sort Function

I work to answer question 5-4 in the book Accelerated C ++ for home teaching purposes.

5-4. Look again at the driver features that you wrote in the previous exercise. Note that you can write a driver that differs only in the type declaration for the data structure that contains the input file. If your vector and list test drivers differ in any other way, rewrite them so that they differ only in this declaration.

Besides this tutorial question, it would be useful if I used templates instead of typedef to determine the type of container.

+4
source share
2 answers

... sorts either a list or a vector using the same command?

The easiest way is to overload:

template <typename T>
inline void sort(std::vector<T>& x) { std::sort(x.begin(), x.end()); }

template <typename T>
inline void sort(std::list<T>& x) { x.sort(); }

UPDATE

You mean that your text has not yet entered patterns, so here is an alternative without patterns. The question says that "differs only in type declaration for the data structure that contains the input file," and we are talking about vectorand list, therefore, there should be something like:

std::vector<std::string> input;

, std::string - , , , , vector list, typedef, ala input::element_type. , , vector list :

inline void sort(std::vector<input::element_type>& x) { std::sort(x.begin(), x.end()); }

inline void sort(std::list<input::element_type>& x) { x.sort(); }

, hardcode std::string , - , , .

inline void sort(std::vector<std::string>& x) { std::sort(x.begin(), x.end()); }

inline void sort(std::list<std::string>& x) { x.sort(); }
+9

, :

#include <algorithm>
#include <iterator>
#include <list>
#include <vector>

template <typename IterType>
struct sort
{
    template<typename Cont, typename Comp>
    inline static void impl(Cont &container, Comp F) {
        container.sort(F);
    }
    template<typename Cont>
    inline static void impl(Cont &container) {
        container.sort();
    }
};

template <>
struct sort<std::random_access_iterator_tag>
{
    template<typename Cont, typename Comp>
    inline static void impl(Cont &container, Comp F) {
        std::sort(std::begin(container), std::end(container), F);
    }
    template<typename Cont>
    inline static void impl(Cont &container) {
        std::sort(std::begin(container), std::end(container));
    }
};

template<typename Cont>
void Sort(Cont &container) {
    sort<typename std::iterator_traits<typename Cont::iterator
        >::iterator_category>::impl(container);
}

template<typename Cont, typename Comp>
void Sort(Cont &container, Comp F) {
    sort<typename std::iterator_traits<typename Cont::iterator
        >::iterator_category>::impl(container, F);
}

int main ()
{
    std::list<int> ml;
    std::vector<int> mv;

    Sort(ml);
    Sort(mv);
    Sort(ml, [](int a, int b) { return a > b; });
    Sort(mv, [](int a, int b) { return a > b; });

    return 0;
}

, list a vector, . , , , .

, , std::array std::deque ,

+5

Source: https://habr.com/ru/post/1534181/


All Articles