C++ 两排序数组实现中的第k个元素
我正在尝试将著名的两个排序数组算法的第k个元素C++ 两排序数组实现中的第k个元素,c++,iterator,C++,Iterator,我正在尝试将著名的两个排序数组算法的第k个元素c++实现应用到我的应用程序中(第二个数组按递增顺序排序)。现在,我可以看到两种解决方案: 用反向迭代器替换B(第二个数组)元素的迭代器 尝试手动执行此操作,例如,用减量替换B指针的增量 现在,我开始尝试2)不是因为不喜欢而是因为我不太擅长使用反向迭代器。然而,我的“解决方案”不起作用。我想知道应该如何更改的代码以对第二个数组使用反向迭代器 #include <cmath> #include <ctime> #include
c++
实现应用到我的应用程序中(第二个数组按递增顺序排序)。现在,我可以看到两种解决方案:
B
(第二个数组)元素的迭代器B
指针的增量#include <cmath>
#include <ctime>
#include <functional>
#include <fstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <vector>
#include <random>
#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <numeric>
#define SIZE(a) (sizeof(a)/sizeof(*a))
#define NDEBUG
using namespace std;
template<class RandomAccessIterator, class Compare>
typename std::iterator_traits<RandomAccessIterator>::value_type
nsmallest_iter(RandomAccessIterator firsta,RandomAccessIterator lasta,RandomAccessIterator firstb,RandomAccessIterator lastb,size_t n,Compare less){//https://stackoverflow.com/questions/4607945/how-to-find-the-kth-smallest-element-in-the-union-of-two-sorted-arrays/11698659#11698659
assert(std::is_sorted(firsta,lasta,less) && std::is_sorted(firstb,lastb,less));
const float x_0=*firsta,x_1=*lastb;
std::cout << x_0 << std::endl;
std::cout << x_1 << std::endl;
for(;;){
assert(n<static_cast<size_t>((lasta-firsta)+(lastb-firstb)));
if(firsta==lasta) return *(firstb+n);
if(firstb==lastb) return *(firsta+n);
size_t mida=(lasta-firsta)/2;
size_t midb=(lastb-firstb)/2;
if((mida+midb)<n){
if(less(*(firstb+midb),*(firsta+mida))){
firstb+=(midb+1);
n-=(midb+1);
} else {
firsta+=(mida+1);
n-=(mida+1);
}
} else {
if(less(*(firstb+midb),*(firsta+mida))){
lasta=(firsta+mida);
} else {
lastb=(firstb+midb);
}
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义尺寸(a)(尺寸(a)/尺寸(*a))
#定义NDEBUG
使用名称空间std;
模板
typename std::迭代器特征::值类型
nsmallest_iter(RandomAccessIterator firsta,RandomAccessIterator lasta,RandomAccessIterator firstb,RandomAccessIterator lastb,大小,比较较小){//https://stackoverflow.com/questions/4607945/how-to-find-the-kth-smallest-element-in-the-union-of-two-sorted-arrays/11698659#11698659
assert(std::is_sorted(firsta,lasta,less)和&std::is_sorted(firstb,lastb,less));
常量浮点x_0=*firsta,x_1=*lastb;
std::cout使用反向迭代器,只需对所提供的代码进行一些小的调整:
template<class RandomAccessIterator1, typename RandomAccessIterator2, class Compare>
typename std::iterator_traits<RandomAccessIterator1>::value_type
nsmallest_iter(RandomAccessIterator1 firsta, RandomAccessIterator1 lasta,
RandomAccessIterator2 firstb, RandomAccessIterator2 lastb,
size_t n,
Compare less) {
...
}
模板
typename std::迭代器特征::值类型
nsmallest_iter(RandomAccessIterator 1 firsta,RandomAccessIterator 1 lasta,
RandomAccessIterator2FirstB,RandomAccessIterator2LastB,
尺寸,
比较少){
...
}
及
intv=nsmallest\u iter(
a、 a+尺寸(a),
std::reverse_迭代器(b+SIZE(b)),std::reverse_迭代器(b),
尺寸(a)+尺寸(b)-1-i,
std::greater());
template<class RandomAccessIterator1, typename RandomAccessIterator2, class Compare>
typename std::iterator_traits<RandomAccessIterator1>::value_type
nsmallest_iter(RandomAccessIterator1 firsta, RandomAccessIterator1 lasta,
RandomAccessIterator2 firstb, RandomAccessIterator2 lastb,
size_t n,
Compare less) {
...
}
int v = nsmallest_iter(
a, a + SIZE(a),
std::reverse_iterator<int*>(b + SIZE(b)), std::reverse_iterator<int*>(b),
SIZE(a)+SIZE(b)-1-i,
std::greater<int>());