1

I am a bit stuck on this, I'm using a std::array to store reference wrappers to vectors. I was trying to sort these by the vector's size using std::sort but am unable for reasons I am not quite sure of, even after reading the compiler errors. Will I have to use another sort function because it appears that std::sort implementations use operations that are unavailable for use on reference wrappers.

Thank you :)

Compiler explorer version

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <vector>
    #include <array>
    #include <string>
    #include <sstream>
    #include <utility>
    
    
    void findSum(const std::vector<int>& vec1, const std::vector<int>& vec2, const std::vector<int>& vec3)
    {    
        std::array<std::reference_wrapper<const std::vector<int>>, 3> vecRefs{vec1, vec2, vec3};
        
        std::sort(std::cbegin(vecRefs), std::cend(vecRefs), [](const auto vecA, const auto vecB) -> bool {
                return vecA.get().size() > vecB.get().size(); //descending order
            });
    
        //vecRefs should now be storing reference wrappers to the vectors with sizes in descending order
    
        //print longest vec first, shortest vec last
        for (const auto vec : vecRefs)
        {
            for (const auto val : vec.get())
            {
                std::cout << val << ' ';
            }
            std::cout << '\n';
        }
    
        //TODO: rest of function(the bit before this can be made into a function)
    }

3
  • You are passing const_iterator to sort. That can't work. Commented Mar 20, 2021 at 15:29
  • BTW you are missing the <functional> include for ref_wrapper Commented Mar 20, 2021 at 15:34
  • Thanks all, I was just unable to properly understand the error messages. Commented Mar 20, 2021 at 15:35

1 Answer 1

2

It's because you are using std::cbegin (const begin) and std::cend (const end) in std::sort.
It means, that you can't change the order of your array!
Just replace it with std::begin and std::end like this:

std::sort(std::begin(vecRefs), std::end(vecRefs), [](const auto vecA, const auto vecB) -> bool {
        return vecA.get().size() > vecB.get().size();
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.