STL算法
程序员文章站
2022-03-06 12:09:33
...
遍历算法
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <functional>
#include <string>
#include <iterator>
using namespace std;
void printV(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
}
void printList(list<int> &l)
{
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
}
// 函数对象
void showElem(int &n)
{
cout << n << " ";
}
class CMyShow
{
public:
CMyShow()
{
num = 0;
}
void operator()(int &n)
{
num++;
cout << n << " ";
}
void printNum()
{
cout << "num : " << num << endl;
}
private:
int num;
};
void main1_foreach_transform()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
// 函数对象 -- 回调函数入口地址
for_each(v1.begin(), v1.end(), showElem);
cout << endl;
//
for_each(v1.begin(), v1.end(), CMyShow());
cout << endl;
CMyShow mya;
CMyShow my1 = for_each(v1.begin(), v1.end(), mya); // 这是给my1初始化
mya.printNum(); // mya 和 my1 是两个不同的对象
my1.printNum();
my1 = for_each(v1.begin(), v1.end(), mya); // 这是个my1赋值
my1.printNum();
cout << endl;
}
int increase(int i)
{
return i + 100;
}
void main1_transform()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
printV(v1);
cout << endl;
// 使用回调函数
transform(v1.begin(), v1.end(), v1.begin(), increase);
printV(v1);
cout << endl;
// 使用预定义的函数对象 -- 变成负数
transform(v1.begin(), v1.end(), v1.begin(), negate<int>());
printV(v1);
cout << endl;
// transform 使用函数适配器
// 把运算结果放在list里面去
list<int> mylist;
mylist.resize(v1.size());
transform(v1.begin(), v1.end(), mylist.begin(), bind2nd(multiplies<int>(), 10));
printList(mylist);
cout << endl;
// transform 也可以把运算结果直接输出到屏幕
// transform(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "), negate<int>() );
transform(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "), negate<int>());
}
int main()
{
// main1_foreach_transform();
main1_transform();
system("pause");
return 0;
}
查找算法
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <algorithm>
#include <functional>
#include <string>
#include <iterator>
using namespace std;
int main_adjacent_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(2);
v1.push_back(3);
v1.push_back(5);
vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
if (it == v1.end())
{
cout << "没有找到重复的元素" << endl;
}
else
{
cout << *it << endl;
}
int index = distance(v1.begin(), it);
cout << index << endl;
return 0;
}
// 用二分法进行查找
void main_binary_search()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(9);
bool isFind = binary_search(v1.begin(), v1.end(), 7);
if (isFind == true)
{
cout << "找到了" << endl;
}
else
{
cout << "没有找到" << endl;
}
}
// 查找出现的个数
void main_count()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
int num = count(v1.begin(), v1.end(), 7);
cout << num << endl;
}
bool GreatThree(int iNum)
{
if (iNum > 3)
{
return true;
}
return false;
}
// 查找元素大于 3 的个数
void main_count_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
int num = count_if(v1.begin(), v1.end(), GreatThree);
cout << num << endl;
}
void main_find()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
v1.push_back(7);
v1.push_back(7);
v1.push_back(9);
v1.push_back(7);
vector<int>::iterator it = find(v1.begin(), v1.end(), 5);
cout << "*it : " << *it << endl;
}
void main_find_if()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(9);
v1.push_back(7);
v1.push_back(7);
v1.push_back(2);
v1.push_back(7);
vector<int>::iterator it = find_if(v1.begin(), v1.end(), GreatThree);
cout << "*it " << *it << endl;
}
int main()
{
// main_adjacent_find();
// main_binary_search();
// main_count();
// main_count_if();
// main_find();
main_find_if();
system("pause");
return 0;
}