Essential C++ 第三章-3.1
泛型编程风格
Standard Template Library(STL)两种组件构成:
容器(container)(vector、list、set、map…)
操作以上容器的所谓的泛型算法(generic algorithm)(find(), sort(), replace(), merge()…)
序列式容器:
vector
string (它不是类模板)
list
forward_list
deque
queue
priority_queue
stack
有序关联容器:
map
multimap
set
multiset
无序关联容器:
unordered_map
unordered_multimap
unordered_set
unordered_multiset
泛型算法:提供了许多可施行于容器类及数组类型上的操作行为。和想要操作的元素类型无关。
泛型算法通过 function template 技术,达成“与操作对象类型相互独立”的目的,而达成“与容器无关”的诀窍,就是不要直接在容器身上进行操作。取代方式是:藉由一对 iterators (first, last),标示我们想进行迭代的元素区间。每次first前进一个位置。
3.1 指针的算术运算(The Arithmetic of Pointers)
array[ 2 ] 等同于 *(array + 2) 地址运算中+2会把指针所指向的类型的容量大小算进去
array定义不可能为空,vector定义可以为空,所以要判断vector是不是为空
list:双向循环链表
以下是指针算术运算代码
#include <iostream>
#include <vector>
#include <string>
//#include <ostream>
using namespace std;
/*
const int* find( const vector<int> &vec, int value){
for (int i = 0; i < vec.size(); ++i)
if ( vec[ i ] == value )
return &vec [ i ];
return 0;
}
template <typename elemType>
const elemType* find (const vector<elemType> &vec, elemType value){
for (int i = 0; i < vec.size(); ++i)
if ( vec[ i ] == value )
return &vec [ i ];
return 0;
}*/
//传入大小范围
template <typename elemType>
const elemType* find ( const elemType *array, int size, const elemType &value);
//传入读取操作的终点
template <typename elemType>
const elemType* find ( const elemType *array, const elemType *sentinel, const elemType &value);
template <typename elemType>
const elemType* find ( const elemType *array, int size, const elemType &value){
cout << "\tWelcome to elemType* find ( const *, int size, const &)" << endl;
if ( ! array || size < 1)
return 0;
for ( int i = 0; i < size; ++i)
if ( array[ i ] == value )//*(array+i)
return &array[ i ];
/* for ( int i = 0; i < size; ++i, ++array )
if ( *array == value )
return array;*/
return 0;
}
template <typename elemType>
const elemType* find ( const elemType *first, const elemType *last, const elemType &value){
cout << "\tWelcome to elemType* find ( const *, const *, const &)" << endl;
if ( ! first || ! last )
return 0;
for ( ; first != last; ++first )
if ( *first == value)
return first;
return 0;
}
//获取第一个元素的地址
template <typename elemType>
inline const elemType* begin( const vector<elemType> &vec )
{ return vec.empty() ? 0 : &vec[0]; }
//获取最后一个元素的地址
template <typename elemType>
inline const elemType* end( const vector<elemType> &vec )
{ return vec.empty() ? 0 : &vec[vec.size()]; }
template <typename elemType>
void display( const vector<elemType> &vec, ostream &os = cout){
os << "display template\n";
vector<elemType>::const_iterator iter = vec.begin();
vector<elemType>::const_iterator end_it = vec.end();
for( ; iter != end_it; ++iter )
os << *iter << ' ';
os << endl;
}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9,523};
string str_test[4] = {"pooh", "piglet", "Rodge", "test" };
vector<int> test(a, a+10);
vector<int> testnull;
const vector<string> cs_vec(str_test, str_test+4);
vector<string>::const_iterator iter_test = cs_vec.begin();
cout << iter_test->size() << endl;
/*for (int i =0; i <test.size(); i++)
cout << test.at(i) << " ";*/
for ( vector<int>::iterator iter = test.begin();
iter != test.end(); ++iter )
cout << *iter << " ";
cout << endl;
display( test );
//if( find(test, 523) ) cout << "find--" <<*(find(test, 523))<< endl;
//else cout << "not find" << endl;
int ia[8] = {1, 2, 3, 523, 4, 5, 6, 7};
double da[6] = {1.5, 2.4, 5.4, 5.23, 8.8, 5.5};
string sa[4] = {"pooh", "piglet", "Rodge", "test" };
const int *pi = find( ia, ia+8, ia[3] );
const double *pd = find( da, da+6, da[3] );
const string *ps = find( sa, sa+4, sa[2] );
pi = find (ia, 8, ia[3]);
cout <<*pi<<endl<<*pd<<endl<<*ps<<endl;
if( ! test.empty() ){
cout <<*( find( begin( test ), end( test ), 523 ) ) << endl;
}
///cout << *( find( begin( testnull), end( testnull), 523 ) ) << endl;
if( find( begin( testnull), end( testnull), 523 ) )
cout << "find" <<endl;
else cout << " Not find " <<endl;
return 0;
}