我们有一个课堂示例,但我不明白。在这种情况下,我不太了解operator()的工作方式,而一切都从sort开始。运行程序后,我查看了输出,但看不到如何获得这些值。

sort indices array: 2 8 10 4 1 7 5 3 0 9 6 11
replay numbers array: 37 33 29 36 32 35 39 34 30 38 31 40
number array via indices 29 30 31 32 33 34 35 36 37 38 39 40


由于标题是仿函数示例,因此我尝试在此板上查找仿函数,但我想我看不到仿函数在这里的作用。我完全迷失了任何想法。谢谢!

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

#include "IndexCompare.h"

using namespace std;

template <class ForwardIterator, class T>
void iota(ForwardIterator first, ForwardIterator last, T value) {
     while (first != last) {
        *first++ = value++;
     }
}

const int MAX = 12;

int main() {
    int numbers[] = {37, 33, 29, 36, 32, 35, 39, 34, 30, 38, 31, 40};

    vector<int> vecNum(numbers, numbers + MAX);

    // Display original number array.
    cout << "--- initial numbers array ---" << endl;

    vector<int>::iterator iter = vecNum.begin();
    for (; iter != vecNum.end(); iter++ ) {
        cout << *iter << " ";
    }
    cout << "\n";

    vector<int> indices( vecNum.size() );

    // fill indices array
    cout << "\n--- invoke 'iota' on indices array ---";
    iota( indices.begin(), indices.end(), 0 );

    // Display original indices array.
    cout << "\n linear indices array: ";
    vector<int>::iterator iterIdx = indices.begin();
    for (; iterIdx != indices.end(); iterIdx++ ) {
        cout << *iterIdx << " ";
    }
    cout << "\n";

    // sort indices array
    cout << "\n--- invoke 'Sort' on indices based on number array ---";
    sort(indices.begin(), indices.end(),
          IndexCompare<vector<int>::iterator>(vecNum.begin(),vecNum.end()));

    // Display sorted indices array
    cout << "\n Sorted indices array: ";
    for (iterIdx = indices.begin(); iterIdx != indices.end(); iterIdx++ ) {
        cout << *iterIdx << " ";
    }
    cout << "\n";

    cout << "\n--- Run check on number array indexed normally ---";
    // Display original numbers array.
    cout << "\n replay numbers array: ";
    iter = vecNum.begin();
    for (; iter != vecNum.end(); iter++ ) {
        cout << *iter << " ";
    }
    cout << "\n";

    cout << "\n--- Run check on number array indexed with sorted indices ---";
    // Print original nums array indirectly through indices.
    cout << "\n number array via indices: ";
    for (int index = 0; index < vecNum.size(); index++ )
        cout << vecNum[indices[index]] << " ";
    cout << "\n";

    getchar();

    return 0;
}

// IndexCompare.h - interface for IndexCompare class template
#ifndef _INDEXCOMPARE_H_
#define _INDEXCOMPARE_H_
#pragma once

template <class random_iterator>
class IndexCompare {
public:
    IndexCompare(random_iterator begin, random_iterator end)
        : begin(begin), end(end) {}

    ~IndexCompare() {}

    bool operator() (unsigned int first, unsigned int second) {
            return (*(begin + first) < *(begin + second));

    }

private:
    random_iterator begin;
    random_iterator end;
};

#endif

最佳答案

我不确定我能否正确解释这一点。这是我的尝试:

(1)。 vector<int> indices( vecNum.size() );

您正在创建一个向量来保存向量vecNum中元素的索引。显然,此向量中的元素数与vecNum中的元素数相同。

(2)。 iota( indices.begin(), indices.end(), 0 );

indices中的值初始化0 - vecNum.size() - 1

(3)。

sort(indices.begin(), indices.end(),
              IndexCompare<vector<int>::iterator>(vecNum.begin(),vecNum.end()));


对于indices向量中的每个元素,调用函子IndexCompare。此函子在其operator()中从与给定索引位置相对应的vecNum向量中获取值。因此,基本上,您是根据indices中的值对vecNum向量(不是vecNum)进行排序。因此,vecNum保持不受影响,并且indices根据来自vecNum的值进行排序。

为了更清楚(我希望),索引向量的初始状态为:
指数= 0,1,2
和vecNum = 20,10,30

现在,您要使用自己的函子来调用std::sort。因此,确定0是否小于1 sort算法将使用您的函子。在函子内部,您使用逻辑是否vecNum [0](即20)
10-08 02:10