1.容器的共通能力
2.各个容器的使用时机
3.常用算法
3.1知识点
3.2源程序
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
class cStudent
{
public:
cStudent(int iID, string strName)
{
m_iID = iID;
m_strName = strName;
}
int getteriID() const { return m_iID; }
string getterstrName() const { return m_strName; }
private:
int m_iID;
string m_strName;
};
bool GreaterThree(int iNum)
{
if (iNum >= 3)
{
return true;
}
else
{
return false;
}
}
bool Compare(const cStudent& stuA, const cStudent& stuB)
{
return (stuA.getteriID() < stuB.getteriID());
}
void show(const int& iItem)
{
cout << iItem << " ";
}
int increase(int i)
{
return i + 1;
}
int main()
{
cout << "1.常用的查找算法:\n";
cout << "1.1查找算法之adjacent_find()\n";
vector<int> vecInt1;
vecInt1.push_back(1);
vecInt1.push_back(2);
vecInt1.push_back(2);
vecInt1.push_back(4);
vecInt1.push_back(5);
vector<int>::iterator it1 = adjacent_find(vecInt1.begin(), vecInt1.end());
cout << "*it1 = " << *it1 << endl << endl;
vector<int> vecInt2;
vecInt2.push_back(1);
vecInt2.push_back(2);
vecInt2.push_back(4);
vecInt2.push_back(2);
vecInt2.push_back(5);
vector<int>::iterator it2 = adjacent_find(vecInt2.begin(), vecInt2.end());
cout << "1.2查找算法之binary_search()\n";
set<int> setInt;
setInt.insert(3);
setInt.insert(1);
setInt.insert(7);
setInt.insert(5);
setInt.insert(9);
bool bFind1 = binary_search(setInt.begin(), setInt.end(), 5);
bool bFind2 = binary_search(setInt.begin(), setInt.end(), 15);
cout << "bFind1 = " << bFind1 << endl;
cout << "bFind2 = " << bFind2 << endl << endl;
cout << "1.3查找算法之count()\n";
vector<int> vecIntCount;
vecIntCount.push_back(1);
vecIntCount.push_back(2);
vecIntCount.push_back(2);
vecIntCount.push_back(4);
vecIntCount.push_back(2);
vecIntCount.push_back(5);
int iCount = count(vecIntCount.begin(), vecIntCount.end(), 2);
cout << "iCount = " << iCount << endl << endl;
cout << "1.4查找算法之count_if()\n";
vector<int> vecIntCountif;
vecIntCountif.push_back(1);
vecIntCountif.push_back(3);
vecIntCountif.push_back(5);
vecIntCountif.push_back(7);
vecIntCountif.push_back(9);
vecIntCountif.push_back(11);
int iCountif = count_if(vecIntCountif.begin(), vecIntCountif.end(), GreaterThree);
cout << "iCountif = " << iCountif << endl << endl;
cout << "1.5查找算法之equal_range()\n此处的例子见之前的STL例子的讲解\n\n";
cout << "1.6查找算法之find()\n";
vector<int> vecFind;
vecFind.push_back(1);
vecFind.push_back(3);
vecFind.push_back(5);
vecFind.push_back(7);
vecFind.push_back(9);
vector<int>::iterator itFind = find(vecFind.begin(), vecFind.end(), 5);
cout << "*itFind = " << *itFind << endl << endl;
cout << "1.7查找算法之find_if()\n";
vector<int> Find_if;
Find_if.push_back(1);
Find_if.push_back(3);
Find_if.push_back(5);
Find_if.push_back(3);
Find_if.push_back(2);
Find_if.push_back(9);
vector<int>::iterator itFind_if = find_if(Find_if.begin(), Find_if.end(), GreaterThree);
cout << "*itFind_if = " << *itFind_if << endl ;
cout << "*(itFind_if+1) = " << *(itFind_if + 1) << endl;
cout << "*(itFind_if+2) = " << *(itFind_if + 2) << endl;
cout << "*(itFind_if+3) = " << *(itFind_if + 3) << endl;
cout << "*(itFind_if+4) = " << *(itFind_if + 4) << endl << endl << endl;
cout << "2.常用的排序算法:\n";
cout << "2.1常用的排序算法之merge()\n";
vector<int> vecMergeA, vecMergeB, vecMergeC;
vecMergeA.push_back(1);
vecMergeA.push_back(3);
vecMergeA.push_back(5);
vecMergeA.push_back(7);
vecMergeA.push_back(90);
cout << "vecMergeA的元素:";
for (vector<int>::iterator itMergeA = vecMergeA.begin(); itMergeA != vecMergeA.end(); ++itMergeA)
{
cout << *itMergeA << " ";
}
cout << "\n\n";
vecMergeB.push_back(2);
vecMergeB.push_back(4);
vecMergeB.push_back(60);
vecMergeB.push_back(80);
vecMergeB.push_back(100);
cout << "vecMergeB的元素:";
for (vector<int>::iterator itMergeB = vecMergeB.begin(); itMergeB != vecMergeB.end(); ++itMergeB)
{
cout << *itMergeB << " ";
}
cout << "\n\n";
vecMergeC.resize(18);
merge(vecMergeA.begin(), vecMergeA.end(), vecMergeB.begin(), vecMergeB.end(), vecMergeC.begin());
cout << "vecMergeC的元素:";
for (vector<int>::iterator itMergeC = vecMergeC.begin(); itMergeC != vecMergeC.end(); ++itMergeC)
{
cout << *itMergeC << " ";
}
cout << "\n\n";
cout << "2.2常用的排序算法之sort()\n";
cout << "2.2.1 一般用法\n";
vector<int> vecSort;
vecSort.push_back(2);
vecSort.push_back(1);
vecSort.push_back(4);
vecSort.push_back(3);
vecSort.push_back(6);
vecSort.push_back(5);
cout << "vecSort的元素:";
for (vector<int>::iterator itSort = vecSort.begin(); itSort != vecSort.end(); ++itSort)
{
cout << *itSort << " ";
}
cout << "\n\n";
sort(vecSort.begin(), vecSort.end());
cout << "使用sort()排序后,vecSort的元素:";
for (vector<int>::iterator itSort = vecSort.begin(); itSort != vecSort.end(); ++itSort)
{
cout << *itSort << " ";
}
cout << "\n";
cout << "2.2.2 如果vector,T是自定义类型,则要提供T类型的比较函数。\n";
vector<cStudent> vecStu;
vecStu.push_back(cStudent(2, "老二"));
vecStu.push_back(cStudent(1, "老大"));
vecStu.push_back(cStudent(3, "老三"));
vecStu.push_back(cStudent(4, "老四"));
sort(vecStu.begin(), vecStu.end(), Compare);
cout << "使用sort()排序后,vecSort的元素:";
for (vector<cStudent>::iterator itSort = vecStu.begin(); itSort != vecStu.end(); ++itSort)
{
cout << itSort->getteriID() << " " << (*itSort).getterstrName() << "; ";
}
cout << "\n\n";
cout << "2.3常用的排序算法之random_shuffle()\n";
srand(time(0));
vector<int> vecrandom_shuffle;
vecrandom_shuffle.push_back(1);
vecrandom_shuffle.push_back(3);
vecrandom_shuffle.push_back(5);
vecrandom_shuffle.push_back(7);
vecrandom_shuffle.push_back(9);
string str("UIPower");
random_shuffle(vecrandom_shuffle.begin(), vecrandom_shuffle.end());
random_shuffle(str.begin(), str.end());
cout << "使用random_shuffle()排序后,vecrandom_shuffle的元素:";
for (vector<int>::iterator itRandom_shuffle = vecrandom_shuffle.begin(); itRandom_shuffle != vecrandom_shuffle.end(); ++itRandom_shuffle)
{
cout << *itRandom_shuffle << " ";
}
cout << "\n\n";
cout << "使用random_shuffle()排序后,str的元素:";
for (string::iterator itRandom_shuffle = str.begin(); itRandom_shuffle != str.end(); ++itRandom_shuffle)
{
cout << *itRandom_shuffle << " ";
}
cout << "\n\n";
cout << "2.4常用的排序算法之reverse()\n";
vector<int> vecReverse;
vecReverse.push_back(1);
vecReverse.push_back(3);
vecReverse.push_back(5);
vecReverse.push_back(7);
vecReverse.push_back(9);
cout << "使用reverse()排序后,vecrandom_shuffle的元素:";
for (vector<int>::iterator itReverse = vecReverse.begin(); itReverse != vecReverse.end(); ++itReverse)
{
cout << *itReverse << " ";
}
cout << "\n\n";
reverse(vecReverse.begin(), vecReverse.end());
cout << "使用reverse()排序后,vecrandom_shuffle的元素:";
for (vector<int>::iterator itReverse = vecReverse.begin(); itReverse != vecReverse.end(); ++itReverse)
{
cout << *itReverse << " ";
}
cout << "\n\n\n";
cout << "3.常用的拷贝和替换算法之:\n";
cout << "3.1常用的拷贝和替换算法之:\n";
vector<int> vecCopyA;
vecCopyA.push_back(1);
vecCopyA.push_back(11);
vecCopyA.push_back(5);
vecCopyA.push_back(7);
vecCopyA.push_back(9);
vector<int> vecCopyB;
vecCopyB.resize(5);
copy(vecCopyA.begin(), vecCopyA.end(), vecCopyB.begin());
cout << "使用copy()后,vecCopyB的元素:";
for (vector<int>::iterator itvecCopyB = vecCopyB.begin(); itvecCopyB != vecCopyB.end(); ++itvecCopyB)
{
cout << *itvecCopyB << " ";
}
cout << "\n\n";
cout << "3.2常用的拷贝和替换算法之replace():\n";
vector<int> vecReplace;
vecReplace.push_back(1);
vecReplace.push_back(3);
vecReplace.push_back(5);
vecReplace.push_back(3);
vecReplace.push_back(9);
replace(vecReplace.begin(), vecReplace.end(), 3, 8);
cout << "使用replace()后,vecReplace的元素:";
for (vector<int>::iterator itvecReplace = vecReplace.begin(); itvecReplace != vecReplace.end(); ++itvecReplace)
{
cout << *itvecReplace << " ";
}
cout << "\n\n";
cout << "3.3常用的拷贝和替换算法之replace_if():\n";
vector<int> vecReplace_if;
vecReplace_if.push_back(1);
vecReplace_if.push_back(3);
vecReplace_if.push_back(5);
vecReplace_if.push_back(3);
vecReplace_if.push_back(9);
replace_if(vecReplace_if.begin(), vecReplace_if.end(), GreaterThree, 8);
cout << "使用replace_if()后,vecReplace_if的元素:";
for (vector<int>::iterator itvecReplace_if = vecReplace_if.begin(); itvecReplace_if != vecReplace_if.end(); ++itvecReplace_if)
{
cout << *itvecReplace_if << " ";
}
cout << "\n\n";
cout << "3.4常用的拷贝和替换算法之swap():\n";
vector<int> vecSwapA;
vecSwapA.push_back(1);
vecSwapA.push_back(3);
vecSwapA.push_back(5);
vector<int> vecSwapB;
vecSwapB.push_back(2);
vecSwapB.push_back(4);
cout << "使用swap()前,vecSwapA的元素:";
for (vector<int>::iterator itvecSwapA = vecSwapA.begin(); itvecSwapA != vecSwapA.end(); ++itvecSwapA)
{
cout << *itvecSwapA << " ";
}
cout << "\n\n";
cout << "使用swap()前,vecSwapB的元素:";
for (vector<int>::iterator itvecSwapB = vecSwapB.begin(); itvecSwapB != vecSwapB.end(); ++itvecSwapB)
{
cout << *itvecSwapB << " ";
}
cout << "\n\n";
swap(vecSwapA, vecSwapB);
cout << "使用swap()后,vecSwapA的元素:";
for (vector<int>::iterator itvecSwapA = vecSwapA.begin(); itvecSwapA != vecSwapA.end(); ++itvecSwapA)
{
cout << *itvecSwapA << " ";
}
cout << "\n\n";
cout << "使用swap()后,vecSwapB的元素:";
for (vector<int>::iterator itvecSwapB = vecSwapB.begin(); itvecSwapB != vecSwapB.end(); ++itvecSwapB)
{
cout << *itvecSwapB << " ";
}
cout << "\n\n";
cout << "4.常用的算术和生成算法:\n";
cout << "4.1常用的算术和生成算法之accumulate():\n";
vector<int> vecAccumulate;
vecAccumulate.push_back(1);
vecAccumulate.push_back(3);
vecAccumulate.push_back(5);
vecAccumulate.push_back(7);
vecAccumulate.push_back(9);
int iSum = accumulate(vecAccumulate.begin(), vecAccumulate.end(), 100);
cout << "iSum = " << iSum << endl << endl;
cout << "4.2常用的算术和生成算法之fill():\n";
vector<int> vecFill;
vecFill.push_back(1);
vecFill.push_back(3);
vecFill.push_back(5);
vecFill.push_back(7);
vecFill.push_back(9);
fill(vecFill.begin(), vecFill.end(), 8);
cout << "使用fill()后,vecFill的元素:";
for (vector<int>::iterator itvecFill = vecFill.begin(); itvecFill != vecFill.end(); ++itvecFill)
{
cout << *itvecFill << " ";
}
cout << "\n\n";
cout << "5.常用的集合算法:\n";
vector<int> vecSetA;
vecSetA.push_back(1);
vecSetA.push_back(3);
vecSetA.push_back(5);
vecSetA.push_back(7);
vecSetA.push_back(9);
vector<int> vecSetB;
vecSetB.push_back(1);
vecSetB.push_back(3);
vecSetB.push_back(5);
vecSetB.push_back(6);
vecSetB.push_back(8);
vector<int> vecSetC;
vecSetC.resize(10);
cout << "5.1常用的集合算法之set_union()并集:\n";
set_union(vecSetA.begin(), vecSetA.end(), vecSetB.begin(), vecSetB.end(), vecSetC.begin());
cout << "使用set_union()后,vecSetC的元素:";
for (vector<int>::iterator itvecSetC = vecSetC.begin(); itvecSetC != vecSetC.end(); ++itvecSetC)
{
cout << *itvecSetC << " ";
}
cout << "\n\n";
cout << "5.2常用的集合算法之set_intersection()交集:\n";
fill(vecSetC.begin(), vecSetC.end(), 0);
set_intersection(vecSetA.begin(), vecSetA.end(), vecSetB.begin(), vecSetB.end(), vecSetC.begin());
cout << "使用set_intersection()后,vecSetC的元素:";
for (vector<int>::iterator itvecSetC = vecSetC.begin(); itvecSetC != vecSetC.end(); ++itvecSetC)
{
cout << *itvecSetC << " ";
}
cout << "\n\n";
cout << "5.3常用的集合算法之set_difference()差集:\n";
fill(vecSetC.begin(), vecSetC.end(), 0);
set_difference(vecSetA.begin(), vecSetA.end(), vecSetB.begin(), vecSetB.end(), vecSetC.begin());
cout << "使用set_difference()后,vecSetC的元素:";
for (vector<int>::iterator itvecSetC = vecSetC.begin(); itvecSetC != vecSetC.end(); ++itvecSetC)
{
cout << *itvecSetC << " ";
}
cout << "\n\n";
cout << "6.常用的遍历算法:\n";
cout << "6.1常用的遍历算法之for_each():\n";
int iArray[] = { 0,1,2,3,4 };
vector<int> vecFor_each(iArray, iArray + sizeof(iArray) / sizeof(iArray[0]));
for_each(vecFor_each.begin(), vecFor_each.end(), show);
cout << endl << endl;
cout << "6.2常用的遍历算法之transform():\n";
vector<int> vecTransform;
vecTransform.push_back(1);
vecTransform.push_back(3);
vecTransform.push_back(5);
vecTransform.push_back(7);
vecTransform.push_back(9);
transform(vecTransform.begin(), vecTransform.end(), vecTransform.begin(), increase);
for_each(vecTransform.begin(), vecTransform.end(), show);
return 0;
}
3.3运行结果