STL容器算法主要有:、、组成
algorithm主要有遍历、比较、交换、查找、拷贝、修改等;
numeric体积很小,主要包括几个序列上的进行数学运算的函数模板;
functional定义了一些模板类,用于声明仿函数;
for_each()函数用于完成容器遍历,函数参数如下:
for_each(_InIt _First, _InIt _Last, _Fn _Func)
形参:_First、_Last --容器的起始和结束迭代器
_Func -- >仿函数(回调函数),处理要输出的信息
自定义数据格式:
class Person
{
friend class mycompare;
friend class Myprint;//友元类
friend void Print(const Person& p);//友元函数
public:
Person() {}
Person(string name, int age) :name(name), age(age) {
}
Person(const Person& p)
{
name = p.name;
age = p.age;
}
bool operator< (const Person& p)const
{
if (this- >age == p.age)
{
return p.name < p.name;
}
return this- >age > p.age;
}
private:
string name;
int age;
};
//创建函数对象
class Myprint
{
public:
void operator()(const Person& p)
{
cout < < "姓名:" < < p.name < < "t年龄:" < < p.age < < endl;
}
void operator()(const pair< Person, int >& p)
{
cout < < "姓名:" < < (p.first).name < < "t年龄:" < < (p.first).age < < "t成绩:" < < p.second < < endl;
}
};
void vector_test()
{
cout < < "tvector容器示例:" < < endl;
//创建vector容器--- >单端数组
vector< Person >vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小林", 18));
vtr.push_back(Person("小李", 18));
vtr.push_back(Person("小刘", 18));
cout < < "提供一个函数示例:" < < endl;
for_each(vtr.begin(), vtr.end(), Print);
cout < < "提供一个仿函数示例:" < < endl;
for_each(vtr.begin(), vtr.end(), Myprint());
}
void list_test()
{
//创建一个list容器-- >双向链表
cout < < "tlist容器示例:" < < endl;
list< Person > t;
t.push_back(Person("小王", 18));//尾插
t.push_front(Person("小林", 18));//头插
t.push_back(Person("小李", 18));
t.push_back(Person("小刘", 18));
cout < < "提供一个仿函数示例:" < < endl;
for_each(t.begin(), t.end(), Myprint());
}
void set_test()
{
cout < < "tset/multiset关联式容器:" < < endl;
multiset< Person >mt;//该容器会自动排序
mt.insert(Person("小王", 18));
mt.insert(Person("小林", 18));
mt.insert(Person("小李", 17));
mt.insert(Person("小刘", 18));
mt.insert(Person("小李", 22));
for_each(mt.begin(), mt.end(), Myprint());
cout < < "tset/multiset关联式容器(仿函数示例):" < < endl;
multiset< Person, mycompare >mt2;
mt2.insert(Person("小王", 18));
mt2.insert(Person("小林", 18));
mt2.insert(Person("小李", 17));
mt2.insert(Person("小刘", 18));
mt2.insert(Person("小李", 22));
for_each(mt.begin(), mt.end(), Myprint());
}
void map_test()
{
cout < < "tmap关联式容器示例:" < < endl;
multimap< Person, int >mp;
mp.insert(make_pair(Person("小王", 18), 88));
mp.insert(pair< Person,int >(Person("小林", 18),99));
mp.insert(pair< Person, int >(Person("小李", 17), 95));
mp.insert(pair< Person, int >(Person("小刘", 18), 95));
mp.insert(pair< Person, int >(Person("小李", 22), 90));
for_each(mp.begin(), mp.end(), Myprint());
}
int main()
{
vector_test();//vector容器
list_test();//list容器
set_test();//set容器
map_test();//map容器
system("pause");
}
transform函数:
transform(const _InIt _First, const _InIt _Last, _OutIt _Dest, _Fn _Func)
将一个函数中的元素拷贝到另一个容器中,
_First、_Last --要转移的容器的起始迭代器和结束迭代器
_Dest --目标容器的起始迭代器
_Func --仿函数,支持一元运算,可以对当前值进行+ - * 等各种运算
使用示例:
#include < iostream >
using namespace std;
#include < algorithm >
#include < vector >
class myprint
{
public:
void operator()(int val)
{
cout < < val < < " ";
}
};
class myfunc
{
public:
int operator()(int val1)
{
return val1;
}
};
void test()
{
vector< int >t1;
for (int i = 0; i < 10; i++)
{
int temp = rand() % 101;
t1.push_back(temp);
}
cout < < "原容器数据:" < < endl;
for_each(t1.begin(), t1.end(), myprint());
cout < < endl;
vector< int >target;
target.resize(t1.size());//设置target容器大小
transform(t1.begin(), t1.end(), target.begin(), myfunc());
cout < < "转移后的容器内容:" < < endl;
for_each(target.begin(), target.end(), myprint());
cout < < endl;
}
int main()
{
test();
system("pause");
}
STL中支持多种查找方式。
find函数:
find(_InIt _First, const _InIt _Last, const _Ty& _Val);
_First、_Last --查找的起始和结束位置
_Val --要查找的内容
返回值:成功返回查找到位置的迭代器
失败返回end();
#include < iostream >
using namespace std;
#include < algorithm >
#include < vector >
#include < list >
#include < stack >
#include < map >
class Person
{
friend class map_compare;
friend class print;
friend ostream& operator< <(ostream& cout, const Person& p);
public:
Person(){}
Person(string name, int age):name(name),age(age) {
}
bool operator==(const Person& p)const
{
if (name == p.name && age == p.age)return true;
return false;
}
bool operator< (const Person& p)const
{
if (p.age == age)
{
return name < p.name;
}
return age > p.age;
}
private:
string name;
int age;
};
ostream& operator< <(ostream& cout, const Person& p)
{
cout < < "姓名:" < < p.name < < "t年龄:" < < p.age ;
return cout;
}
class print
{
public:
void operator()(const Person& p) {
cout < < "姓名:" < < p.name < < "t年龄:" < < p.age < < endl;
}
};
void vec_test()
{
cout < < "tvector容器查找示例:" < < endl;
vector< Person >vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小刘", 17));
vtr.push_back(Person("小李", 22));
vtr.push_back(Person("小林", 19));
for_each(vtr.begin(),vtr.end(),print());
vector< Person >::iterator ret=find(vtr.begin(), vtr.end(), Person("小刘", 17));
if (ret == vtr.end())
{
cout < < "未找到该成员" < < endl;
}
else
{
cout < < "查找成功: " < < endl;
cout < < *ret < < endl;;
}
}
void list_test()
{
cout < < "nlist容器使用示例:" < < endl;
list< Person >t;
t.push_back(Person("小王", 18));
t.push_front(Person("小刘", 17));
t.insert(t.end(),Person("小王", 18));
t.insert(t.begin(), Person("小王", 18));
t.push_back(Person("小李", 22));
t.push_back(Person("小林", 19));
for_each(t.begin(), t.end(), print());
cout < < "查找成员Person("小王", 18)" < < endl;
int count = 0;
for (list< Person >::iterator ptr = t.begin();ptr != t.end(); )
{
ptr =find(ptr, t.end(), Person("小王", 18));
if (ptr != t.end())
{
cout < < "查找到第 " < < ++count < < "成员:";;
cout < < *ptr < < endl;
ptr++;
}
}
}
//栈容器
void stack_test()
{
cout < < "stack容器查找示例:" < < endl;
//stack栈:先进后出
stack< Person >s;
s.push(Person("小王", 18));
s.push(Person("小刘", 17));
s.push(Person("小李", 22));
s.push(Person("小林", 19));
s.push(Person("小王", 18));
cout < < "元素个数:" < < s.size() < < endl;
while (!s.empty())//判断容器是否为空
{
if (s.top() == Person("小王", 18))
{
cout < < "查找成功" < < endl;
}
s.pop();//出栈
}
}
class map_print
{
public:
void operator()(const pair< Person, int >& p)
{
cout < < p.first < < "t成绩:" < < p.second < < endl;
}
};
class map_compare
{
public:
bool operator()(const pair< Person, int >& p1, const pair< Person, int >& p2)
{
if (p1.first == p2.first && p1.second == p2.second)return true;
return false;
}
};
//map容器
void multimap_test()
{
cout < < "nmap容器查找示例:" < < endl;
multimap< Person, int >mp;
//插入数据
mp.insert(pair< Person, int >(Person("小王", 18), 88));
mp.insert(make_pair(Person("小刘", 17), 99));
mp.insert(multimap< Person, int >::value_type(Person("小王", 18), 100));
mp.insert(make_pair(Person("小李", 19), 95));
for_each(mp.begin(), mp.end(), map_print());
cout< < "查找成员Person("小王", 18)" < < endl;
multimap< Person, int >::iterator ret = mp.find(Person("小王", 18));//查找数据
map_print p;
//multimap容器由于数据都是有序的,所以相同的数据都是连在一起的
for (int i = 0; i < mp.count(Person("小王", 18)); i++, ret++)
{
if (ret != mp.end())
{
p(*ret);
}
}
}
class stu
{
public:
stu() {}
stu(string name, int age) :name(name), age(age) {
}
bool operator==(const stu& p)const
{
if (name == p.name && age == p.age)return true;
return false;
}
bool operator< (const stu& p)const
{
if (p.age == age)
{
return name < p.name;
}
return age > p.age;
}
string name;
int age;
};
int main()
{
vec_test();
list_test();
stack_test();
multimap_test();
system("pause");
}
#endif
find_if(_InIt _First, const _InIt _Last, _Pr _Pred)
形参:_First、_Last --查找的起始和结束迭代器
_Pred --仿函数,查找条件
返回值:成功返回查找到的成员的位置迭代器
失败返回endl
#include < iostream >
#include < vector >
#include < map >
#include < algorithm >
#include < functional >
using namespace std;
class Person
{
friend std::ostream& operator< <(std::ostream& cout, Person& p);
friend class compare;
friend class print;
public:
Person() {}
Person(std::string name, int age) :name(name), age(age) {
}
bool operator >(const Person& p)const
{
return age > p.age;
}
private:
std::string name;
int age;
};
std::ostream& operator< <(std::ostream& cout, Person& p)
{
cout < < "姓名:" < < p.name < < "t年龄" < < p.age;
return cout;
}
class compare
{
public:
bool operator()(const Person& p)
{
return p.age > 18;
}
bool operator()(const Person& p1, const Person& p2)const
{
if (p1.age == p2.age)return p1.name < p2.name;
return p1.age < p2.age;
}
};
class print
{
public:
void operator()(pair< Person, int >p)
{
cout < < p.first < < "t成绩" < < p.second < < endl;
}
};
void vector_test()
{
cout < < "vector容器条件查找示例:" < < endl;
std::vector< Person > vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小刘", 17));
vtr.push_back(Person("小李", 22));
vtr.push_back(Person("小林", 19));
std::cout < < "查找年龄大于18的成员:" < < std::endl;
std::vector< Person >::iterator ret=find_if(vtr.begin(), vtr.end(), compare());
if (ret != vtr.end())
{
std::cout < < "查找成功" < < std::endl;
std::cout < < *ret < < std::endl;
}
}
void multimap_test()
{
cout < < "nmultimap条件查找示例:" < < endl;
multimap< Person, int, compare >p;
p.insert(make_pair(Person("小王", 18), 88));
p.insert(make_pair(Person("小刘", 17), 99));
p.insert(make_pair(Person("小刘", 17), 77));
p.insert(make_pair(Person("小林", 17), 77));
p.insert(make_pair(Person("小李", 22), 100));
for_each(p.begin(), p.end(), print());
cout < < "查找年龄Person("小刘", 17)" < < endl;
pair< multimap< Person,int >::iterator,multimap< Person,int >::iterator > ret=p.equal_range(Person("小刘", 17));
for (; ret.first != ret.second;ret.first++)
{
print temp;
temp(*(ret.first));
}
}
int main()
{
vector_test();
multimap_test();
system("pause");
}
adjacent_find() -查找相邻重复元素,查找成功返回相邻重复元素的首位置的迭代器,失败返回end
iterator adjacent_find(const _FwdIt _First, _FwdIt _Last, _Pr _Pred)
形参:_First、_Last --要查找的起始、结束区间迭代器
_Pred --查找条件
返回值:成功返回相邻重复元素的首位置的迭代器
失败返回end
示例:
#include < iostream >
#include < algorithm >
#include < vector >
#include < map >
using namespace std;
class Person
{
friend ostream& operator< <(ostream& cout, Person& p);
public:
Person(string name, int age) :name(name), age(age) {
}
bool operator==(Person& p)
{
if (p.age == this- >age && p.name == name)return true;
return false;
}
string name;
int age;
};
ostream& operator< <(ostream& cout, Person& p)
{
cout < < "姓名:" < < p.name < < "t年龄:" < < p.age;
return cout;
}
class compare
{
public:
bool operator()(const Person& p1, const Person& p2)const
{
if (p1.name == p2.name && p1.age == p2.age)return true;
return false;
}
};
void vector_test()
{
cout < < "tvector容器使用示例!" < < endl;
vector< Person >vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小刘", 18));
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小李", 18));
auto ret=adjacent_find(vtr.begin(), vtr.end(), compare());
if (ret != vtr.end())
{
cout < < "查找成功:";
cout < < *ret < < endl;
}
}
class print
{
public:
void operator()( pair< Person, int > p)const
{
cout < < p.first < < "t成绩:" < < p.second < < endl;
}
};
class mapcompare
{
public:
bool operator()(const Person& p1, const Person& p2)const
{
if ( p1.age == p2.age)return p1.name< p2.name;
return p1.age< p2.age;
}
bool operator()(const Person& p)
{
return p.age > 17;
}
};
void mulitmap_test()
{
cout < < "ntmultimap使用示例!" < < endl;
multimap< Person, int, mapcompare >mp;
mp.insert(make_pair(Person("小王", 16), 77));
mp.insert(make_pair(Person("小刘", 18), 88));
mp.insert(make_pair(Person("小刘", 18), 88));
mp.insert(make_pair(Person("小李", 19), 99));
cout < < "元素个数:" < < mp.size() < < endl;
for_each(mp.begin(), mp.end(), print());
auto begin=mp.lower_bound(Person("小刘", 18));
auto end = mp.upper_bound(Person("小刘", 18));
cout < < "查找相邻重复内容Person("小刘", 18):" < < endl;
for (; begin != end; begin++)
{
print p;
p(*begin);
}
}
int main()
{
vector_test();
mulitmap_test();
system("pause");
}
二分法查找元素:
bool binary_search(val); -- >查找成功返回true,失败返回false,保证数据顺序为从小到大
binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val, _Pr _Pred)
形参:_First、_Last --起始和结束迭代器
_Val --要查找的内容
_Pred --数据顺序方式(默认从小到大)
注意:调用该函数必须保证有序,否结果可能不准确
#include < iostream >
#include < vector >
#include < algorithm >
#include < functional >
using namespace std;
class Print
{
public:
void operator()(int v1)
{
cout < < v1 < < " ";
}
};
void test()
{
vector< int >vtr;
vtr.push_back(10);
vtr.push_back(3);
vtr.push_back(6);
vtr.push_back(2);
vtr.push_back(5);
cout < < "从小到大排序:" < < endl;
sort(vtr.begin(), vtr.end(), less< int >());
for_each(vtr.begin(), vtr.end(), Print());
cout < < endl;
auto ret=binary_search(vtr.begin(), vtr.end(), 5);
if (ret)
{
cout < < "查找5:查找成功" < < endl;
}
}
int main()
{
test();
system("pause");
}
查找指定成员个数:
count(const _InIt _First, const _InIt _Last, const _Ty& _Val)-- >返回查找到的个数
形参:_First、_Last --起始和结束迭代器
_Val --要查找的内容
返回值:返回查找到的个数
示例:
#include < iostream >
using namespace std;
#include < vector >
#include < algorithm >
class Person
{
public:
Person() {}
Person(string name, int age) :name(name), age(age) {
}
bool operator==(const Person p)const
{
if (age == p.age && name == p.name)return true;
return false;
}
bool operator==(const int age)const
{
if (this- >age == age)return true;
return false;
}
string name;
int age;
};
class compare
{
public:
bool operator()(const Person p1, const Person p2)const
{
if (p1.age == p2.age && p1.name == p2.name)return true;
return false;
}
};
void test()
{
vector< Person > vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小刘", 22));
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小林", 17));
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小蒋", 20));
auto cnt=count(vtr.begin(), vtr.end(), Person("小王", 18));
cout < < "符合要求的成员个数:" < < cnt < < endl;
cnt = count(vtr.begin(), vtr.end(), 22);
cout < < "年龄等于22的个数:" < < cnt < < endl;
}
int main()
{
test();
system("pause");
}
按条件查找:
count_if(_InIt _First, _InIt _Last, _Pr _Pred);--返回查找到个数
形参:_First、_Last --起始和结束迭代器
_Pred --查找条件
返回值:返回查找到的元素位置的迭代器
失败返回end()
#include < iostream >
#include < algorithm >
#include < vector >
using namespace std;
class Person
{
public:
Person() {}
Person(string name, int age) :name(name), age(age) {
}
string name;
int age;
};
class Compare
{
public:
bool operator()(const Person& p)const
{
if (p.name == "小王" && p.age > 18)return true;
return false;
}
};
void test()
{
vector< Person >vtr;
vtr.push_back(Person("小王", 18));
vtr.push_back(Person("小刘", 22));
vtr.push_back(Person("小王", 19));
vtr.push_back(Person("小林", 17));
vtr.push_back(Person("小王", 22));
vtr.push_back(Person("小蒋", 20));
auto ret=count_if(vtr.begin(), vtr.end(), Compare());
cout < < "姓名为小王,年龄大于18的个数:" < < ret< ;>
全部0条评论
快来发表一下你的评论吧 !