国外设计网站pinterest网址,nodejs 网站开发,钢结构招聘网,河北省建设执业资格中心网站算法(2)----STL里的排序函数。
1. sort: 对容器或普通数组中指定范围内的元素进行排序#xff0c;默认进行升序排序。 sort函数是基于快速排序实现的#xff0c;属于不稳定排序。 只支持3种容器#xff1a;array、vector、deque。 如果容器中存储的是自定义的对象#xff…算法(2)----STL里的排序函数。
1. sort: 对容器或普通数组中指定范围内的元素进行排序默认进行升序排序。 sort函数是基于快速排序实现的属于不稳定排序。 只支持3种容器array、vector、deque。 如果容器中存储的是自定义的对象则该类必须提供移动构造函数和移动赋值运算符。 代码示例
class AA
{int* m_pValue;
public:AA(int v) :m_pValue(new int(v)) {}//拷贝构造函数AA(const AA other) {if (0 ! other.m_pValue) {this-m_pValue new int(*other.m_pValue);}else {this-m_pValue 0;}}//析构函数~AA() {delete m_pValue;}//移动构造函数AA(AA other) noexcept : m_pValue(other.m_pValue) {other.m_pValue 0;}//移动赋值操作符AA operator (AA other) noexcept {if (this ! other) {delete m_pValue;m_pValue other.m_pValue;other.m_pValue 0;}return *this;}//比较操作符bool operator (AA other) {if (0 ! this-m_pValue 0 ! other.m_pValue) {return *this-m_pValue *other.m_pValue;}return false;}void Print() {if (0 ! this-m_pValue) {cout *this-m_pValue ;}else {cout null ;}}
};int main() {std::vectorAA v1{ 5,6,9,8,3,2,1,4 };for_each(v1.begin(), v1.end(), mem_fun_ref(AA::Print));cout endl;sort(v1.begin(), v1.end());for_each(v1.begin(), v1.end(), mem_fun_ref(AA::Print));return 0;
}
2. stable_sort: 排序后保证相等元素的相对位置和排序前是一样的。 stable_sort函数是基于归并排序实现的属于稳定排序。用法和sort一样。
3. partial_sort(first, middle, last) 从指定范围内选出(middle-first)个最小的元素并排序存放在 [first,middle) 区间。 代码示例
void printInt(int val)
{cout val ;
}
int main() {std::vectorint v1{ 3,2,5,4,1,6,9,8 };for_each(v1.begin(), v1.end(), printInt);cout endl;//将v1中最小的 3 个元素移动到开头位置并排好序partial_sort(v1.begin(), v1.begin() 3, v1.end());for_each(v1.begin(), v1.end(), printInt);return 0;
}
4. partial_sort_copy(first, last, result_first, result_last) 从指定范围内选出(result_last-result_first)个元素排序后拷贝到另一个容器。 代码示例
void printInt(int val)
{cout val ;
}
int main() {int target[4] { 0 };std::vectorint v1{ 3,2,5,4,1,6,9,8 };//将v1中前面5个元素排序然后拷贝3个元素到targetpartial_sort_copy(v1.begin(), v1.begin() 5, target, target 3);for_each(target, target 4, printInt);return 0;
}
5. nth_element (first, nth, last) 找到[first, last)范围内按照排序规则(默认升序)位于第nth个位置处的元素并将其放置到此位 置。同时使所有比此元素小的元素在左侧比它大的元素在右侧。
void printInt(int val)
{cout val ;
}
int main() {std::vectorint v1{ 8,1,3,4,5,6,0,2,7,9 };//默认升序排序nth_element(v1.begin(), v1.begin() 2, v1.end());cout nth_element排序 endl;for_each(v1.begin(), v1.end(), printInt);return 0;
}
6. partition (first, last, pred) 根据用户自定义的筛选规则重新排列指定区域内存储的数据使其分为 2 组第一组为符合 筛选条件的数据另一组为不符合筛选条件的数据。返回第二组的第一个元素。 代码示例
void printInt(int val)
{cout val ;
}
bool compare(int i) { return (i % 2) 0; }
int main() {std::vectorint v1{ 1,2,3,4,5,6,7,8,9 };auto bound partition(v1.begin(), v1.end(), compare);//按奇偶分组cout bound *boundendl;for_each(v1.begin(), v1.end(), printInt);return 0;
}
7. stable_partition (first, last, pred) 保证对指定区域内数据完成分组的同时不改变各组内元素的相对位置。用法和partition一样。
8. is_sorted (first, last, comp) 此函数专门用于判断某个序列是否为有序序列。 代码示例
bool compare(int i, int j) { return i j; }
int main() {std::vectorint v1{ 9, 8, 7, 6, 2 };cout v1 is sorted? is_sorted(v1.begin(), v1.end(), compare) endl;return 0;
} 文章转载自: http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.bqmsm.cn.gov.cn.bqmsm.cn http://www.morning.zrnph.cn.gov.cn.zrnph.cn http://www.morning.bksbx.cn.gov.cn.bksbx.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn http://www.morning.guofenmai.cn.gov.cn.guofenmai.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.cylbs.cn.gov.cn.cylbs.cn http://www.morning.rknsp.cn.gov.cn.rknsp.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.bqmdl.cn.gov.cn.bqmdl.cn http://www.morning.jjxxm.cn.gov.cn.jjxxm.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.znlhc.cn.gov.cn.znlhc.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.ogzjf.cn.gov.cn.ogzjf.cn http://www.morning.lbhck.cn.gov.cn.lbhck.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.tqpds.cn.gov.cn.tqpds.cn http://www.morning.cxlys.cn.gov.cn.cxlys.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.txkrc.cn.gov.cn.txkrc.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.wjdgx.cn.gov.cn.wjdgx.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.ktntj.cn.gov.cn.ktntj.cn http://www.morning.qygfb.cn.gov.cn.qygfb.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com http://www.morning.qbtkg.cn.gov.cn.qbtkg.cn http://www.morning.rythy.cn.gov.cn.rythy.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.qpqwd.cn.gov.cn.qpqwd.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.ggmls.cn.gov.cn.ggmls.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.kggxj.cn.gov.cn.kggxj.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.lcplz.cn.gov.cn.lcplz.cn http://www.morning.lwzgn.cn.gov.cn.lwzgn.cn http://www.morning.qqrlz.cn.gov.cn.qqrlz.cn http://www.morning.kqxwm.cn.gov.cn.kqxwm.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.sskns.cn.gov.cn.sskns.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.fnfhs.cn.gov.cn.fnfhs.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.mdgpp.cn.gov.cn.mdgpp.cn http://www.morning.zwdrz.cn.gov.cn.zwdrz.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.dmfdl.cn.gov.cn.dmfdl.cn http://www.morning.crxdn.cn.gov.cn.crxdn.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn