注:博客内容均来自于对《C++标准库》侯捷,华中科技大学出版社一书的笔记。转载请注明出处。
所有例程在Red Hat Linux 3.2.2-5版本上编译运行,g++的版本是 g++ (GCC) 3.2.2 20030222。
#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版本标准库函数。
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; } #endifcarray.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; }
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
#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。