C++中nth_element函数的用法

本文详细介绍了C++标准库中的std::nth_element函数,该函数能够将指定位置的元素调整到其在完全排序序列中应有的位置,同时确保该元素之前的所有元素都不大于它,之后的所有元素都不小于它。文章通过示例代码展示了如何使用此函数。

std::nth_element

<algorithm>
template <class RandomAccessIterator>
  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void nth_element ( RandomAccessIterator first, RandomAccessIterator nth,
                     RandomAccessIterator last, Compare comp );
Sort element in range

Rearranges the elements in the range [first,last), in such a way that the element at the resulting nth position is the element that would be in that position in a sorted sequence, with none of the elements preceding it being greater and none of the elements following it smaller than it. Neither the elements preceding it nor the elements following it are guaranteed to be ordered.

The elements are compared using operator< for the first version, and comp for the second.

Parameters

first, last
Random-Access iterators to the initial and final positions of the sequence to be used. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
Notice that in this function, these are not consecutive parameters, but the first and third ones.
nth
Random-Access iterator pointing to the location within the range [first,last) that will have the sorted element.
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.


 

// alg_nth_elem.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>      // For greater<int>( )
#include <iostream>

// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 ) {
 return elem1 > elem2;
}

int main() {
 using namespace std;
 vector <int> v1;
 vector <int>::iterator Iter1;

 int i;
 for ( i = 0 ; i <= 5 ; i++ )
  v1.push_back( 3 * i );

 int ii;
 for ( ii = 0 ; ii <= 5 ; ii++ )
  v1.push_back( 3 * ii + 1 );

 int iii;
 for ( iii = 0 ; iii <= 5 ; iii++ )
  v1.push_back( 3 * iii +2 );

 cout << "Original vector:\n v1 = ( " ;
 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  cout << *Iter1 << " ";
 cout << ")" << endl;

 nth_element(v1.begin( ), v1.begin( ) + 3, v1.end( ) );
 //
 //nth_element(v1.begin( ), v1.begin( ) + 18, v1.end( ) );
 //cout << "Position 18 partitioned vector:\n v1 = ( " ;
 cout << "Position 3 partitioned vector:\n v1 = ( " ;
 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  cout << *Iter1 << " ";
 cout << ")" << endl;
 cout<<v1[3]<<endl;       //输出第3大的元素
 // To sort in descending order, specify binary predicate
 nth_element( v1.begin( ), v1.begin( ) + 4, v1.end( ),
  greater<int>( ) );  //greater<int>由大到小排序
 cout << "Position 4 partitioned (greater) vector:\n v1 = ( " ;
 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  cout << *Iter1 << " ";
 cout << ")" << endl;
 cout<<v1[4]<<endl;   //输出第4大的元素(从0开始排的)

 random_shuffle( v1.begin( ), v1.end( ) );
 cout << "Shuffled vector:\n v1 = ( " ;
 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  cout << *Iter1 << " ";
 cout << ")" << endl;

 // A user-defined (UD) binary predicate can also be used
 nth_element( v1.begin( ), v1.begin( ) + 5, v1.end( ), UDgreater );
 cout << "Position 5 partitioned (UDgreater) vector:\n v1 = ( " ;
 for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
  cout << *Iter1 << " ";
 cout << ")" << endl;
 cout<<v1[5]<<endl;  //输出第5大的元素,第0大,第1大....
}

测试结果:

nth_element只是将位置为n(从0开始)的元素放到第n大的位置,处理完之后,默认排在它前面的元素都不比它大,排在它后面的元素都不比它小,并且有v[n]分开的两段,各段内部,没有进行明显的排序,但是在vs里面,却将所有的顺序都给排好了。加入comp函数之后,按comp函数的方式进行比较

引用中的代码是一个Vue文件中的抽屉关闭方法。在Vue2和Element UI中,抽屉是通过el-drawer组件来实现的。要使用el-drawer组件,首先需要在Vue文件中引入Element UI的样式和组件库。然后可以在template中使用el-drawer组件来创建一个抽屉,通过设置相应的属性来控制抽屉的显示和隐藏。 在给出的代码中,handleClose()方法是用来关闭抽屉的。首先它通过comparison()函数来比较this.$refs.comparisonRef中的arr数组是否包含"tableData"这个值。如果包含,则直接将抽屉关闭,即将this.drawerVisible设置为false。如果不包含,则会弹出一个确认框,询问用户是否关闭抽屉,如果用户点击确认,则将抽屉关闭,即将this.drawerVisible设置为false。 需要注意的是,在抽屉关闭之前,可能会有数据改动。因此在关闭抽屉时会有一个确认框来提示用户是否保存数据。如果用户点击取消,则抽屉不会关闭。如果用户点击确认,则抽屉将被关闭。 总结来说,这段代码是一个Vue2和Element UI中的抽屉关闭方法,根据数据的改动情况来判断是否关闭抽屉,并提供了确认框来提示用户保存数据的选择。<span class="em">1</span> #### 引用[.reference_title] - *1* [vue2使用elementui抽屉 关闭之前对比抽屉展示的组件数据是否有改动。项目实战。](https://blog.youkuaiyun.com/L_TZzzz/article/details/120202794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值