How to copy references of elements of one vector to another vector?

gaazkam

I have a vector:

std::vector<island> sea;

And now I want to hold pointers to all elements of this vector in another vector. But just for a personal exercise I want to do this in a fancy C++ style, so instead of std::vector<const island*> p_sea I type:

std::vector<std::reference_wrapper<const island>> r_sea;

And now I'd like to populate this new vector with references:

std::transform(sea.begin(), sea.end(),
               std::back_inserter(r_sea),
               std::cref<island>
);

The way I understand it, from cppreference articles, the fourth argument of transform should be a function that takes const references to elements from the source range and returns elements of the destination range; and this is exactly what std::cref<island> does: it takes const island& as arguments and returns std::reference_wrapper<const island>. So I believe this should work??

However, it doesn't:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>


struct island {
    long long x,y; //coords
};

int main() {
    std::vector<island> sea;
    std::vector<std::reference_wrapper<const island>> r_sea;

    std::transform(sea.begin(), sea.end(),
                   std::back_inserter(r_sea),
                   std::cref<island>
    );

    return 0;
}

This results in the following compilation errors:

prog.cpp: In function ‘int main()’:
prog.cpp:19:5: error: no matching function for call to ‘transform(std::vector<island>::iterator, std::vector<island>::iterator, std::back_insert_iterator<std::vector<std::reference_wrapper<const island> > >, <unresolved overloaded function type>)’
     );
     ^
In file included from /usr/include/c++/6/algorithm:62:0,
                 from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4166:5: note: candidate: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)
     transform(_InputIterator __first, _InputIterator __last,
     ^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4166:5: note:   template argument deduction/substitution failed:
prog.cpp:19:5: note:   could not resolve address from overloaded function ‘cref<island>’
     );
     ^
In file included from /usr/include/c++/6/algorithm:62:0,
                 from prog.cpp:3:
/usr/include/c++/6/bits/stl_algo.h:4203:5: note: candidate: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
     transform(_InputIterator1 __first1, _InputIterator1 __last1,
     ^~~~~~~~~
/usr/include/c++/6/bits/stl_algo.h:4203:5: note:   template argument deduction/substitution failed:
prog.cpp:19:5: note:   could not resolve address from overloaded function ‘cref<island>’
     );

https://ideone.com/E80WXH

What am I doing wrong?

...I'm back to evil C pointers.

StoryTeller - Unslander Monica

std::cref is overloaded. Merely specifying the template argument is not enough to disambiguate between the overloads. You have two options:

  1. Cast it

    static_cast<std::reference_wrapper<const island>(*)(const island&)>(std::cref<island>)
    
  2. Lift the name up to a functor object (a lambda). like @Yakk proposed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Filter/copy a vector based on elements in another vector

Copy items from one vector to another vector

Copying the elements of one vector into another

How do I copy one vector to another vector in Java without updating the original vector?

How can I copy a vector mask from one document to another?

How can I copy one vector to another through different classes

Assigning elements of one vector to elements of another with R

Fastest way to copy one vector into another conditionally

copy one vector to another, omitting NA values

How to create a vector indicating matches between the elements of one vector and any element in another?

Find position of elements in one vector in another in MATLAB

Position of elements from one vector in another vector with R

Count values in a vector less than each one of the elements in another vector

how to copy a vector in std::vector<Eigen::vectorXd> to another vector in another std::vector<Eigen::vectorXd>

Copy vector element of a vector to another vector

How can I copy a vector to the end of another vector?

How to copy a vector to a subvector of another vector without segfaulting?

template function that uses n_copy to copy first n elements form one vector another causing a compilation error

Appending one vector to another vector

How do I remove the elements of vector that occur in another vector in Rust?

How to add elements in vector together based on info from another vector

Parallel algorithm to sum-assign the elements of a vector to the elements of another one

replace elements of one vector by elements of another on particular positions

How to sort elements of a vector due to another data?

How to quickly map a combination of elements to another vector

How to subtract one vector from another vector in r

How to move one vector into another vector without copying

How may I sort vectors for one vector by another vector?

How to merge lists of vectors based on one vector belonging to another vector?