标准模板库简介(一)(翻译)

Cppasm 2004-01-18 10:36:25
加精
这篇文章翻译自SGI的网站,原文名为Introduction to Stand Template Library,链接为http://www.sgi.com/tech/stl/.我看后感觉对于理解一些相关概念和学习STL很有帮助就把它翻译过来了,是中英文对照的,因为我也不敢保证完全能表达原文意思,只是按自己的理解翻译出来了,希望对大家能有点帮助.
标准模板库简介
标准模板库,或者STL,是一个包括容器类(container classes),算法(algorithms),和迭代器(iterator)的C++类库.它提供了许多计算机科学中的基本算法和数据结构.STL是一个泛型库,也就是说它的组件都是大量参数化了的(heavily parameterized):STL中的几乎每个组件都是一个模板.在开始使用STL之前,你应当确信你明白C++中的模板是如何运作的.
容器和算法
和许多类库一样,STL包括了容器类:它的目的是容纳其它对象.STL包含了vector,list,deque,set,multiset,map,multimap,hash_set,hash_multiset,hash_map和hash_multimap.这些类中的每一个都是一个模板.可以被实例化来容纳任何类型的对象.例如,你可以以与使用一个普通的C数组极其相似的方式来使用vector<int>,,除了vector消除了手工管理动态内存分配之外.
vector<int> v(3); //声明一个有三个元素的vector
v[0] = 7;
v[1] = v[0]+3;
v[2] = v[0]+v[1]; //v[0]=7,v[1]=10,v[2]=17
STL也包含了大量的算法来操纵存储在容器中数据.例如,通过使用reverse算法,你可以反转一个vector中的元素顺序.
reverse(v.begin(),v.end()); //v[0]=17,v[1]=10,v[2]=7
对于reverse的这个调用有两点值得注意.首先,它是一个全局函数,而不是一个成员函数.第二,它接受两个参数而不是一个:它操作一系列元素,而不是一个容器.在这个例子中元素范围刚好是整个容器v.
导致这两个事实的原因都是一样的:reverse,像其它STL算法一样,是与STL容器类分离的,也就是说,reverse不仅可以用来反转vector中的元素,而且可以反转list甚至C数组中的元素.下面的程序也是有效的:
double A[8] = {1.2,1.3,1.4,1.5,1.6,1.7};
reverse(A,A+6);
for(int I = 0;i < 6;i++)
cout << “A[“ << I << “]=” << A[i];
这个例子使用了一个范围(range),就如反转一个vector的例子一样;reverse的第一个参数是一个指向该范围的开始指针,第二个参数指向该范围最后一个元素之后的位置.这个范围以[A,A+6]来标记;这个非对称的标记是用来提醒那两个端点是不同的,第一个是该范围的开始,第二个则是范围末尾之后的位置(也就是最后一个元素后面那个位置).
迭代器(Iterators)
在反转一个C数组的例子中,reverse的参数类型明显的是double*.但是如果反转一个vector或者一个list的话reverse的参数又是什么呢?即reverse所声明的参数究竟是什么,以及v.begin()和v.end()究竟返回的是什么?
The answer is that the arguments to reverse are iterators, which are a generalization of pointers. Pointers themselves are iterators, which is why it is possible to reverse the elements of a C array. Similarly, vector declares the nested types iterator and const_iterator. In the example above, the type returned by v.begin() and v.end() is vector<int>::iterator. There are also some iterators, such as istream_iterator and ostream_iterator, that aren't associated with containers at all.
答案就是reverse的参数为iterators,它是指针的一个泛化(generalization).指针自身就是是迭代器(iterator),所以才有可能反转一个C数组的元素.相似的,vector声明了内嵌类型(nested type)iterator和const_iterator.在上面的例子中,v.begin()和v.end()所返回的类型是vector<int>::iterator.还有一些迭代器(iterators),例如istream_iterator和ostream_iterator与容器根本就没有关联.
Iterators are the mechanism that makes it possible to decouple algorithms from containers: algorithms are templates, and are parameterized by the type of iterator, so they are not restricted to a single type of container. Consider, for example, how to write an algorithm that performs linear search through a range. This is the STL's find algorithm.
template <class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value) {
while (first != last && *first != value) ++first;
return first;
}
迭代器(iterators)是一种使得将算法与容器进行分离成为可能的机制:算法是模板,用迭代器的类型进行参数化,所以它们并不限于单一类型的容器.例如,考虑怎样编写一个在一个范围内执行线性搜索的算法.这就是STL的find算法.
Template<class InputIterator, class T>
InputIterator find(InputIterator first, InputIterator last, const T& value) {
While(first != last && *fist != value) ++first;
Return first;
}
Find takes three arguments: two iterators that define a range, and a value to search for in that range. It examines each iterator in the range [first, last), proceeding from the beginning to the end, and stops either when it finds an iterator that points to value or when it reaches the end of the range.
Find接受三个参数:两个规定了一个范围的迭代器和在该范围内要搜寻的一个值value.它在[first,last]范围内检查每一个迭代器,从开始到最后,当它找到一个指向value的迭代器或者达到末尾时就停止了.
First and last are declared to be of type InputIterator, and InputIterator is a template parameter. That is, there isn't actually any type called InputIterator: when you call find, the compiler substitutes the actual type of the arguments for the formal type parameters InputIterator and T. If the first two arguments to find are of type int* and the third is of type int, then it is as if you had called the following function.
int* find(int* first, int* last, const int& value) {
while (first != last && *first != value) ++first;
return first;
}
first和last被声明为类型InputIterator,它是一个模板参数.即,事实上没有任何一个类型叫做InputIterator:当你调用find时,编译器用实际的参数类型来替换形式类型参数InputIterator和T.如果find的头两个参数类型为int*而第三个参数类型为int的话,那么就像你调用了下列函数一样:
int* find(int* first, int* last, const int& value) {
while (first != last && *first != value) ++first;
return first;
}
...全文
437 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
心雨楼 2004-03-25
  • 打赏
  • 举报
回复
good
binariman 2004-03-21
  • 打赏
  • 举报
回复
辛苦了,不过我认为作为从事计算机的一员看英文文档是必须的
Darkay_Lee 2004-03-17
  • 打赏
  • 举报
回复
我看英文的文章都已经很类了,楼主,辛苦啦!
wuangle 2004-02-01
  • 打赏
  • 举报
回复
double A[8] = {1.2,1.3,1.4,1.5,1.6,1.7};
reverse(A,A+6);
for(int I = 0;i < 6;i++)
cout << “A[“ << I << “]=” << A[i];
这个例子使用了一个范围(range),就如反转一个vector的例子一样;reverse的第一个参数是一个指向该范围的开始指针,第二个参数指向该范围最后一个元素之后的位置.这个范围以[A,A+6]来标记;这个非对称的标记是用来提醒那两个端点是不同的,第一个是该范围的开始,第二个则是范围末尾之后的位置(也就是最后一个元素后面那个位置).


里面的是[A,A+6),而不是[A,A+6]
marshero 2004-01-20
  • 打赏
  • 举报
回复
谢谢
Wolf0403 2004-01-20
  • 打赏
  • 举报
回复
今天翻译了 cplusplus.com 的 faq 才知道翻译有多累。。。向楼主致敬。

Find接受三个参数:两个规定了一个范围的迭代器和在该范围内要搜寻的一个值value.它在[first,last]范围内检查每一个迭代器,从开始到最后,当它找到一个指向value的迭代器或者达到末尾时就停止了.

这里 [first, last] 是笔误吧?应该是 [first, last)
另外:…… 从开始到最后,当它找到一个指向value的迭代器或者达到末尾时就停止了.
不妨换个句法:从开始到最后,直到它找到一个指向 value 的迭代器或者达到容器末尾。

^_^

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧