1.sort排序算法
sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred) --默認為升序排序
形參:_First、_Last --容器的起始和結束迭代器
_Pred --排序規則,默認為從小到大
示例:
#include
#include
#include
using namespace std;
class Print
{
public:
void operator()(int val)
{
cout < val < " ";
}
};
void test()
{
vectorvtr;
vtr.push_back(rand()%50);
vtr.push_back(rand() % 50);
vtr.push_back(rand() % 50);
vtr.push_back(rand() % 50);
vtr.push_back(rand() % 50);
vtr.push_back(rand() % 50);
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
cout < "升序排序:" < endl;
sort(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
cout < "降序排序:" < endl;
sort(vtr.begin(), vtr.end(), greater());
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
}
int main()
{
test();
system("pause");
}
2.random_shuffle打亂順序(洗牌)
打亂有序數列,重新洗牌:
void random_shuffle(_RanIt _First, _RanIt _Last);
形參:_First、_Last --起始和結束迭代器
#include
using namespace std;
#include
#include
#include
#include
class Person
{
friend class Print;
public:
Person() {}
Person(string name, int age) :name(name), age(age) {
}
bool operator(const Person p)const
{
if (age == p.age)
{
return name < p.name;
}
return age < p.age;
}
string name;
int age;
};
class Print
{
public:
void operator()(int val)
{
cout < val < " ";
}
void operator()(Person& p)
{
cout < "姓名:" < p.name < "t年齡:" < p.age < endl;
}
};
void test()
{
vectorvtr;
vtr.resize(10);
for (int i = 0; i < 10; i++)
{
vtr[i] = i;
}
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
cout < "洗牌后:" < endl;
random_shuffle(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
}
void test02()
{
cout < "t自定義數據:" < endl;
vectorvtr;
vtr.push_back(Person("小王", 1));
vtr.push_back(Person("小王", 2));
vtr.push_back(Person("小王", 3));
vtr.push_back(Person("小李", 1));
vtr.push_back(Person("小李", 2));
for_each(vtr.begin(), vtr.end(), Print());
cout < "洗牌后:" < endl;
random_shuffle(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), Print());
}
class mapPrint
{
public:
void operator()(pairp)
{
cout < "姓名:" < (p.first).name < "t年齡:" < (p.first).age < "t得分:" < p.second < endl;
}
};
int main()
{
srand(time(NULL));//random_shuffle底層需要隨機數種子,否則每次生成結果一樣
test();
test02();
system("pause");
}
3.merge合并容器
容器合并:
merge()實現吧兩個容器合并在一起,存放到第三個容器中。
注意:merge()合并一定要保證容器元素有序,默認是從小到大的順序。
merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) -->默認從小到大
merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred) -->重載版本,支持自定義排序規則
_First1、_Last1 --第一個容器的起始和結束迭代器
_Last2、_Dest --第二個元素的起始和結束迭代器
_Dest --要存儲的新容器起始迭代器
_Pred --謂詞,設定排序規則
謂詞:
函數對象返回中為bool類;
函數對象形參只有一個 --> 一元謂詞
函數對象形參有兩個 --> 二元謂詞
示例:
#include
#include
#include
#include
using namespace std;
class Print
{
public:
void operator()(int val)
{
cout < val < " ";
}
};
void test01()
{
vectorv1;
vectorv2;
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
}
v2.push_back(2);
v2.push_back(16);
v2.push_back(12);
v2.push_back(14);
sort(v2.begin(), v2.end(), less());
vectorv3;
v3.resize(v1.size() + v2.size());
cout < "從小到大于有序合并:" < endl;
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), Print());
cout());
sort(v2.begin(), v2.end(), greater());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),greater());
for_each(v3.begin(), v3.end(), Print());
cout < endl;
}
int main()
{
test01();
system("pause");
}
;>
4.reverse元素反轉
函數功能: 元素反轉,將容器中的元素前后顛倒
reverse(const _BidIt _First, const _BidIt _Last)
形參:_First、_Last --起始和結束迭代器
#include
#include
#include
using namespace std;
class Person
{
public:
Person() {}
Person(string name, int age) :name(name), age(age) {
}
Person(const Person& p)
{
this->age = p.age;
name = p.name;
}
bool operator( Person p)const
{
if (age == p.age)return name < p.name;
return age < p.age;
}
string name;
int age;
};
class Print
{
public:
void operator()(Person p)
{
cout < "姓名:" < p.name < "t年齡:" < p.age < endl;
}
};
void test()
{
vectorvtr;
vtr.push_back(Person("小王",18));
vtr.push_back(Person("小劉", 15));
vtr.push_back(Person("小林", 20));
for_each(vtr.begin(), vtr.end(), Print());
cout < "反轉:" < endl;
reverse(vtr.begin(), vtr.end());
for_each(vtr.begin(), vtr.end(), Print());
}
int main()
{
test();
system("pause");
}
5.copy元素拷貝
_OutIt copy(_InIt _First, _InIt _Last, _OutIt _Dest)
形參:_First、_Last --原容器的起始和結束位置
_Dest --目標容器的起始位置
該函數功能類似于重載運算符=功能
#include
#include
#include
void test()
{
std::vectorvtr, vtr2;
vtr.push_back("C++");
vtr.push_back("copy算法");
vtr.push_back("學習");
vtr.push_back("案例");
std::cout < "copy算法使用示例:" < std::endl;
vtr2.resize(vtr.size());
copy(vtr.begin(), vtr.end(), vtr2.begin());//copy函數類似于賦值操作,vtr2=vtr1
for (std::vector::iterator p = vtr2.begin(); p != vtr2.end(); p++)
{
std::cout < *p < " ";
}
std::cout < std::endl;
}
int main()
{
test();
system("pause");
}
6.replace元素替換
元素替換
void replace(const _FwdIt _First, const _FwdIt _Last, const _Ty& _Oldval, const _Ty& _Newval)
形參:_First、_Last --要替換的數據區間
_Oldval --要替換的內容
_Newval --替換后的內容
#include
#include
using namespace std;
#include
using namespace std;
class Print
{
public:
void operator()(int val)
{
cout < val < " ";
}
};
void test()
{
vectorvtr;
vtr.push_back(10);
vtr.push_back(10);
vtr.push_back(30);
vtr.push_back(10);
vtr.push_back(35);
vtr.push_back(10);
cout < "替換前:" < endl;
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
cout < "將10替換成666" < endl;
replace(vtr.begin(), vtr.end(), 10, 666);
for_each(vtr.begin(), vtr.end(), Print());
cout < endl;
}
int main()
{
test();
system("pause");
}
7.條件替換replace_if
條件替換
replace_if(const _FwdIt _First, const _FwdIt _Last, _Pr _Pred, const _Ty& _Val)
形參:_First、_Last --要替換的區間
_Pred --謂詞,替換條件
_Val --替換后的值
示例:
#include
#include
#include
#include
using namespace std;
class Myreplace
{
public:
bool operator()(int val)
{
return val >= 30;
}
};
void test()
{
vectorvtr;
vtr.push_back(10);
vtr.push_back(10);
vtr.push_back(30);
vtr.push_back(10);
vtr.push_back(50);
vtr.push_back(10);
cout < "將所有的?>=30的值替換為666" < endl;
replace_if(vtr.begin(), vtr.end(), Myreplace(), 666);
for (vector::iterator ptr = vtr.begin();ptr != vtr.end();ptr++)
{
cout < *ptr < " ";
}
cout < endl;
}
int main()
{
test();
system("pause");
}
8.容器互換swap
容器元素互換:
swap(container v1,container v2);
將v1和v2的容器元素進行互換,類似于成員函數swap();
#include
using namespace std;
#include
#include
class Print
{
public:
void operator()(int val)
{
cout < val < " ";
}
};
void test()
{
vectorvtr1 = { 10,20,30,50 };
vectorvtr2 = { 10,29,88 };
cout < "替換前:" < endl;
for_each(vtr1.begin(), vtr1.end(), Print());
cout < endl;
for_each(vtr2.begin(), vtr2.end(), Print());
cout < endl;
swap(vtr1, vtr2);
cout < "替換結果:" < endl;
for_each(vtr1.begin(), vtr1.end(), Print());
cout < endl;
for_each(vtr2.begin(), vtr2.end(), Print());
cout < endl;
}
int main()
{
test();
system("pause");
}
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
算法
+關注
關注
23文章
4455瀏覽量
90753 -
C++
+關注
關注
21文章
2066瀏覽量
72900 -
STL
+關注
關注
0文章
85瀏覽量
18212
發布評論請先 登錄
相關推薦
c++算法庫的移植問題
目前有一個算法庫 用的c++ ?用ccs在dsp上可以調試
現在希望把庫放在8168的dsp上 ?需要將c++轉換成c 有個工具
mmCC C++-
發表于 06-21 13:14
C++ STL的概念及舉例
本篇文章是作者本人使用STL 后的一些看法, 對於想要靠此文章學習STL, 是不可能的. 建議叁后面介紹的一些書入門.
STL的概念
在STL 中, 大至上分三個主要的
發表于 08-30 11:39
?1303次閱讀
STL算法在GIS中的應用
使用STL 算法實現GIS 算法可以保證它的簡潔和高效該文結合C++代碼實例抽象出了地理算子的概念應用在GIS 算法當中通過定制適配器來消除
發表于 06-28 16:55
?33次下載
C++ STL基本概念是什么
STL,英文全稱 standard template library,中文可譯為標準模板庫或者泛型庫,其包含有大量的模板類和模板函數,是 C++ 提供的一個基礎模板的集合,用于完成諸如輸入/輸出、數學計算等功能。
評論