/*
第二篇 C++STL泛化技术基础
第4章 C++STL泛型库概述
4.1 C++STL的发展历程
4.2 C++STL的各种实现版本
4.3 C++STL的Visual C++编译
4.4 C++STL的体系结构
4.5 C++STL存在的一些问题
4.6 本章小结
第二篇 C++STL泛化技术基础
第4章 C++STL泛型库概述
4.1 C++STL的发展历程
4.2 C++STL的各种实现版本
4.2.1 HP STL
4.2.2 SGI STL
4.2.3 STLport
4.2.4 P.J.Plauger STL
4.2.5 Rouge Wave STL
4.3 C++STL的Visual C++编译
4.4 C++STL的体系结构
4.4.1 容器(Container)
4.4.2 迭代器(Iterator)
4.4.3 算法(Algorithm)
4.4.4 函数对象(Function Object)
4.4.5 适配器(Adapter)
4.4.6 内存分配器(Allocator)
4.4.7 概念(Concept)和模型(Model)
4.5 C++STL存在的一些问题
4.6 本章小结
*/
// 第4章 C++STL泛型库概述
// 4.1 C++STL的发展历程 ---------------------------------------------------------------------------------------
// 4.2 C++STL的各种实现版本 ---------------------------------------------------------------------------------------
// 4.3 C++STL的Visual C++编译 ---------------------------------------------------------------------------------------
// 51
#include <vector>
#include <iostream>
#include <string>
int main(void)
{
using namespace std;
vector < string > vec;
vector < string > ::const_iterator i;
vec.push_back("dog");
vec.push_back("bird");
vec.push_back("girl");
vec.push_back("boy");
vec.push_back("Hello,there");
for(i = vec.begin(); i != vec.end(); ++i)
{
cout << (*i) << endl;
}
return 0;
}
// 4.4 C++STL的体系结构 ---------------------------------------------------------------------------------------
// 54
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
vector<int> v;
v.push_back(6);
v.push_back(8);
v.push_back(31);
vector<int>::iterator result=
find(v.begin(),v.end(),8);
cout << *result << endl;
return 0;
}
//55
// bind1st(not_equal_to<int>(),0)
template <class Operation> class binder1st: public unary_function
<typename Operation::second_argument_type, typename Operation::result_type>
{
protected:
Operation op;
typename Operation::first_argument_type value;
public:
binder1st(
const _Operation &x, const typename Operation::first_argument_type &y)
: op(x), value(y){} // 构造函数。not_equal_to->op 0->value
typename Operation::result_type operator()
(const typename _Operation::second_argument_type &x)const
{
return op(value, x); // 0 not_equal_to x
}
};
// my test, bind
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
vector<int> v;
v.push_back(1);
v.push_back(10);
v.push_back(20);
size_t i=count_if(v.begin(),v.end(),
bind2nd(less_equal<int>(),10)); // <=10的数有几个?2个
cout << i << endl;
return 0;
}
// my test ,bind2nd
// 可见书中的例子是错的。bind1st(not_equal_to<int>(),0),不对。value,应该在第一个位置
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
vector<int> v;
v.push_back(1);
v.push_back(10);
v.push_back(20);
size_t i=count_if(v.begin(),v.end(),
bind2nd(not_equal_to<int>(),10)); // !=10的数有几个?2个
cout << i << endl;
return 0;
}
/*
c++ primer
14.8. 调用操作符和函数对象
标准库定义了两个绑定器适配器:bind1st 和 bind2nd。每个绑定器接受一个函数对象和一个值。
正如你可能想到的,bind1st 将给定值绑定到二元函数对象的第一个实参,bind2nd 将给定值绑定到二元函数对象的第二个实参。
标准库还定义了两个求反器:not1 和 not2。你可能已经想到的,not1 将一元函数对象的真值求反,not2 将二元函数对象的真值求反。
这里,首先将 less_equal 对象的第二个操作数绑定到 10,实际上是将该二元操作转换为一元操作。
*/
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void)
{
vector<int> v;
v.push_back(1);
v.push_back(10);
v.push_back(20);
size_t i=count_if(v.begin(),v.end(),
not1(bind2nd(not_equal_to<int>(),10))); // 不是!=10的数有几个?1个
cout << i << endl;
return 0;
}
// 4.5 C++STL存在的一些问题 ---------------------------------------------------------------------------------------
// 4.6 本章小结 ---------------------------------------------------------------------------------------
TOP