STL常用算法
- 算法主要是由
<algorithm><functional><numeric>组成
<algorithm>
是STL头文件中体积最大的一个,涉及比较、交换、查找、遍历、复制、修改等<numeric>
体积很小,只包括几个序列上进行简单数学运算的模板函数<functional>
定义了一些模板类,用以声明模板函数
一、常用遍历算法
一、for_each
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
void print1(int val)
{
cout << val << " ";
}
class print2
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
for_each(v.begin(), v.end(), print1);
cout << endl;
for_each(v.begin(), v.end(), print2());
cout << endl;
}
使用普通函数和仿函数作为操作函数,for_each遍历容器
二、transform
class Transform
{
public:
int operator()(int n)
{
return n+100;
}
};
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
vector<int>vTarget;
vTarget.resize(v.size());
transform(v.begin(), v.end(), vTarget.begin(), Transform());
for_each(vTarget.begin(), vTarget.end(), print2());
cout << endl;
}
定义仿函数提供transform的操作函数,将v的值+100转移到vTarget上,一定要事先开辟空间
二、常用查找算法
find:查找元素
find_if:按条件查找元素
adjacent_find:查找相邻重复元素
binary_search二分查找法
count:统计元素个数
count_if:按条件统计元素个数
一、find
- 查找指定元素,找到返回元素的迭代器,找不到返回结束迭代器位置
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
auto pos=find(v.begin(), v.end(),5);
if (pos != v.end())
cout << "找到了:" << *pos << endl;
else
cout << "没找到" << endl;
}
class Person
{
public:
string m_name;
int m_age;
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
bool operator==(const Person&p)
{
if (this->m_age == p.m_age&&this->m_name == p.m_name)
return true;
else
return false;
}
};
void test02()
{
vector<Person>v;
Person p[4] = { {"a",11},{"b",22},{"c",33},{"d",44} };
for (int i = 0; i < 4; i++)
v.push_back(p[i]);
auto pos = find(v.begin(), v.end(), p[1]);
if (pos != v.end())
cout << "找到了,姓名:" << pos->m_name << " 年龄:"<<pos->m_age<<endl;
else
cout << "没找到" << endl;
}
int main()
{
test01();
test02();
}
通过源码可以看到查找时会有值的比较,对于自定义数据类型需要重载==,否则无法比较
二、find_if
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
class Five
{
public:
bool operator()(int n)
{
return n > 5;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
auto pos = find_if(v.begin(), v.end(), Five());
if (pos != v.end())
cout << "找到了:" << *pos << endl;
else
cout << "没找到" << endl;
}
class Person
{
public:
string m_name;
int m_age;
Person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
};
class Twenty
{
public:
bool operator()(const Person&p)
{
return p.m_age > 20;
}
};
void test02()
{
vector<Person>v;
Person p[4] = { {"aaa",10},{"bbb",20},{"ccc",30},{"ddd",40} };
for (int i = 0; i < 4; i++)
v.push_back(p[i]);
auto pos=find_if(v.begin(), v.end(), Twenty());
if(pos != v.end())
cout << "找到了,姓名:" << pos->m_name << " 年龄:"<<pos->m_age<<endl;
else
cout << "没找到" << endl;
}
int main()
{
test01();
test02();
return 0;
}
使用与find一样,只不过是查找方法变成了仿函数设置条件,或者函数。
三、adjacent_find
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void test01()
{
vector<int>v;
v.push_back(0);
v.push_back(2);
v.push_back(0);
v.push_back(3);
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(3);
auto pos = adjacent_find(v.begin(), v.end());
if (pos == v.end())
cout << "为找到相邻元素" << endl;
else
cout << "找到相邻重复元素:" << *pos << endl;
}
返回值是第一个找到的有相邻重复元素的迭代器
四、binary_search
二分查找,效率很高,但要注意的的是,查找的数据必须是有序存储的
其返回值是一个bool类型的值,找到返回true,反之false
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
bool ret=binary_search(v.begin(), v.end(), 7);
if (ret)
cout << "找到了" << endl;
else
cout << "没找到" << endl;
}
五、count
统计元素个数
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
void test01()
{
vector<int>v;
for (int i = 0; i <= 573; i++)
v.push_back(i);
v.push_back(33);
v.push_back(33);
v.push_back(7);
int n1 = count(v.begin(), v.end(), 1),
n2 = count(v.begin(), v.end(), 7),
n3 = count(v.begin(), v.end(), 33);
cout << "1的个数:" << n1 << endl
<< "7的个数:" << n2 << endl
<< "33的个数:" << n3 << endl;
}
class Person
{
public:
int m_age;
string m_name;
Person(int age, string name)
{
this->m_age = age;
this->m_name = name;
}
bool operator==(const Person&p)
{
return this->m_age == p.m_age;
}
};
void test02()
{
vector<Person>v;
Person p[5]=
{
{10,"猪猪侠"},
{10,"Tom and Jerry"},
{10,"光头强"},
{20,"星游记"},
{30,"快乐星猫"}
};
for (int i = 0; i < 5; i++)
v.push_back(p[i]);
Person s(10, "喜羊羊");
int num=count(v.begin(), v.end(), s);
cout << "和喜羊羊同岁数的人员个数为:" << num << endl;
}
int main()
{
test01();
test02();
return 0;
}
同样对于自定义数据类型的计数需要重载==设置判断条件
六、count_if
就是把查找的标准从相等匹配换为一个条件判断
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
class Person
{
public:
int m_age;
string m_name;
Person(int age, string name)
{
this->m_age = age;
this->m_name = name;
}
};
class cmp1
{
public:
bool operator()(int val)
{
return val > 5;
}
};
class cmp2
{
public:
bool operator()(const Person&p)
{
return p.m_age > 10;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
int n1 = count_if(v.begin(), v.end(), cmp1());
cout << "大于5的值的个数:" << n1;
cout << endl;
vector<Person>vp;
Person p[5] =
{
{10,"猪猪侠"},
{10,"Tom and Jerry"},
{10,"光头强"},
{20,"星游记"},
{30,"快乐星猫"}
};
for (int i = 0; i < 5; i++)
vp.push_back(p[i]);
int n2 = count_if(vp.begin(), vp.end(), cmp2());
cout << "年龄大于10的人员个数为:" << n2 << endl;
}
int main()
{
test01();
return 0;
}
三、常用排序算法
sort:对容器内元素排序
random_shuffle:洗牌,指定范围内的元素随机调整次序
merge:容器元素合并,并存储到另一容器中
reverse:反转指定范围内的元素
一、sort
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
void Print(int val)
{
cout << val << " ";
}
class cmp
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test01()
{
vector<int>v;
for (int i = 0; i < 5; i++)
v.push_back(i);
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), Print);
cout << endl;
sort(v.begin(), v.end(), cmp());
for_each(v.begin(), v.end(), Print);
cout << endl;
}
可以使用仿函数指定排序方式
二、random_shuffle
随机调整设定范围内数据的顺序
需要设置时间戳srand((unsigned int)time(NULL))
保证随机数的有效性
#include<time.h>
void test02()
{
vector<int>v;
for (int i = 0; i < 10; i++)
v.push_back(i);
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(),Print);
cout << endl;
}
int main()
{
srand((unsigned int)time(NULL));
//test01();
test02();
}
三、merge
合并的两个容器必须有序
同时注意,将存储合并值的容器要有足够的空间去容纳。
void test03()
{
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i*i);
}
vector<int>vTarget;
vTarget.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
for_each(vTarget.begin(), vTarget.end(), Print);
}
这里的合并不是相加合并而是将两块数据有序合并到一块空间上
四、reverse
void test04()
{
vector<int>v;
for (int i = 10; i < 100; i += 10)
v.push_back(i);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), Print);
cout << endl;
cout << "反转后:" << endl;
reverse(v.begin(),v.end());
for_each(v.begin(), v.end(), Print);
cout << endl;
}
对指定区间内的数据进行逆序