2010-11-05 152 views
0

我试图使用boost::bindstd::sort函数。我想绑定到一个不带参数的函数中,并指定一个int数组进行排序。使用std :: sort和boost :: bind

我遇到了绑定排序时如何指定为模板参数的问题,如果我使用的是标准容器,我只是使用std::container<int>::iterator,但由于我使用的是数组,因此我无法弄清楚什么使用。

下面是一些代码,说明我的编译问题:

const int NUM_VALUES = 100; 
    int nums[NUM_VALUES]; 
    for (int i=0; i<NUM_VALUES; ++i) 
    { 
    nums[i] = NUM_VALUES - i; 
    } 
    boost::function<void()> sortHolder = boost::bind(&std::sort<type?>, nums, nums+NUM_VALUES); 

任何人都知道在这里用什么模板参数?任何其他讨论也欢迎。

回答

2

当您想将“迭代器”放入数组中时,可以使用指向数组中元素的指针。这样的指针可以用作随机访问迭代器:

boost::function<void()> sortHolder = 
    boost::bind(&std::sort<int*>, nums, nums + NUM_VALUES); 
+0

你试过编译这个吗? – wilhelmtell 2010-11-05 02:42:27

+0

@wilhelmtell:整理。我没有对我的lappy进行Boost,因此我使用了Visual C++ 2010'',并在答案中将'std ::'替换为'boost ::'。我把东西弄脏了吗? (真诚的道歉,如果我这样做的话:'(如果是这样,请随时发布更正,我会删除这个并加快修正) – 2010-11-05 02:44:19

+0

@James这里的问题是,std :: sort <>'甚至没有解决'std :: sort '。 – wilhelmtell 2010-11-05 02:45:39