读书笔记之:C++标准程序库(2)
第5章 STL标准模板库
5.1 STL中的各个组件
STL的基本观念就是将数据和操作分离。而这种将数据和算法分开对待的考虑和面向对象的思想是矛盾的。
5.2 容器
序列容器Sequence containers和关联式容器Assocative containers
严格来说,C++标准并未定义某一种容器的具体实现,然而标准却规定了对其行为和复杂度的要求,这让库的作者没有太多变化的余地。所以实际上各个实际版本之间只是在细节上有所差异。
STL中的插入迭代器
测试三种迭代器的代码:
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <iterator>
using namespace std;
int main(){
list<int> coll1;
for(int i=1;i<=9;i++)
coll1.push_back(rand()%100);
copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));
cout<<endl;
vector<int> coll2;
copy(coll1.begin(),coll1.end(),
back_inserter(coll2));
copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
deque<int> coll3;
copy(coll1.begin(),coll1.end(),
front_inserter(coll3));
copy(coll3.begin(),coll3.end(),ostream_iterator<int>(cout," "));
cout<<endl;
set<int> coll4;
copy(coll1.begin(),coll1.end(),
inserter(coll4,coll4.begin()));
copy(coll4.begin(),coll4.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
流迭代器
下面是一段示例程序:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
vector<string> col1;
copy(istream_iterator<string>(cin),
istream_iterator<string>(),
back_inserter(col1));
sort(col1.begin(),col1.end());
unique_copy(col1.begin(),col1.end(),
ostream_iterator<string>(cout,"\n"));
}
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
vector<string> col1;
copy(istream_iterator<string>(cin),
istream_iterator<string>(),
back_inserter(col1));
sort(col1.begin(),col1.end());
unique_copy(col1.begin(),col1.end(),
ostream_iterator<string>(cout,"\n"));
}
逆向迭代器
这个在进行反转容器的时候很常用。
5.6.2 更易型算法与关联容器
5.6.3 算法与成员函数的效率
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
// remove all elements with value 3
// - poor performance
coll.erase (remove(coll.begin(),coll.end(),
3),
coll.end());
// remove all elements with value 4
// - good performance
coll.remove (4);
}
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
// remove all elements with value 3
// - poor performance
coll.erase (remove(coll.begin(),coll.end(),
3),
coll.end());
// remove all elements with value 4
// - good performance
coll.remove (4);
}
5.8.2 判别式Predicates
5.9 仿函数Functor,函数对象Function Object
5. 11. 1 STL中的错误处理
STL强调的是效率,所以很少进行错误检查。
5.12 扩展的STL
第6章 STL容器
普通的数组也可以作为STL容器来使用,下面是一种对STL的包装:
#include <cstddef>
#include <algorithm>
#include <functional>
#include <iterator>
#include <iostream>
template <class T,std::size_t thesize>
class carray{
private:
T v[thesize];
public:
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
iterator begin(){
return v;
}
const_iterator bgein()const{
return v;
}
iterator end(){
return v+thesize;
}
const_iterator end()const{
return v+thesize;
}
reference operator[](std::size_t i){
return v[i];
}
const_reference operator[](std::size_t i)const{
return v[i];
}
size_type size()const{
return thesize;
}
size_type max_size()const{
return thesize;
}
T* as_array(){
return v;
}
};
int main(){
using namespace std;
carray<int,10> a;
for(int i=0;i<a.size();++i){
a[i]=i+1;
}
copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
cout<<endl;
reverse(a.begin(),a.end());
copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
#include <algorithm>
#include <functional>
#include <iterator>
#include <iostream>
template <class T,std::size_t thesize>
class carray{
private:
T v[thesize];
public:
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
iterator begin(){
return v;
}
const_iterator bgein()const{
return v;
}
iterator end(){
return v+thesize;
}
const_iterator end()const{
return v+thesize;
}
reference operator[](std::size_t i){
return v[i];
}
const_reference operator[](std::size_t i)const{
return v[i];
}
size_type size()const{
return thesize;
}
size_type max_size()const{
return thesize;
}
T* as_array(){
return v;
}
};
int main(){
using namespace std;
carray<int,10> a;
for(int i=0;i<a.size();++i){
a[i]=i+1;
}
copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
cout<<endl;
reverse(a.begin(),a.end());
copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
6.7.3 hash table 哈希表
第7章 STL迭代器
ostream迭代器的操作
istream迭代器操作
7.5 迭代器特性