gpt4 book ai didi

c++ - boost 绑定(bind)到 operator[]

转载 作者:搜寻专家 更新时间:2023-10-30 23:59:30 24 4
gpt4 key购买 nike

我正在尝试对二维数组 vector<vector<int> > a(M,vector<int>(N)) 进行排序就其第 n 列而言,按行排列,如下所示:

sort(a.begin(),a.end(),
(bind(&vector<int>::operator[],_1,n) >
bind(&vector<int>::operator[],_2,n)));

但是我的编译器告诉我

error: no matching function for call to ‘bind(<unresolved overloaded function type>, const boost::lambda::placeholder1_type&, int)’
error: no matching function for call to ‘bind(<unresolved overloaded function type>, const boost::lambda::placeholder2_type&, int)’

我应该如何解决这个问题?

PS.: 尝试了一个更简单的前面访问 operator[] 的版本

  vector<int> a(10);
(bind(&vector<int>::operator[],_1,_2))(a,2);

这是直接从 Karlsson 的书中复制、剪切和粘贴的内容。得到

error: no matching function for call to ‘bind(<unresolved overloaded function type>, const boost::lambda::placeholder1_type&, const boost::lambda::placeholder2_type&)’

也为了那个...

最佳答案

借助 Boost.Phoenix,您可以使用 @sellibitze 在评论中提到的内容:

#include <iostream>
#include <vector>

#include <boost/phoenix.hpp>

namespace phx=boost::phoenix;

int main()
{
std::vector<std::vector<int>> matrix
{
{1, 2, 3, 4},
{4, 3, 4, 1},
{9, 1, 0, 2},
{3, 1, 5, 1}
};

const auto N = 2;

using phx::arg_names::_1;
using phx::arg_names::_2;

std::sort( matrix.begin(), matrix.end(), _1[N] > _2[N] );

for(const auto& row: matrix)
{
for(const auto& elem: row)
std::cout << elem << ' ';

std::cout << std::endl;
}

return 0;
}

关于c++ - boost 绑定(bind)到 operator[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16260445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com