标准模板库STL
set容器
四种查找数据的方法:
例子:
set<int> s;
//1.查找数据
//返回值:指向找到位置的迭代器,如果没有元素 返回end()
set<int>::iterator it = s.find(8);
if(it != s.end())
cout << "找到" << endl;
else
cout << "没找到" << endl;
//2.查找数据
//返回值:返回的是第一个 >= 要找元素的迭代器
it = s.lower_bound(3);
if(it != s.end())
cout << "s.lower_bound(3) 找到元素:" << *it << endl;
else
cout << "没有找到元素" << endl;
it = s.lower_bound(5);
if(it != s.end())
cout << "s.lower_bound(5) 找到元素:" << *it << endl;
else
cout << "没有找到元素" << endl;
//3.查找
//返回值:返回的是第一个 > 要查找元素的迭代器
it = s.upper_bound(3);
if(it != s.end())
cout << "s.upper_bound(3) 找到元素:" << *it << endl;
else
cout << "没有找到元素" << endl;
//4.查找
//第一个值:等价于lowerbound >=
//第二个值:等价于upper_bound >
// std::pair<set<int>::iterator, set<int>::iterator> ret =s.equal_range(3);
auto ret = s.equal_range(3);
if(ret.first != s.end())
cout << "s.equal_range(3)first 找到元素:" << *(ret.first) << endl;
if(ret.second != s.end())
cout << "s.equal_range(3)second 找到元素" << *(ret.second) << endl;
multiset : 允许添加重复元素(与set的区别)
map(键值对)
map是标准的关联式容器。
map<int, string> m;
例子:
void print( map<int, string> &m)
{
map<int, string>::iterator it = m.begin();
while(it != m.end())
{
cout << "name = " << it->second << ", id = " << it->first << endl;
it++;
}
}
map容器的插入数据
//map 不允许键重复
void func4()
{
map<int, string> m;
//1.构建pair 对组
m.insert(pair<int, string>(1, "小明1"));
m.insert(pair<int, string>(2, "小明2"));
print(m);
cout << "_________________________"<< endl;
//2.使用make_pair
m.insert(make_pair(3, "小明3"));
m.insert(make_pair(4, "小明4"));
print(m);
cout << "__________________________" << endl;
//3.使用 value_type
m.insert(map<int, string>::value_type(5, "小明5"));
m.insert(map<int, string>::value_type(6, "小明6"));
print(m);
cout << "__________________________" << endl;
//4.可以 用[]进行插入元素 [键] = 值 []内部的值是map的键 不能当做数组下标
m[7] = "小明7";
m[8] = "小明8";
print(m);
}
pair 对组 可以构建一对数据
第一个元素使用first表示
第二个元素使用second 表示
insert 返回值:
第一个:指向插入位置的迭代器,如果插入失败 返回end()
第二个:表示是否插入成功
注:insert 是函数, 其返回值可以判断是否插入成功
例子:
pair<map<string, int>::iterator, bool> ret = m.insert(pair<string, int>("小明2", 2));
if(ret.second)
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
[] 运算符重载:如果数据存在,则覆盖原有的值, 如果数据不存在,则插入一条新数据
例子:
m["小明8"] = 10;
multimap容器
与mapde 区别:允许键值重复
例子:
multimap<int, string> mp;
mp.insert(make_pair(1,"小明1"));
mp.insert(make_pair(1,"小明1"));
mp.insert(make_pair(1,"小明1"));
mp.insert(make_pair(1,"小明1"));
multimap<int, string>::iterator it = mp.begin();
while(it != mp.end())
{
cout << "id = " << it->first << ", name = " << it->second << endl;
it++;
}
算法
函数对象: 一个对象,可以像函数一样使用 重载了() 运算符的对象
函数对象根据参数不同:一元函数对象(参数只有一个) 和二元函数对象(参数有两个)
例子:
//一元函数对象的使用
class Show
{
public:
Show()
{
count = 0;
sum = 0;
}
void operator()(int a)
{
cout << a << endl;
count++;
sum += a;
}
public:
int count;
int sum;
};
void print(int a)
{
cout << a << endl;
}
void func1()
{
print(10); //函数
Show show; //对象
show(10);
}
//遍历
void func2()
{
vector<int> v;
for(int i = 0; i < 10; ++i)
{
v.push_back(i);
}
// for_each(v.begin(), v.end(), print);
// for_each(v.begin(), v.end(), Sum);
// cout << sum << endl;
Show s;
//for_each 函数有返回值,可以用对象接收
//参数1:起始位置 参数2:截止位置 参数3:操作
s = for_each(v.begin(), v.end(),s);
cout << s.count << endl;
cout << s.sum << endl;
}
//二元函数对象的使用
void func3()
{
vector<int> v1;
vector<int> v2;
vector<int> v3(3);
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v2.push_back(4);
v2.push_back(5);
v2.push_back(6);
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), Add);
for_each(v3.begin(), v3.end(), print);
transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), mul());
for_each(v3.begin(), v3.end(), print);
}
谓词: 函数对象返回值是bool类型====>用于判断
谓词根据参数不同:一元谓词(只有一个形参)、二元谓词(有两个形参)
例子:
//一元谓词
bool greater3(int a)
{
return a > 3;
}
class Div
{
public:
Div(int num)
{
this->num = num;
}
bool operator ()(int a)
{
return a%num == 0;
}
private:
int num;
};
void func()
{
vector<int> v;
for(int i = 0; i < 10; ++i)
v.push_back(i);
int ret = count(v.begin(), v.end(), 3);
cout << ret << endl;
ret = count_if(v.begin(), v.end(), greater3);
cout << ret << endl;
ret = count_if(v.begin(), v.end(), Div(2));
cout << ret << endl;
vector<int>::iterator it = find_if(v.begin(), v.end(),greater3);
cout << "第一个大于3的数 " << *it << endl;
}
//二元谓词
void print(int a)
{
cout << a << endl;
}
bool myCompare(int left, int right)
{
//return left < right;
return left > right;
}
void func2()
{
vector<int> v;
v.push_back(1);
v.push_back(8);
v.push_back(9);
v.push_back(4);
v.push_back(2);
v.push_back(5);
//sort 支持自己定义排序规则, 默认则从小到大排
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), print);
//返回值为假则交换
sort(v.begin(), v.end(), myCompare);
for_each(v.begin(), v.end(), print);
}
预定义函数对象: 系统写好的函数对象
使用预定义函数对象包含头文件
函数适配器
绑定器:通过将二元函数对象的某个形参设置成一个固定的值,从而将二元函数对象变成一元函数对象
bind1st(函数对象, 值) 将值绑定到函数对象的第一个参数
bind2nd(函数对象, 值) 将值绑定到函数对象的第二个参数
取反器
not1 对一元函数对象的值进行取反
not2 对二元函数对象的值进行取反
常用算法:
查找: find find_if binary_search
统计: count count_if
排序: sort 随机排序(洗牌算法) random_shuffle ( 要用到srand)
遍历: for_each
转换: transform
交换: swap
逆序: reverse