C++ 过滤向量并返回';n';th元素
我想写一个函数,它缓慢地计算并返回过滤向量的C++ 过滤向量并返回';n';th元素,c++,boost,vector,C++,Boost,Vector,我想写一个函数,它缓慢地计算并返回过滤向量的nth元素。大多数时候,我对向量中的第一个或第二个元素感兴趣。所以我不想过滤整个列表,然后找到nth元素 我正在学习使用Boost,如果有一个使用Boost的简单解决方案,那将是非常有启发性的 int main() { double x[] = {10, 12.5, 12.9, 13.7, 50.07}; size_t length = sizeof(x)/sizeof(x[0]); std::vector<double
n
th元素。大多数时候,我对向量中的第一个或第二个元素感兴趣。所以我不想过滤整个列表,然后找到n
th元素
我正在学习使用Boost,如果有一个使用Boost的简单解决方案,那将是非常有启发性的
int main() {
double x[] = {10, 12.5, 12.9, 13.7, 50.07};
size_t length = sizeof(x)/sizeof(x[0]);
std::vector<double> vx(x, x+length);
// Need a function to filter out elements less than 11 and return the 2nd
// element greater than 11 (here 12.9) and return without evaluating 13.7 and 50.07
return 0;
}
intmain(){
双x[]={10,12.5,12.9,13.7,50.07};
尺寸长度=sizeof(x)/sizeof(x[0]);
std::向量vx(x,x+长度);
//需要一个函数过滤掉小于11的元素并返回第二个元素
//元素大于11(此处为12.9),返回时未评估13.7和50.07
返回0;
}
使用
float n=11.f;
auto it=std::partition(vx.begin(),vx.end(),
[n] (const double&p){return p类似于使用
float n=11.f;
auto it=std::partition(vx.begin(),vx.end(),
[n] (const double&p){return p如果你对第一个元素感兴趣,我会天真地建议如下:
std::vector<double>::iterator element(size_t n, const std::vector<double>& vec) {
std::vector<double>::iterator it = vec.begin();
size_t found = 0;
while (found != n && it != vec.end()) {
if (*(it++) >= 11) ++found;
}
return it;
}
std::vector::迭代器元素(大小、常量std::vector&vec){
std::vector::iterator it=vec.begin();
找到的大小=0;
while(find!=n&&it!=vec.end()){
如果(*(it++)>=11)+找到;
}
归还它;
}
我有一个线性复杂度,但一旦找到所需的匹配项就会退出。如果您对第一个元素之一感兴趣,我会天真地建议如下:
std::vector<double>::iterator element(size_t n, const std::vector<double>& vec) {
std::vector<double>::iterator it = vec.begin();
size_t found = 0;
while (found != n && it != vec.end()) {
if (*(it++) >= 11) ++found;
}
return it;
}
std::vector::迭代器元素(大小、常量std::vector&vec){
std::vector::iterator it=vec.begin();
找到的大小=0;
while(find!=n&&it!=vec.end()){
如果(*(it++)>=11)+找到;
}
归还它;
}
我有一个线性复杂度,但一旦找到所需的匹配项就会退出。我不知道如何使用boost,但这就是我要使用的:
int binarySearch(int whatToSearch){
//recursive binary search
if value is found
return index
else //not found
return -index-1; //-index+1 will give you the place to insert that element
}
然后我将得到返回索引后的第二个元素(如果它存在)
下面是我使用的二进制搜索代码
int mybinary_search(string array[],int first,int last, string search_key){
int index;
if (first > last)
index = -first-1;
else{
int mid = (first + last)/2;
if (search_key == array[mid])
return mid;
else if (search_key < array[mid])
index = mybinary_search(array,first, mid-1, search_key);
else
index = mybinary_search(array, mid+1, last, search_key);
} // end if
return index;
}
double findAskedValue(int value){
double x[] = {10, 12.5, 12.9, 13.7, 50.07};
size_t length = sizeof(x)/sizeof(x[0]);
int index = mybinarySearch(x,0,length, 11);
if(index >= 0)
cout << "value I'm searching for " << x[index+2] << endl;
else
cout << "value I'm searching for " << x[(-index-1)+2] << endl;
//instead of cout's you can do what ever you want using that index
}
int-mybinary_搜索(字符串数组[],int-first,int-last,字符串搜索键){
整数指数;
如果(第一个>最后一个)
指数=-first-1;
否则{
int mid=(第一个+最后一个)/2;
if(search_key==数组[mid])
中途返回;
else if(search_key=0)
cout我不知道如何使用boost,但这就是我要使用的:
int binarySearch(int whatToSearch){
//recursive binary search
if value is found
return index
else //not found
return -index-1; //-index+1 will give you the place to insert that element
}
然后我将得到返回索引后的第二个元素(如果它存在)
下面是我使用的二进制搜索代码
int mybinary_search(string array[],int first,int last, string search_key){
int index;
if (first > last)
index = -first-1;
else{
int mid = (first + last)/2;
if (search_key == array[mid])
return mid;
else if (search_key < array[mid])
index = mybinary_search(array,first, mid-1, search_key);
else
index = mybinary_search(array, mid+1, last, search_key);
} // end if
return index;
}
double findAskedValue(int value){
double x[] = {10, 12.5, 12.9, 13.7, 50.07};
size_t length = sizeof(x)/sizeof(x[0]);
int index = mybinarySearch(x,0,length, 11);
if(index >= 0)
cout << "value I'm searching for " << x[index+2] << endl;
else
cout << "value I'm searching for " << x[(-index-1)+2] << endl;
//instead of cout's you can do what ever you want using that index
}
int-mybinary_搜索(字符串数组[],int-first,int-last,字符串搜索键){
整数指数;
如果(第一个>最后一个)
指数=-first-1;
否则{
int mid=(第一个+最后一个)/2;
if(search_key==数组[mid])
中途返回;
else if(search_key=0)
我想参数化过滤和我返回的n
th元素。@a看到更新的答案,你可以把它放在函数中,然后返回it
asstd::vector::iterator
我想参数化过滤和n
th元素。@a看到更新的答案,你可以把它放在函数中d返回it
作为std::vector::iterator
iteratorit
需要在while
循环中递增。在while
循环结束时添加++it
就足够了。迭代器it
需要在while
循环中递增。在while循环结束时添加++it
代码>循环就足够了。数组是否总是排序?是否希望元素严格优于11?@fjardon不总是排序。需要大于或等于11数组是否总是排序?是否希望元素严格优于11?@fjardon不总是排序。需要大于或等于11