1

我想从一个vector<foo>基于foo. 我boost::adaptors::transform用来选择成员,然后排序,然后使用boost::adaptors::unique. 我无法让排序步骤正常工作。暂且不谈unique,我在Coliru上尝试了以下代码。

#include <iostream>
#include <string>
#include <vector>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/sort.hpp>

struct foo
{
    foo(std::string a) : bar(a) {}

    std::string bar;
    bool operator<(const foo& rhs) const {return bar < rhs.bar;}
};

int main()
{
    std::vector<foo> words = { foo("z"), foo("d"), foo("b"), foo("c") };

    #if 1
    {
        auto asString = boost::adaptors::transform(words, +[](const foo& x) {return x.bar;});
        auto sortedStrings = boost::range::sort(asString);
        for (const auto& el : sortedStrings)
            std::cout << el << std::endl;
    }
    #else
    {
        auto asString = boost::adaptors::transform(words, +[](const foo& x) {return x.bar;});
        std::sort(asString.begin().base(), asString.end().base());
        for (const auto& el : asString)
            std::cout << el << std::endl;
    }

    {
        auto sortedStrings = boost::range::sort(words);
        for (const auto& el : sortedStrings)
            std::cout << el.bar << std::endl;
    }
    #endif


    return 0;
}

#if部分不起作用:

In file included from /usr/local/include/c++/6.3.0/bits/char_traits.h:39:0,
                 from /usr/local/include/c++/6.3.0/ios:40,
                 from /usr/local/include/c++/6.3.0/ostream:38,
                 from /usr/local/include/c++/6.3.0/iostream:39,
                 from main.cpp:1:
/usr/local/include/c++/6.3.0/bits/stl_algobase.h: In instantiation of 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>; _ForwardIterator2 = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>]':
/usr/local/include/c++/6.3.0/bits/stl_algo.h:84:20:   required from 'void std::__move_median_to_first(_Iterator, _Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/local/include/c++/6.3.0/bits/stl_algo.h:1918:34:   required from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/local/include/c++/6.3.0/bits/stl_algo.h:1950:38:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/local/include/c++/6.3.0/bits/stl_algo.h:1965:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]'
/usr/local/include/c++/6.3.0/bits/stl_algo.h:4707:18:   required from 'void std::sort(_RAIter, _RAIter) [with _RAIter = boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>]'
/usr/local/include/boost/range/algorithm/sort.hpp:33:14:   required from 'RandomAccessRange& boost::range::sort(RandomAccessRange&) [with RandomAccessRange = boost::range_detail::transformed_range<std::__cxx11::basic_string<char> (*)(const foo&), std::vector<foo> >]'
main.cpp:27:57:   required from here
/usr/local/include/c++/6.3.0/bits/stl_algobase.h:148:11: error: no matching function for call to 'swap(boost::iterators::detail::iterator_facade_base<boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>, std::__cxx11::basic_string<char>, boost::iterators::random_access_traversal_tag, std::__cxx11::basic_string<char>, long int, false, false>::reference, boost::iterators::detail::iterator_facade_base<boost::iterators::transform_iterator<std::__cxx11::basic_string<char> (*)(const foo&), __gnu_cxx::__normal_iterator<foo*, std::vector<foo> >, boost::iterators::use_default, boost::iterators::use_default>, std::__cxx11::basic_string<char>, boost::iterators::random_access_traversal_tag, std::__cxx11::basic_string<char>, long int, false, false>::reference)'
       swap(*__a, *__b);

而该部分中std::sort的基本迭代器类型#else确实有效。我不明白为什么。在我的实际用例中,我也在使用boost::adaptors::indirectand boost::adaptors::filter(我更愿意在进行排序之前进行过滤,或者至少尝试这样做并查看它的执行情况),这就是为什么我不简单地words排序进行转换之前的 lambda 谓词。

4

1 回答 1

2

问题是您可以查看无法交换的临时字符串。您可以使用以下方法修复您的代码:

auto asString = boost::adaptors::transform(words, +[](foo& x) -> std::string& {return x.bar;});

演示

请注意,您可以直接对字符串进行排序,而不是对类进行排序。

于 2017-03-12T18:05:24.297 回答