前言
本系列内容为程序媛学习C++时做的笔记。以代码为主,并备注了打印结果以及详尽的解释注释。希望对你有所帮助。
函数适配器
bind1st:绑定固定值到二元函数的第一个参数位置。
bind2nd:绑定固定值到二元函数的第二个参数位置。
#include <iostream>
#include "set";
#include "algorithm"; //find_if/equal_to是算法包的
using namespace std;
int main() {
set<string, less<string>> s;
s.insert("B");
s.insert("A");
s.insert("C");
//equal_to是一个二元谓词(x,y)。现在不想给第一个参数赋值,所以用到bind2nd,只赋值给第二个参数y。
//而第一个参数会从s.begin(),s.end()区间依次取出来,放到x的位置。
set<string, less<string>>::iterator it =
find_if(s.begin(), s.end(), bind2nd(equal_to<string>(), "A"));
if (it != s.end()) {
cout << "查找到了" << endl; //A找到了
} else {
cout << "没有查找到" << endl;
}
return 0;
}
算法包使用(algorithm)
transform
自定义每一个元素的转换。
resize:调整大小
struct __unary_op {
int operator()(const int first) {
return first + 6;//让每个元素都+6
}
};
int main() {
vector<int> v;
v.insert(v.begin(), 1000);
v.insert(v.begin(), 2000);
v.insert(v.begin(), 3000);
vector<int> v2;
v2.resize(v.size());
transform(v.begin(), v.end(), v2.begin(), __unary_op());
for (auto it = v2.begin(); it != v2.end(); it++) {
cout << "加后:" << *it << endl;//3006 2006 1006
}
return 0;
}
附transform源码:
template<typename _InputIterator, typename _OutputIterator, typename _UnaryOperation>
_OutputIterator transform(_InputIterator __first, _InputIterator __last,
_OutputIterator __result, _UnaryOperation __unary_op) {
//省略...
for (; __first != __last; ++__first, (void) ++__result)
*__result = __unary_op(*__first);
return __result;
}
find_if
find 没有自定义仿函数,find_if有。
class __pred {
public:
int num;
__pred(int num) : num(num) {}
bool operator()(const int value) {
return num == value;
}
};
int main() {
vector<int> v;
v.insert(v.begin(), 1000);
v.insert(v.begin(), 2000);
v.insert(v.begin(), 3000);
auto it = find_if(v.begin(), v.end(), __pred(2000));
cout << (it != v.end()) << endl; //1
return 0;
}
count
统计元素的个数。count没有仿函数,count_if有。
vector<int> v;
v.insert(v.begin(), 1000);
v.insert(v.begin(), 2000);
v.insert(v.begin(), 2000);
v.insert(v.begin(), 2000);
int number = count(v.begin(), v.end(), 2000);
cout << "找到了:" << number << "个" << endl;//找到了:3个
merge
vector<int> v1;
v1.insert(v1.begin(), 1);
v1.insert(v1.begin(), 2);
vector<int> v2;
v2.insert(v2.begin(), 3);
v2.insert(v2.begin(), 4);
vector<int> v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for (auto it = v3.begin(); it != v3.end(); it++) {
cout << *it << endl;//2 1 4 3
}
sort
对容器元素进行排序。
vector<int> v;//vector默认没有排序功能
v.insert(v.begin(), 2);
v.insert(v.begin(), 1);
v.insert(v.begin(), 5);
v.insert(v.begin(), 4);
sort(v.begin(), v.end());
for (auto it = v.begin(); it != v.end(); it++) {
cout << *it << endl;//1245
}
sort(v.begin(), v.end(), greater<int>());//降序
for (auto it = v.begin(); it != v.end(); it++) {
cout << *it << endl;//5421
}
random_shuffle
随机打乱元素的顺序。
shuffle [ˈʃʌfl] 洗牌
char arr[] = {'1', '2', '3', '4', '5', '6'};
random_shuffle(arr, arr + 6);
for (int i = 0; i < 6; ++i) {
cout << arr[i] << endl;//5 2 4 3 1 6
}
copy
copy容器1的元素 到 容器2。
vector<int> v;
v.push_back(5);//按顺序添加元素
v.push_back(2);
v.push_back(3);
vector<int> v2;
v2.resize(v.size());
copy(v.begin(), v.end(), v2.begin());
for (auto it = v2.begin(); it != v2.end(); it++) {
cout << *it << endl;//523
}
replace
替换元素内容。
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(10);
v.push_back(10);
//replace参数:查找的开始位置 ; 查找结束位置 ; 要替换的元素 ; 要替换成的值。
replace(v.begin(), v.begin() + 3, 10, 100);
for (auto it = v.begin(); it != v.end(); it++) {
cout << *it << endl;//100 20 100 10
}