• C++11之for 新解(2)


    前记

         C++提供了for range功能,如果自定义的类也想使用for range需要满足什么条件呢,通过观察和查阅资料,推理编译器编译后的结果和正常for循环的结果应给是一样的,正常for循环需要满足四个条件。

        for(auto it = vec_int.begin(); it!= vec_int.end(); ++it) {
            cout << *it << "	";
        }
    

    测试后for range也需要满足这四个条件:

         1、实现begin()
         2 、实现end()
         3 、实现 operator++()
         4 、实现 operator!=(class& other)
         
    例子
    #include <iostream>
    #include <assert.h>
    
    using namespace std;
    
    class VecInt;
    class Iter{
    public:
        Iter(VecInt* pVectInt, int pos) :   pVecInt_(pVectInt),
                                            pos_(pos)
        {
    
        }
    
        const Iter& operator++() {
            ++pos_;
            return *this;
        }
    
        bool operator!=(Iter other) {
            return other.pos_ != pos_;
        }
    
        int operator*();
    private:
        VecInt* pVecInt_;
        int pos_;
    };
    
    class VecInt{
    public:
        VecInt() {
            index_ = 0;
            max_ = 1;
            data_ = new int[max_]();
        }
        ~VecInt() {
            if(max_ > 0) {
                delete []data_;
                max_ = 0;
                index_ = 0;
            }
        }
        int get(size_t pos) {
            assert(pos >= 0 && pos <= index_);
            return data_[pos];
        }
    
        void realloc() {
            max_ = max_ * 2;
            int* temp = new int[max_]();
            for(size_t i = 0; i < index_; ++i) {
                temp[i] = data_[i];
            }
            delete []data_;
            data_ = temp;
        }
        Iter begin() {
            return Iter(this, 0);
        }
    
        Iter end() {
            return Iter(this, index_);
        }
    
        void push_back(int value) {
            if(index_ >= max_) {
                realloc();
            }
            data_[index_] = value;
            index_ = index_ + 1;
        }
    private:
        size_t max_;            // the int array length
        size_t index_;          // current array index
        int* data_;             // the int array pointer
    };
    
    // use the VecInt member function so define after the
    // VecInt define the function
    int Iter::operator*() {
        return pVecInt_->get(pos_);
    }
    int main() {
        VecInt vec_int;
        for(auto i = 1; i < 10; ++i) {
            vec_int.push_back(i);
        }
    
        for(auto it : vec_int) {
            cout << it << "	";
        }
        cout << endl;
    }
    程序很简单,实现了一个动态数组类, 和数组迭代器,在稍微比较难的地方,做了简单的注释,enjoy the code。
    后记
         在学习过程中总是感觉一开始都懂了,但过了一段时间后还是忘记了。或者给别人描述的时候说不清楚。为啥是这样,我想有这么几个原因:
         1、从小受到填鸭式的教育,重记忆,所以每次只是记住了,并没理解
         2、学习后,过了很长时间忘记了
         3、记忆力不行
         如何解决这种学了就忘了得问题呢?有人提出三个境界,what,how, why。如果想真正的掌握这些知识,需要付出更多的精力去了解这东西为啥这样用,发明这些知识的作者当时是如何想得,自己通过已知的知识,是否能推导出相应的一种模式。通过这种方式的理解,相信学习知识的效率更高。
  • 相关阅读:
    Samba服务器配置
    Showdoc
    wkhtmltoimage(网页剪切功能)
    GTID数据库备份
    awstats日志分析
    docker桥接
    php的opcache缓存扩展(php页面代码刷新速度)
    Pureftp SSL/TLS配置
    ssh-keygen配置
    systemctl使用
  • 原文地址:https://www.cnblogs.com/fengju/p/6174297.html
Copyright © 2020-2023  润新知