注:博客内容均来自于对《C++标准库》侯捷,华中科技大学出版社一书的笔记。转载请注明出处。
所有例程在Red Hat Linux 3.2.2-5版本上编译运行,g++的版本是 g++ (GCC) 3.2.2 20030222。
1、string作为容器使用
string是C++标准程序库的一种型别,是用 “侵入性作法编写STL容器”的一个例子。string可被视为以字符为元素的一种容器。字符可以构成序列,可以在序列上来回移动遍历。因此,标准的string提供STL容器接口。string也提供成员函数begin()和end(), 返回随机存取迭代器,可用来遍历整个string。同时为了迭代器和迭代配接器,string也提供一些操作函数:push_back()。string支持的迭代器操作如下表所示:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
int main(int argc, char *argv[])
{
string s("The zip code of Hondelage in Germany id 38108");
cout << "original: " << s << endl;
transform(s.begin(), s.end(), //源字串
s.begin(), //目标位置
::tolower); //仿函数
cout << "lowered: " << s << endl;
transform(s.begin(), s.end(), //源字串
s.begin(), //目标位置
::toupper); //仿函数
cout << "uppered: " << s << endl;
return 0;
}
运行结果:
上述程序中,仿函数带入的时候使用的是: ::tolower() ,这是因为这里支持此操作的是在全局区的C版本标准库函数。
2、Array作为容器使用
print.h
#ifndef __PRINT_H
#define __PRINT_H
#include <iostream>
#include <iterator>
template <class T>
void PRINT_ELEMENTS(const T& coll, const char* optcstr = " ")
{
typename T::const_iterator pos;
std::cout << optcstr;
for(pos = coll.begin(); pos != coll.end(); ++pos)
{
std::cout << *pos << ' ';
}
std::cout << endl;
}
#endif
carray.h
#ifndef __CARRAY_H
#define __CARRAY_H
#include <cstddef>
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 begin() 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;
}
T* as_arry()
{
return v;
}
};
array.cpp
#include <iostream>
#include <functional>
#include <algorithm>
#include "carray.h"
#include "print.h"
using namespace std;
int main(int argc, char *argv[])
{
carray<int,10> a;
for(unsigned i = 0; i < a.size(); ++i)
{
a[i] = i + 1;
}
PRINT_ELEMENTS(a);
reverse(a.begin(), a.end(),
a.begin(),
negate<int>());
PRINT_ELEMENTS(a);
return 0;
}
3、实现Reference语义
countptr.h
#ifndef __COUNTPTR_H
#define __COUNTPTR_H
template <class T>
class CountedPtr
{
private:
T* ptr;
long* count;
public:
explicit CountedPtr(T* p=0)
: ptr (p) , count (new long(1))
{
}
CountedPtr (const CountedPtr<T>& p) throw()
: ptr(p.ptr), count(p.count)
{
++*count;
}
~CountedPtr () throw()
{
dispose();
}
CountedPtr<T>& operator= (const CountedPtr<T>& p) throw()
{
if(this != &p)
{
dispose();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
T& operator*() const throw()
{
return *ptr;
}
T* operator->() const throw()
{
return ptr;
}
private:
void dispose()
{
if(--*count == 0)
{
delete count;
delete ptr;
}
}
};
#endif //__COUNTPTR_H
refsem.cpp
#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include "countptr.h"
using namespace std;
void printCountedPtr(CountedPtr<int> elem)
{
cout<< *elem << ' ';
}
int main(int argc, char *argv[])
{
static int values[] = {3, 5, 9, 1, 6, 4};
typedef CountedPtr<int> IntPtr;
deque<IntPtr> col1;
list<IntPtr> col2;
for(int i=0; i<sizeof(values)/sizeof(values[0]); ++i)
{
IntPtr ptr(new int(values[i]));
col1.push_back(ptr);
col2.push_front(ptr);
}
for_each(col1.begin(), col1.end(),
printCountedPtr);
cout << endl;
for_each(col2.begin(), col2.end(),
printCountedPtr);
cout << endl << endl;
*col1[2] *= *col1[2];
(**col1.begin()) *= -1;
(**col2.begin()) *= 0;
for_each(col1.begin(), col1.end(),
printCountedPtr);
cout << endl;
for_each(col2.begin(), col2.end(),
printCountedPtr);
cout << endl;
return 0;
}
注:博客内容均来自于对《C++标准库》侯捷,华中科技大学出版社一书的笔记。转载请注明出处。
所有例程在Red Hat Linux 3.2.2-5版本上编译运行,g++的版本是 g++ (GCC) 3.2.2 20030222。