gpt4 book ai didi

c++ - 理解 C++ 中的迭代器和运算符重载的问题

转载 作者:行者123 更新时间:2023-11-28 01:10:58 28 4
gpt4 key购买 nike

我们有一个类示例,但我就是不明白。我不太明白 operator() 在这种情况下是如何工作的,以及所有以排序开头的东西。我在运行程序后查看了输出,但没有看到这些值是如何获得的。

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() );

您正在创建一个 vector 来保存 vector 中元素的索引 vecNum .显然,此 vector 中的元素数与 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 中的每个元素 vector 调用仿函数 IndexCompare .这个仿函数在其 operator() 中从 vecNum 获取值对应于给定索引位置的 vector 。所以基本上你正在对 indices 进行排序 vector (不是 vecNum )基于 vecNum 中的值.因此 vecNum保持不受影响并且indices根据 vecNum 中的值进行排序.

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

现在您正在调用 std::sort用你自己的仿函数。所以判断0是否小于1 sort算法将使用你的仿函数。在仿函数内部,您使用逻辑是否 vecNum[0](即 20)< vecNum[1](即 10)来确定 0 < 1。因此,排序后的 put 将是 indices = 1,0,2。

关于c++ - 理解 C++ 中的迭代器和运算符重载的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3287716/

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