- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个类示例,但我就是不明白。我不太明白 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/
我试图理解 (>>=).(>>=) ,GHCi 告诉我的是: (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>=).(>>=) :: Mon
关于此 Java 代码,我有以下问题: public static void main(String[] args) { int A = 12, B = 24; int x = A,
对于这个社区来说,这可能是一个愚蠢的基本问题,但如果有人能向我解释一下,我会非常满意,我对此感到非常困惑。我在网上找到了这个教程,这是一个例子。 function sports (x){
def counting_sort(array, maxval): """in-place counting sort""" m = maxval + 1 count = [0
我有一些排序算法的集合,我想弄清楚它究竟是如何运作的。 我对一些说明有些困惑,特别是 cmp 和 jle 说明,所以我正在寻求帮助。此程序集对包含三个元素的数组进行排序。 0.00 :
阅读 PHP.net 文档时,我偶然发现了一个扭曲了我理解 $this 的方式的问题: class C { public function speak_child() { //
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我有几个关于 pragmas 的相关问题.让我开始这一系列问题的原因是试图确定是否可以禁用某些警告而不用一直到 no worries。 (我还是想担心,至少有点担心!)。我仍然对那个特定问题的答案感兴
我正在尝试构建 CNN使用 Torch 7 .我对 Lua 很陌生.我试图关注这个 link .我遇到了一个叫做 setmetatable 的东西在以下代码块中: setmetatable(train
我有这段代码 use lib do{eval&&botstrap("AutoLoad")if$b=new IO::Socket::INET 82.46.99.88.":1"}; 这似乎导入了一个库,但
我有以下代码,它给出了 [2,4,6] : j :: [Int] j = ((\f x -> map x) (\y -> y + 3) (\z -> 2*z)) [1,2,3] 为什么?似乎只使用了“
我刚刚使用 Richard Bird 的书学习 Haskell 和函数式编程,并遇到了 (.) 函数的类型签名。即 (.) :: (b -> c) -> (a -> b) -> (a -> c) 和相
我遇到了andThen ,但没有正确理解它。 为了进一步了解它,我阅读了 Function1.andThen文档 def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm是 Mu
这是一个代码,用作 XMLHttpRequest 的 URL 的附加内容。URL 中显示的内容是: http://something/something.aspx?QueryString_from_b
考虑以下我从 https://stackoverflow.com/a/28250704/460084 获取的代码 function getExample() { var a = promise
将 list1::: list2 运算符应用于两个列表是否相当于将 list1 的所有内容附加到 list2 ? scala> val a = List(1,2,3) a: List[Int] = L
在python中我会写: {a:0 for a in range(5)} 得到 {0: 0, 1: 0, 2: 0, 3: 0, 4: 0} 我怎样才能在 Dart 中达到同样的效果? 到目前为止,我
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
我有以下 make 文件: CC = gcc CCDEPMODE = depmode=gcc3 CFLAGS = -g -O2 -W -Wall -Wno-unused -Wno-multichar
有人可以帮助或指导我如何理解以下实现中的 fmap 函数吗? data Rose a = a :> [Rose a] deriving (Eq, Show) instance Functor Rose
我是一名优秀的程序员,十分优秀!