1.函数对象
#include <iostream>
#include <list>
using namespace std;
bool removeFunction(int n)
{
if(n < 4)
{
return true; // 删除比4小的值
}
else
{
return false;
}
}
void out(int n)
{
cout << n << " ";
}
// 函数指针类型
// typedef定义了一个名为RemoveFunction_Pointer的数据类型,
// 这个类型是指向某个函数的指针,这个函数的返回值是bool类型,传入参数是int类型
typedef bool (*RemoveFunction_Pointer)(int);
class RemoveClass
{
private:
int mValue;
public:
RemoveClass(int value)
{
mValue = value;
}
bool operator()(int n) // 操作符重载,这个n是遍历容器时传入的参数值
{
if(n >= mValue)
{
return false;
}
else
{
return true; // 因为用的是remove_if方法,所以返回true表示删除该数
}
}
};
int main(int argc, const char * argv[]) {
// 函数对象:函数、函数指针、对象
list<int> values {1,4,3,2,8,5};
//values.remove_if(removeFunction); //打印 4 8 5,remove_if方法的参数是函数对象,循环到容器中的每个值时,都会调用这个函数对象
//RemoveFunction_Pointer p = removeFunction; // 定义了函数指针p,同时令该指针指向函数removeFunction
//values.remove_if(p); // 打印 4 8 5 即函数指针是函数对象中的一种形式
RemoveClass removeClass(5); // ()操作符被重载
values.remove_if(removeClass); // 删除小于5的数
for_each(values.begin(), values.end(), out);
cout << endl; // 8 5
return 0;
}
2.STL中预定义的函数对象(函数符)
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int op_increase (int i) { return ++i; }
int main(int argc, const char * argv[]) {
// 预定义的函数对象(函数符)
vector<int> values1{1,2,3,4,5,6};
vector<int> values2{6,7,8,9,10,11};
vector<int> values3;
values3.resize(values1.size()); // 将values3的大小扩展为6
// transform的最后一个参数就是函数对象/函数符,整体的意思就是,values1的第一个元素和values2的第一个元素相加,存到values3(从开头开始存),一次类推,直到values1结束
transform(values1.begin(), values1.end(), values2.begin(), values3.begin(), plus<int>());
for(int i = 0; i < values3.size();i++)
{
cout << values3[i] << " ";
}
cout << endl; // 7 9 11 13 15 17
vector<int> values4;
values4.resize(values1.size());
transform(values1.begin(), values1.end(), values2.begin(), values4.begin(), multiplies<int>());
for(int i = 0; i < values4.size();i++)
{
cout << values4[i] << " ";
}
cout << endl; // 6 14 24 36 50 66
return 0;
}
3.STL中的算法函数
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
bool compareInt(int n1, int n2)
{
if(n1 <= n2)
{
return true; // 升序
}
else
{
return false;
}
}
int main(int argc, const char * argv[]) {
// STL中的算法
// 1. 非修改式
// 2. 修改式序列操作
// 3. 排序与相关操作
// 4. 通用数字运算
vector<int> values{3,5,6,8,6,10};
// 1. 非修改式
vector<int>::iterator it = find(values.begin(), values.end(), 6);
// 已经找到
if(it != values.end())
{
cout << *it << endl; // 6
cout << *(it + 1) << endl; // 8 输出后面一个元素
}
// 2. 修改式序列操作
replace(values.begin(), values.end(), 6, 100); // 替换,把所有的6换成100
for(auto it = values.begin();it != values.end();it++)
{
cout << *it << " ";
}
cout << endl; // 3 5 100 8 100 10
// 3. 排序与相关操作
sort(values.begin(), values.end(),compareInt);
for(auto it = values.begin();it != values.end();it++)
{
cout << *it << " ";
}
cout << endl; // 3 5 8 10 100 100
// 4. 通用数字运算
int n = accumulate(values.begin(), values.end(), 10); // 累加,将参数一到参数二区间里的所有值累加后,再加上10(参数三)
cout << n << endl; // 236
return 0;
}
4.STL与string
#include <iostream>
#include <aliasdb.h>
#include <string>
#include <vector>
using namespace std;
bool compareChar(char c1, char c2)
{
if(c1 >= c2)
{
return true; // 降序
}
else
{
return false;
}
}
int main(int argc, const char * argv[]) {
// STL与string
string str = "sldfjlsjl23ssfljsjjlj234dsdf";
sort(str.begin(), str.end(), compareChar);
cout << str << endl; // sssssslllllfffddd43322
string str1;
str1.resize(str.size());
copy(str.begin(),str.end(), str1.begin());
cout << str1 << endl; // sssssslllllfffddd43322
vector<char> charValues;
charValues.resize(str.size()); // copy要求被复制函数要有内存空间
copy(str.begin(), str.end(), charValues.begin()); // 会将字符串拆成一个个的字符,传给charValues
for(char c:charValues)
{
cout << "<" << c << ">";
}
cout << endl; // <s><s><s><s><s><s><l><l><l><l><l><f><f><f><d><d><d><4><3><3><2><2>
return 0;
}