std :: set_intersection的struct对象

Fal*_*ise -1 c++ containers stl const set

我试图获得的存储类型被称为结构的对象在两个集合的交集dist,使用std::set_intersection.我希望结果存储在另一个中set<dist>.

但是,编译器会出现以下错误:

In file included from /usr/include/c++/5/algorithm:62:0,
             from /usr/include/x86_64-linux-gnu/c++/5/bits/stdc++.h:64,
             from /home/kirill/CLionProjects/contest/main.cpp:1:
/usr/include/c++/5/bits/stl_algo.h: In instantiation of ‘_OutputIterator std::__set_intersection(_InputIterator1, _InputIterator1, _InputIterator2, _InputIterator2, _OutputIterator, _Compare) [with _InputIterator1 = std::_Rb_tree_const_iterator<dist>; _InputIterator2 = std::_Rb_tree_const_iterator<dist>; _OutputIterator = std::_Rb_tree_const_iterator<dist>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’:
/usr/include/c++/5/bits/stl_algo.h:5122:48:   required from ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator<dist>; _IIter2 = std::_Rb_tree_const_iterator<dist>; _OIter = std::_Rb_tree_const_iterator<dist>]’
/home/kirill/CLionProjects/contest/main.cpp:65:73:   required from here
/usr/include/c++/5/bits/stl_algo.h:5076:16: error: passing ‘const dist’ as ‘this’ argument discards qualifiers [-fpermissive]
      *__result = *__first1;
            ^
/home/kirill/CLionProjects/contest/main.cpp:26:8: note:   in call to ‘dist& dist::operator=(const dist&)’
 struct dist {
    ^
CMakeFiles/contest.dir/build.make:62: recipe for target 'CMakeFiles/contest.dir/main.cpp.o' failed
make[3]: *** [CMakeFiles/contest.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/contest.dir/all' failed
make[2]: *** [CMakeFiles/contest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/contest.dir/rule' failed
make[1]: *** [CMakeFiles/contest.dir/rule] Error 2
Makefile:118: recipe for target 'contest' failed
make: *** [contest] Error 2
Run Code Online (Sandbox Code Playgroud)

这是一个结构定义:

struct dist {
    int x_dist;
    int y_dist;

    bool operator<(dist const & b) const {
        return tie(x_dist, y_dist) < tie(b.x_dist, b.y_dist);
    }
};
Run Code Online (Sandbox Code Playgroud)

这里我正在调用一个set_intersection方法:

#define all(c) (c).begin(), (c).end()
void intersection_taxi_fan() {
    intersection.clear();
    set_intersection(all(taxi_dist), all(fan_dist), intersection.begin());
}
Run Code Online (Sandbox Code Playgroud)

raf*_*x07 6

您不能intersection.begin()作为参数传递,set_intersection因为它是const值的迭代器

*注意:设置中的所有迭代器都指向const元素.[来自http://www.cplusplus.com/reference/set/set/]

set_intersection修改set_intersection此迭代器指向的值(在算法的下面代码段中的行[1]中).

    else if (*first2<*first1) ++first2;
else {
  *result = *first1;   // [1] try modifying const value
  ++result; ++first1; ++first2;
}
Run Code Online (Sandbox Code Playgroud)

无法修改Const对象.

您可以使用std::inserter将元素插入到intersection集合中

    set_intersection(all(taxi_dist), all(fan_dist), inserter(intersection, intersection.end()));
Run Code Online (Sandbox Code Playgroud)

  • 与const或非const相关的事实是,在调用`set :: clear()`之后,`set :: begin()`不会返回有效的迭代器.所以OPs代码是错误的,即使`set :: begin()`会将迭代器返回到非const值.您的解决方案是正确的,但可以改进解释. (2认同)