Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用std::sort对迭代器进行排序_C++_Sorting - Fatal编程技术网

C++ 使用std::sort对迭代器进行排序

C++ 使用std::sort对迭代器进行排序,c++,sorting,C++,Sorting,我想对包含指向另一个向量int_vec中元素的int迭代器的向量vec进行排序。我想使用以下比较函数:it1

我想对包含指向另一个向量int_vec中元素的int迭代器的向量vec进行排序。我想使用以下比较函数:it1
index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()]. 
index[it1-int_vec.begin()]
其中,index是指定迭代器键的第三个向量。现在,向量索引是A的构造函数的内部数组,int_vec是类A的成员变量。我试图传递一个匿名函数,如下所示:

std::sort(vec.begin(),flow.end(), [&index,&edges](const int_iter it1 ,const int_iter it2) -> bool
{ 
    index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()]; 
})
std::sort(vec.begin(),flow.end(),[&index,&edges](常量整数it1,常量整数it2)->bool
{ 
索引[it1-int_vec.begin()]
但是我得到一个错误,告诉我无法捕获成员对象。确切的错误消息是:

'this' cannot be implicitly captured in this context
        index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()];.
“this”不能在此上下文中隐式捕获
索引[it1-int_vec.begin()]

我还试图只声明一个外部比较函数,但我不清楚如何将两个固定值绑定到它(我读到了boost::bind,它看起来正好解决了这个问题,但我不想下载其他库)

你有很多问题

  • 最明显的一点是,您的代码缺少
    [此]

  • vec.begin()、flow.end()

  • 不能取一个向量的开头和另一个向量的结尾

    这是正确的代码:

    std::sort(vec.begin(),vec.end(), [this,&index,&edges](const int_iter it1 ,const int_iter it2) -> bool
    { 
        index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()]; 
    })
    
    排序(vec.begin(),vec.end(),[this,&index,&edges](常量整数it1,常量整数it2)->bool { 索引[it1-int_vec.begin()] 不过,你应该告诉我们你正在努力实现什么,我相信我们可以找到更好的解决方案。使用其他向量的迭代器的向量已经非常危险了,在没有检查的情况下对它们进行减法是很粗心的

    不太危险的解决方案:

    std::vector<int> int_vec;
    std::vector<size_t> int_vec_order(int_vec.size());
    std::iota(int_vec_order.begin(), int_vec_order.end(), size_t(0));
    
    std::sort(int_vec_order.begin(), int_vec_order.end(), [&int_vec](const size_t a, const size_t b) {
      // apply your order to int_vec.at(a) and int_vec.at(b)
    });
    
    // output them
    for(const size_t i : int_vec_order) {
      // output int_vec.at(i)
    }
    
    std::vector int\u vec;
    std::vector int_vec_order(int_vec.size());
    std::iota(int_vec_order.begin()、int_vec_order.end()、size_t(0));
    排序(int_vec_order.begin()、int_vec_order.end()、[&int_vec](常量大小a、常量大小b){
    //将您的订单应用于int_vec.at(a)和int_vec.at(b)
    });
    //输出它们
    用于(常量大小:整数向量顺序){
    //输出整数向量(i)
    }
    
    您在这方面有很多问题

  • 最明显的一点是,您的代码缺少
    [此]

  • vec.begin()、flow.end()

  • 不能取一个向量的开头和另一个向量的结尾

    这是正确的代码:

    std::sort(vec.begin(),vec.end(), [this,&index,&edges](const int_iter it1 ,const int_iter it2) -> bool
    { 
        index[it1 - int_vec.begin()] < index[it2 - int_vec.begin()]; 
    })
    
    排序(vec.begin(),vec.end(),[this,&index,&edges](常量整数it1,常量整数it2)->bool { 索引[it1-int_vec.begin()] 不过,你应该告诉我们你正在努力实现什么,我相信我们可以找到更好的解决方案。使用其他向量的迭代器的向量已经非常危险了,在没有检查的情况下对它们进行减法是很粗心的

    不太危险的解决方案:

    std::vector<int> int_vec;
    std::vector<size_t> int_vec_order(int_vec.size());
    std::iota(int_vec_order.begin(), int_vec_order.end(), size_t(0));
    
    std::sort(int_vec_order.begin(), int_vec_order.end(), [&int_vec](const size_t a, const size_t b) {
      // apply your order to int_vec.at(a) and int_vec.at(b)
    });
    
    // output them
    for(const size_t i : int_vec_order) {
      // output int_vec.at(i)
    }
    
    std::vector int\u vec;
    std::vector int_vec_order(int_vec.size());
    std::iota(int_vec_order.begin()、int_vec_order.end()、size_t(0));
    排序(int_vec_order.begin()、int_vec_order.end()、[&int_vec](常量大小a、常量大小b){
    //将您的订单应用于int_vec.at(a)和int_vec.at(b)
    });
    //输出它们
    用于(常量大小:整数向量顺序){
    //输出整数向量(i)
    }
    

    我想对包含指向另一个向量int_-vec中元素的int迭代器的向量vec进行排序——仅此一点是个坏主意,因为如果调整向量的大小,向量迭代器将无效。但是向量永远不会调整大小为什么它是向量?使用
    std::array
    'member objects cannot capture',听起来你需要捕获这个,只是
    [this]
    请在你的问题中添加准确的错误消息。我想对一个向量向量向量向量进行排序,该向量向量包含指向另一个向量int\u vec中元素的int迭代器——仅仅这是一个坏主意,如果向量被调整大小,那么向量迭代器将无效。但是向量从未被调整大小。那么它是一个向量吗?使用
    std::array
    'member objects cannot capture',听起来你需要捕获这个,只需
    [this]
    请在你的问题中添加准确的错误消息。哦,流是一个输入错误。所以这是虚构的。实际上,int_vec不存储int,而是存储非常大的结构,我不想复制它们,所以我使用迭代器。也许这样说会更好。我想做以下几件事:我在int_vec中以预定义的顺序存储一些对象,然后在其中存储一些其他对象。然后我对int_vec进行排序(按一些不重要的顺序)。现在我想按预定义的顺序打印对象(但不是int_vec中的其他对象)。编辑答案。您总是可以使用危险性较小的索引。@user3726947解决方案是使用索引,而不是迭代器。这不正是我写的吗?你的编辑没有准确显示如何编写排序的lambda部分。哦,流的东西是一个打字错误。所以这是虚构的。实际上,int_vec不存储int,而是存储非常大的结构,我不想复制它们,所以我使用迭代器。也许这样说会更好。我想做以下几件事:我在int_vec中以预定义的顺序存储一些对象,然后在其中存储一些其他对象。然后我对int_vec进行排序(按一些不重要的顺序)。现在我想按预定义的顺序打印对象(但不是int_vec中的其他对象)。编辑答案。您总是可以使用危险性较小的索引。@user3726947解决方案是使用索引,而不是迭代器。这不正是我写的吗?您的编辑没有准确显示如何编写排序的lambda部分。