`
tcspecial
  • 浏览: 895876 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

STL ++iter与iter++区别

阅读更多

 

 之前编码一直用的是iter++,同事说该方式效率比较低。带着疑惑看STL源码:

 

// vector

_Myiter& operator++()
	{	// preincrement
	++*(_Mybase *)this;
	return (*this);
	}

_Myiter operator++(int)
	{	// postincrement
	_Myiter _Tmp = *this;
	++*this;
	return (_Tmp);
	}

 

果不其然,++iter 前向表达式返回引用,而iter++返回的却是一临时对象。

原来前向后向重载表达式的差异,只是多了一个int参数。

 

// Test
class Test
{
public:
        Test():val(0){}
        ~Test(){}

        Test &operator++()
        {
                ++val;
                printf("Pre incr val:%d\n", val);
                return (*this);
        }

        Test operator++(int)
        {
                Test pTmp = *this;              // save
                val++;
                printf("Post incr val:%d\n", val);
                return pTmp;
        }

private:
        int val;
};

// main
int main()
{
        Test test;
        test++;
        ++test;
        return 0;
}

 

输出:

Post incr val:1

Pre incr val:2

 

 

 

 

分享到:
评论

相关推荐

    iter:Go实现C ++ STL迭代器和算法

    重复去实现C ++ STL迭代器和算法。 更少的手写循环,更具表现力的代码。 README翻译:动机尽管Go没有泛型,但我们值得拥有可重用的通用算法。 iter可通过多种方式帮助改进Go代码: 一些简单的循环不太可能是错误的或...

    STL源码剖析.pdg

    第1章 stl 概论与版本简介001 1.1 stl 概论 001 1.1.1 stl的历史 003 1.1.2 stl与c++ 标准程序库 003 . 1.2 stl 六大组件 - 功能与运用 004 1.3 gnu源码开放精神 007 1.4 hp stl实现版本 009 1.5 p.j. ...

    SGI STL deque相关代码

    SGI STL deque相关代码

    C++ STL开发技术导引(第5章)

    22.4 迭代器交换iter_swap 307 22.5 区间元素交换swap_ranges 308 22.6 元素变换transform 309 22.7 替换 310 22.8 条件替换replace_if 311 22.9 替换和复制replace_copy 312 22.10 条件替换和复制...

    STL 源码剖析(侯捷先生译著)

    这本书也不适合带领你学习面向对象(Object Oriented)技术 — 是的,STL 与面向对象没有太多关连。本书前言清楚说明了书籍的定位和合适的读者,以及各类基础读物。如果你的Generic Programming/STL实力足以阅读本书...

    C++ STL 开发技术导引(第6章)

    22.4 迭代器交换iter_swap 307 22.5 区间元素交换swap_ranges 308 22.6 元素变换transform 309 22.7 替换 310 22.8 条件替换replace_if 311 22.9 替换和复制replace_copy 312 22.10 条件替换和复制...

    C++标准程序库STL的架构

    7.3.3 iter_swap()交换两个迭代器所指内容 68 7.4 迭代器配接器(adapter) 69 7.4.1 逆向迭代器 69 7.4.2 Insert迭代器 72 7.4.3 Stream迭代器 75 7.5 迭代器特性 76 8 STL仿函数 77 8.1 仿函数概念 77 8.1.1 仿函数...

    C++标准库使用范例

    * 功能与使用方法 ***************************/ #include <vector> #include <algorithm> #include <functional> #include <iostream> #include <ostream> using namespace std; bool mod_equal ( int elem1...

    SGI-STL 源码以及 word 注解版

    #include #ifndef _RWSTD_NO_NAMESPACE namespace std { #endif // // Forward declare raw_storage_iterator // template , class T> class raw_storage_iterator;...// Non-modifying sequence operations. ...

    C++ STL开发技术导引(第3章)

    22.4 迭代器交换iter_swap 307 22.5 区间元素交换swap_ranges 308 22.6 元素变换transform 309 22.7 替换 310 22.8 条件替换replace_if 311 22.9 替换和复制replace_copy 312 22.10 条件替换和复制...

    STL常用函数

    //定义了一个叫iter的vector用迭代器变量 //加入新元素 words.push_back(s); //获取vector大小 words.size(); //遍历vector for(int i=0;i<word.size();i++) //注意,它和数组一样,下标从0开始 //用sort对vector...

    C++ STL入门教程(6) set(集合)的使用方法

    一、简介 集合(Set)是一种包含已排序对象的关联... set<int>::iterator iter; cout << num.max_size() << endl;///set容纳上限 cout << endl; ///2. 添加元素 for (int i = 0; i < 10; i++)

    unix / linux c++ example

    algo examples.tgz i18n iter Makefile.h memory README string cont fo io Makefile MAKE.LOG num stl util

    C++ STL入门教程(7) multimap、multiset的使用

    一、multimap(一对多索引) C++ multimap和map所支持的操作相同(除了multimap不支持下标运算),但是multimap允许重复的元素。 完整程序代码: /*请务必运行以下程序后对照阅读*/ ... multimap<int>::iterator iter,

    《C++编程艺术》教程+代码

    2.7 Iter 34 2.8 如何使用GCPtr 36 2.8.1 处理分配异常 37 2.8.2 一个更有趣的示例 38 2.8.3 对象的分配和丢弃 40 2.8.4 分配数组 41 2.8.5 使用具有类类型的GCPtr 43 2.8.6 一个比较大的演示程序 45 2.8.7 加载测试...

    C++大学教程,一本适合初学者的入门教材(part2)

    20.5.7 swap、iter_swap和swap_ranges 20.5.8 copy—backward、 merge、 unique和reverse 20.5.9 inplace_merge、 unique—copy和reverse—copy 20.5.10 集合操作 20.5.11 1ower—bound、 upper—bound和...

    C++大学教程,一本适合初学者的入门教材(part1)

    20.5.7 swap、iter_swap和swap_ranges 20.5.8 copy—backward、 merge、 unique和reverse 20.5.9 inplace_merge、 unique—copy和reverse—copy 20.5.10 集合操作 20.5.11 1ower—bound、 upper—bound和...

    -C++参考大全(第四版) (2010 年度畅销榜

    23.11 C与C++的区别 第24章 标准模板库 24.1 STL概述 24.2 容器类 24.3 一般的操作原理 24.4 vector容器 24.5 list容器 24.6 map容器 24.7 算法 24.8 使用函数对象 24.9 string类 24.10 关于STL的最后一点说明 第三...

    C++标准库(第二版)英文版.pdf

    The C++ Standard Library A Tutorial and Reference (2nd Edition)+cppstdlib-code.zip C++标准库(第二版)英文版.pdf 非扫描版+源代码 Prefaceto the SecondEdition xxiii Acknowledgments for the Second...

Global site tag (gtag.js) - Google Analytics