3、仿函数、适配器、算法(STL基础)

1 函数对象

重载函数调用操作符的类,其对象常称为函数对象,即它们是行为类似函数的对象,
也叫仿函数,其实就是重载“()”操作符,使得类对象可以像函数那样调用。
1.1 本质是一个类的对象,因此称为函数对象,也叫仿函数
1.2 函数对象 超出了普通函数的概念,可以拥有自己状态
1.3 函数对象可以作为函数参数
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class MyPrint
{
public:
	void operator()(int num)
	{
		cout << num << endl;
		m_Count++;
	}

	int m_Count = 0;
};

void myPrint02(int num)
{
	cout << num << endl;
}

void test01()
{
	MyPrint myPrint;
	myPrint(100); //本质是一个类的对象,因此称为函数对象,也叫仿函数

	myPrint02(100);
}


void test02()
{
	//函数对象 超出了普通函数的概念,可以拥有自己状态

	MyPrint myPrint;
	myPrint(100);
	myPrint(100);
	myPrint(100);
	myPrint(100);

	cout << "调用次数为: " << myPrint.m_Count << endl;
}


//函数对象可以作为函数参数
void doPrint(MyPrint myPrint , int num)
{
	myPrint(num);
}

void test03()
{
	doPrint(MyPrint(), 1000);
}

int main(){

	//test01();
	//test02();
	test03();

	system("pause");
	return EXIT_SUCCESS;
}

2 谓词

2.1 普通函数或者仿函数的返回值是bool类型,称为谓词
2.2 一元谓词
	2.2.1 查找容器中大于20的数字    find_if
2.3 二元谓词
	2.3.1 对容器进行排序  sort
2.4 lambda表达式 	[](){}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

//一元谓词
class GreaterThan20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};

void test01()
{
	vector<int>v;

	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	v.push_back(50);


	vector<int>::iterator ret = find_if(v.begin(), v.end(), GreaterThan20());
	if ( ret != v.end())
	{
		cout << "找到大于20的数字为: " << *ret << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


}


//二元谓词
void myPrintInt( int val)
{
	cout << val << " ";
}

class MyCompare
{
public:
	bool operator()(int v1,int v2)
	{
		return v1 > v2;
	}
};

void test02()
{
	vector<int>v;

	v.push_back(10);
	v.push_back(30);
	v.push_back(20);
	v.push_back(40);
	v.push_back(50);

	sort(v.begin(), v.end()); //从小到大

	for_each(v.begin(), v.end(), myPrintInt);//myPrintInt是回调函数不需要括号
	cout << endl;


	sort(v.begin(), v.end(), MyCompare());//MyCompare是仿函数,需要括号

	//lambda表达式  匿名函数  []代表lambda表达式标志  [](){}
	//等价于for_each(v.begin(), v.end(), myPrintInt);
	//[](int val){ cout << val << " "; }相当于myPrint
	for_each(v.begin(), v.end(), [](int val){ cout << val << " "; });

	cout << endl;
}


int main(){
	//test01();
	test02();

	system("pause");
	return EXIT_SUCCESS;
}

3 内建函数对象

3.1 引入头文件  #include< functional>
3.2 取反  negate<int>
3.3 加法  plus<int>
3.4 大于  greater<int>
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <functional>  //内建函数对象  头文件
#include <vector>
#include <algorithm>
/*
template<class T> T negate<T>//取反仿函数  一元运算
*/

void test01()
{
	negate<int>n;

	cout << n(10) << endl;

}
//template<class T> T plus<T>//加法仿函数
void test02()
{
	plus<int> p;

	cout << p(10, 10) << endl;

}

//template<class T> bool greater<T>//大于
void test03()
{
	vector<int>v;
	v.push_back(20);
	v.push_back(50);
	v.push_back(10);
	v.push_back(30);
	v.push_back(40);

	//从大到小排序
	sort(v.begin(), v.end(), greater<int>());


	for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
	cout << endl;
}

int main(){

	//test01();
	//test02();
	test03();

	system("pause");
	return EXIT_SUCCESS;
}

4 适配器

4.1 函数对象适配器
	4.1.1 //1、利用bind2nd 进行绑定
	4.1.2 //2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
	4.1.3 //3、加const
4.2 取反适配器
	4.2.1 一元取反  not1
	4.2.1.1 //1、利用not1进行取反
	4.2.1.2 //2、继承 public unary_function<int,bool>
	4.2.1.3 //3、加const
	4.2.2 二元取反  not2
4.3 函数指针适配器
	4.3.1 ptr_fun将普通函数指针 适配成函数对象
4.4 成员函数适配器
	4.4.1 如果存放的是对象实体   mem_fun_ref
	4.4.2 如果存放的是对象指针   mem_fun
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <string>

class MyPrint :public binary_function<int,int,void>
{
public:
	void operator()(int val , int start)const 
	{
		cout << "val = " << val << " start = " << start <<  " sum = " <<val +  start << endl;
	}
};
//1、函数对象适配器
void test01()
{
	vector<int>v;

	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}
	cout << "请输入起始累加值: " << endl;
	int num;
	cin >> num;

	for_each(v.begin(), v.end(), bind2nd( MyPrint(), num ) );//v绑定到val,num绑定到start
	//for_each(v.begin(), v.end(), bind1st(MyPrint(), num));//和上面相反
}

//1、利用bind2nd 进行绑定
//2、继承 public binary_function<参数1 类型,参数2类型,返回值类型>
//3、加const


//2、取反适配器
class GreaterThanFive:public unary_function<int,bool>
{
public:
	bool operator()(int val) const
	{
		return val > 5;
	}
};
void test02()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}

	//一元取反
	//vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( GreaterThanFive()));

	vector<int>::iterator pos = find_if(v.begin(), v.end(), not1( bind2nd( greater<int>() , 5 )));

	if (pos != v.end())
	{
		cout << "找到小于5的值为: " << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}


	//二元取反
	sort(v.begin(), v.end(),  not2 (less<int>()));//从大到小排序
	for_each(v.begin(), v.end(), [](int val){cout << val << endl; });
}

//1、利用not1进行取反
//2、继承 public unary_function<int,bool>
//3、加const


void myPrint3( int val , int start) 
{
	cout << val + start << endl;
}
//3、 函数适配器
void test03()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//将函数指针 适配成函数对象  ptr_fun
	for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint3), 1000));
}


//4、成员函数适配器
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	void showPerson()
	{
		cout << "成员函数----姓名: " << this->m_Name << " 年龄: " << this->m_Age << endl;
	}

	void addAge()
	{
		this->m_Age += 100;
	}

	string m_Name;
	int m_Age;
};

//void myPrint4( Person & p)
//{
//	cout << "姓名: " << p.m_Name << " 年龄: " << p.m_Age << endl;
//}

void test04()
{
	vector< Person > v;

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//利用 mem_fun_ref
	for_each(v.begin(), v.end(),  mem_fun_ref(&Person::showPerson));
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::addAge));
	for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}

int main(){

//	test01();
//	test02();
//	test03();
	test04();

	system("pause");
	return EXIT_SUCCESS;
}

5 常用遍历算法

5.1 for_each
	5.1.1 用于遍历
	5.1.2 有返回值
	5.1.3 可以绑定参数进行输出
5.2 transform
	5.2.1 搬运
	5.2.2 注意:目标容器要有容量
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>

class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << endl;
		m_Count++;
	}

	int m_Count = 0;
};

//for_each  用于遍历
//有返回值的
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}

	MyPrint print = for_each(v.begin(), v.end(), MyPrint());

	cout << "print.count = " << print.m_Count << endl;

}

//for_each可以绑定参数输出
class MyPrint2 :public binary_function<int,int,void>
{
public:
	void operator()(int val , int start) const
	{
		cout << val << endl;
		
	}

};

void test02()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	for_each(v.begin(), v.end(),  bind2nd( MyPrint2(), 1000));

}


//transform算法 将指定容器区间元素搬运到另一容器中
class MyTransform
{
public:
	int operator()(int val)
	{	
		return val + 10000;
	}
};
void test03()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}



	vector<int>v2;
	v2.resize(v.size());

	transform(v.begin(), v.end(), v2.begin(), MyTransform());//v搬到v2

	for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });

}

int main(){
	//test01();
	//test02();
	test03();


	system("pause");
	return EXIT_SUCCESS;
}

6 常用查找算法

6.1 find  查找
6.2 find_if  按条件查找
6.3 adjacent_find算法 查找相邻重复元素
6.4 binary_search算法 二分查找法
	6.4.1 注意: 在无序序列中不可用
6.5 count算法 统计元素出现次数
6.6 count_if 按条件进行统计
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <string>
#include <functional>
/*
find算法 查找元素
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param value 查找的元素
@return 返回查找元素的位置


find_if算法 条件查找
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param  callback 回调函数或者谓词(返回bool类型的函数对象)
@return bool 查找返回true 否则false

*/



void test01()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}
	
	vector<int>::iterator pos = find(v.begin(), v.end(), 5);

	if (pos != v.end())
	{
		cout << "找到了元素:" << *pos << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	bool operator==(const Person & p)
	{
		return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
	}

	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person> v;

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

 
	vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
	if (pos != v.end())
	{
		cout << "找到了元素  姓名: " << (*pos).m_Name << " 年龄: " << (*pos).m_Age << endl;
	}

}

class MyComparePerson :public binary_function< Person *, Person *, bool>
{
public:
	bool operator()(  Person * p1 ,  Person *p2 ) const
	{
		return p1->m_Name == p2->m_Name  && p1->m_Age == p2->m_Age; 
	}
};

void test03()
{
	vector<Person *> v;

	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);

	Person * p = new Person("bbb", 20);

	vector<Person *>::iterator pos = find_if(v.begin(), v.end(), bind2nd( MyComparePerson() ,p)  );

	if (pos != v.end())
	{
		cout << "找到了元素--- 姓名: " << (*pos)->m_Name << " 年龄: " << (*pos)->m_Age << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}


//adjacent_find算法 查找相邻重复元素
void test04()
{
	vector<int>v;

	v.push_back(3);
	v.push_back(2);
	v.push_back(300);
	v.push_back(300);
	v.push_back(6);
	v.push_back(3);

	vector<int>::iterator ret = adjacent_find(v.begin(), v.end());

	if (ret != v.end())
	{
		cout << "找到了相邻的重复元素: " << *ret << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

}


/*
binary_search算法 二分查找法
注意: 在无序序列中不可用
*/

void test05()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//v.push_back(4);  必须是有序序列,如果无效 结果未知

   bool ret=binary_search(v.begin(), v.end(), 2);
   if (ret)
   {
	   cout << "查到了数据2" << endl;
   }
   else
   {
	   cout << "未找到数据2" << endl;
   }

}

/*
count算法 统计元素出现次数
@param beg 容器开始迭代器
@param end 容器结束迭代器
@param  value回调函数或者谓词(返回bool类型的函数对象)
@return int返回元素个数
*/
class GreaterThan3
{
public:
	bool operator()(int val)
	{
		return val >= 3;
	}
};

void test06()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	v.push_back(3);
	v.push_back(3);
	v.push_back(3);

	int num = count(v.begin(), v.end(), 3);

	cout << "3的个数为: " << num << endl;


	//统计大于等于3的个数
	num = count_if(v.begin(), v.end(), GreaterThan3());
	// 0 1 2 3 4 5 6 7 8 9 3 3 3 
	cout << "大于等于3的个数为: " << num << endl;

}


int main(){
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
	test06();


	system("pause");
	return EXIT_SUCCESS;
}

7 常用排序算法

7.1 merge 合并  
	7.1.1 将两个容器合并到 目标容器中 
	7.1.2 注意: 两个容器必须是有序序列
	7.1.3 目标容器必须有容量
7.2 sort 排序
7.3 random_shuffle 洗牌
7.4 reverse 反转
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <ctime>
/*
merge算法 容器元素合并,并存储到另一容器中
注意 : 两个容器必须是有序的,顺序要一致
*/

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i + 1);
	}

	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(), [](int val){cout << val << " "; });

}

//sort
void test02()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}

	//降序排序

	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
	cout << endl;
}


//random_shuffle算法 对指定范围内的元素随机调整次序
void test03()
{
	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(), [](int val){cout << val << " "; });
	cout << endl;
}

//reverse算法 反转指定范围的元素
void test04()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	cout << "反转前打印:" << endl;
	for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
	cout << endl;


	reverse(v.begin(), v.end());

	cout << "反转后打印: " << endl;
	for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
	
	cout << endl;
}

int main(){

	srand((unsigned int)time(NULL));

	//test01();
	//test02();
	//test03();
	test04();


	system("pause");
	return EXIT_SUCCESS;
}

8 常用的拷贝和替换算法

8.1 copy 拷贝
	8.1.1 实现打印  copy(v.begin(),v.end() , ostream_iterator<int>(cout , “ ”));
8.2 replace 替换
8.3 replace_if 按条件替换
8.4 swap 交换
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <iterator>
//copy算法 将容器内指定范围的元素拷贝到另一容器中
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}

	vector<int>v2;
	v2.resize(v.size());
	copy(v.begin(), v.end(), v2.begin());

	//for_each(v2.begin(), v2.end(), [](int val){cout << val << " "; });
	//cout << endl;

	copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;
}

//replace算法 将容器内指定范围的旧元素修改为新元素
//replace_if(iterator beg, iterator end, _callback, newvalue) 按条件替换

class MyReplace
{
public:
	bool operator()(int val)
	{
		return val > 3;
	}
};
void test02()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	//将容器中的3替换为 3000
	replace(v.begin(), v.end(), 3, 3000);

	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
	cout << endl;



	//将容器中所有大于3的都替换为 30000;
	replace_if(v.begin(), v.end(), MyReplace() , 30000);
	// 0 1 2 30000 ...
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));//打印输出
	cout << endl;
}


//swap交换
void test03()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	vector<int>v2(10, 100);

	cout << "交换数据前:" << endl;
	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;


	cout << "交换数据后:" << endl;
	swap(v, v2);

	copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	copy(v2.begin(), v2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

}

int main(){

	//test01();
	//test02();
	test03();
	system("pause");
	return EXIT_SUCCESS;
}

9 常用的算数生成算法

9.1 头文件  #include <numeric>
9.2 accumulate算法 计算容器元素累计总和
9.3 fill算法 向容器中添加元素
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric> //accumulate算法头文件


//accumulate算法 计算容器元素累计总和
void test01()
{
	vector<int>v;
	for (int i = 0; i <= 100;i++)
	{
		v.push_back(i);
	}

	int num = accumulate(v.begin(), v.end(),1000); // 参数3代表 累加起始值

	cout << "num = " << num << endl;
}

//fill算法 向容器中添加元素
void test02()
{
	vector<int>v;
	v.resize(10);

	fill(v.begin(), v.end(), 100);

	for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
	cout << endl;

}


int main(){

	//test01();
	test02();


	system("pause");
	return EXIT_SUCCESS;
}

10 常用集合算法

10.1 set_intersection算法 求两个set集合的交集
10.2 set_union算法 求两个set集合的并集
10.3 set_difference算法 求两个set集合的差集
10.4 注意:两个集合必须是有序序列
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include <vector>
#include <algorithm>

/*
set_intersection算法 求两个set集合的交集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest  目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
*/
void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10;i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int>vTarget;

	vTarget.resize(min(v1.size(), v2.size()));

	vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });
	cout << endl;
}


/*
set_union算法 求两个set集合的并集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest  目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
*/

void test02()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int>vTarget;
	vTarget.resize(v1.size() + v2.size());
	vector<int>::iterator itEnd  = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), itEnd , [](int val){cout << val << " "; });
	cout << endl;
}

/*
set_difference算法 求两个set集合的差集
注意:两个集合必须是有序序列
@param beg1 容器1开始迭代器
@param end1 容器1结束迭代器
@param beg2 容器2开始迭代器
@param end2 容器2结束迭代器
@param dest  目标容器开始迭代器
@return 目标容器的最后一个元素的迭代器地址
*/

void test03()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int>vTarget;
	vTarget.resize( max(v1.size(),v2.size()) );

	// A 与 B 差集
	//vector<int>::iterator itEnd  = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	// B 与 A 差集
	vector<int>::iterator itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());


	for_each(vTarget.begin(), itEnd, [](int val){cout << val << " "; });
	cout << endl;

}


int main(){
	//test01();
	//test02();
	test03();

	system("pause");
	return EXIT_SUCCESS;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《Effective STL:50条有效使用标准模板库函数的方法》是由Scott Meyers所著的一本关于使用C++标准库的优秀指南。该书共分为50个章节,每个章节都介绍了一条有效地使用STL函数的方法。 书中的目录如下: 第一章:导论 1. 了解STL的内涵和背景 2. 了解STL的相关技术 第二章:使用容器类 3. 使用vector和string来管理内存 4. 使用list来管理内存 5. 了解适当的使用deque 6. 了解适当的使用vector和string 7. 使用set和multiset来封装搜索条件 8. 使用map和multimap来封装搜索条件 第三章:STL算法 9. 了解STL算法框架 10. 使用泛型算法 11. 使用常见的泛型算法 12. 使用STL的数值算法 13. 提高算法的效率 14. 使用new后自动进行内存释放 15. 使用函数对象来代替函数指针 16. 了解STL的predicate、binary predicate和unary function 17. 使用特定的函数对象来改善程序性能 第四章:指针和迭代器 18. 使用容器迭代器来遍历元素 19. 使用算法函数来遍历元素 20. 使用reverse_iterator来逆序遍历容器 21. 理解插入迭代器的作用 22. 使用迭代器的适配器 第五章:关联容器和哈希表 23. 了解关联容器的选择 24. 了解SGI中哈希函数的实现原理 25. 使用自定义的比较函数和哈希函数来操作关联容器和哈希表 26. 使用invalid_handle来避免关联容器的错误 27. 自定义哈希表中的键来提高程序性能 第六章:字符串 28. 使用string和char*来处理字符串 29. 使用C++11中的新特性来处理字符串 第七章:容器适配器和迭代器适配器 30. 使用stack和queue来封装算法 31. 使用heap和priority_queue来实现特殊需求 32. 使用insert_iterator和ostream_iterator来实现输出重定向 第八章:算法适配器函数适配器 33. 使用算法适配器来改进函数功能 34. 使用函数适配器来改进函数功能 第九章:异常安全 35. 确保异常安全 36. 使用RAII管理资源 第十章:性能调优和测试 37. 了解性能调优的基本原则 38. 使用优化技术来提高性能 39. 写好测试,保障程序的正确性 第十一章:STL的特殊技巧 40. 使用函数对象的成员函数来代替算法函数 41. 使用成员函数或non-member non-friend函数来代替member函数 42. 使用函数对象和指针来封装条件 43. 使用作用域内解析来限制函数的参数和类型 第十二章:使用STL 44. 了解STL的实现原理 45. 追踪STL实现的细节 46. 打破STL的限制 47. 使用STL来解决实际问题 在这本书中,读者将学到如何正确和高效地使用STL的容器、算法、迭代器和适配器等各个方面的知识。每个章节都通过示例代码和详细的解释来展示如何遵循STL的最佳实践。通过学习本书,读者能够更好地使用C++的标准库,并且编写出高效可靠的程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值