一直以为STL的实现中,size()操作都是常数时间,今天却遇到了比较郁闷的事.
在VC7中,list的size()实现如下: size_type size() const { // return length of sequence return (_Mysize); }
VC7的实现中使用变量_Mysize来存储list的大小,在调用size()的时候直接返回变量_Mysize,调用时间是常数的.
而GCC中的实现:
/** Returns the number of elements in the %list. */
size_type size() const { return std::distance(begin(), end()); }
每次调用都需要遍历LIST,是线性时间..真让人郁闷.
以后在unix下需要注意了!