SGI STL (7) :: why stl containers have their own iterators?

本文探讨了为何STL容器需要拥有自己的迭代器,并通过设计一个简单的列表迭代器来解释其实现原理。文章展示了如何创建迭代器使得自定义容器能够与标准库函数如std::find配合使用。
Hi guys

My Email: jakezhang1989@hotmail.com
My GitHub Link

Thank you for taking time to go through this post from which you would get what you want.
If you have any problems or opinions that are different form mins, please email me or leave a comments. I will reply as soon as possible.

OK, Let us get started!

The aim of this post is to resolve a problem why stl containers have their own iterators?

Firstly, we need konw what is itertor?
iterator is pretty like pointer whose most important jobs are operator* and operator->

Secondly, Let us design a simple list iterator know where it comes from, assume that list and node’s structure are listed below:

#include <iostream>
template<typename T>
class ListElem
{
    public:
    T value(){ return value_; }
    ListElem* next() const { return next_; }
    private:
    T value_;
    ListElem* next_;
};

template<typename T>
class SampleList
{
    public:
    void insert_front(T value);
    void insert_end(T value);
    void display(std::ostream& os = std::cout) const;
    ListElem<T>* front() const { return front_; }
    // ...
    private:
    ListElem<T>* end_;
    ListElem<T>* front_;
    long size_;
};

A problem we are facing now is how to make SampleList work with std::find() function?
one solution is to to create a pointer-alike hat for it, more specifically to say, an iterator.
when we dereference this iterator, the ListElem object should be returned; When we increment it, it should points out to the next ListEle object. In order to make it work for any type of data more than ListElem, we can treat it as a class template as shown below:

//! the Item represents the elem node in SampleList and only serve for SampleList
// due to its special operator++() method
template <typename Item>
struct ListIter
{
    Item* pointer_; //!< keep a reference to container
    ListIter(Item* pointer = nullptr) : pointer_(pointer) {} //!< default ctor

    // no need to implement copy ctor, default one is working well
    // no need to implement operator==, default one is working well

    Item& operator*() const
    {
        return *pointer_;
    }

    Item* operator->() const
    {
        return pointer_;
    }

    // the following two operator++  are std-conforming 
    // (1) pre-incre operator
    ListIter& operator++()
    {
        pointer_ = pointer_->next(); return *this;
    }
    // (2) post-incre operator
    ListIter operator++(int)
    {
        ListIter tmp = *this;
        ++*this;
        return tmp;
    }

    bool operator== (const ListIter& i) const
    {
        return pointer_ == i->pointer_;
    }
    bool operator!=(const ListIter& i) const
    {
        return pointer_ != i->pointer_;
    }
};

As we konw, std::find() uses *iter != value to find value. In our example, value’s type is int, iter’s type is ListElem, and so we have to provide a glabal operator!=. the reason why not add operator!= in ListIter is that there is no way to fetch the type of int from ListIter. see below:

template <typename value_type>
bool operator!=(const ListElem<value_type>& elem, value_type n)
{
    return elem.value() != n;
}

now, let us run our SampleList collaborating with std::find() with the help of ListIter:

void main()
{
    SampleList<int> myList;
    for (int i = 0; i < 5; ++i)
    {
        myList.insert_front(i);
        myList.insert_end(i + 2);
    }

    myList.display(); // 10 elements (4, 3, 2, 1, 0, 2, 3, 4, 5, 6)

    ListIter<ListElem<int>> begin(myList.front());
    ListIter<ListElem<int>> end; //!< default nullptr
    ListIter<ListElem<int>> iter; //!< default nullptr

    iter = std::find(begin, end, 3);

    if (iter == end)
    {
        std::cout << "not found" << std::endl;
    }
    else {
        std::cout << "found" << "value is " << iter->value() << std::endl;
    }

    // run result: not found
}

Concusion
based on above, in order to create a iterator for our list class, too many implementation details have been exposed: in main() function, begain and end interators exposed ListElem class; in class ListIter, we expose ListElem’s next() method in order to implement operator++. If this is the case, why not let the list designer people handle everything. If so, all the implementation details will be hidden from outside. This is why all stl containers have their own iterators.

内容概要:本文介绍了一个基于Matlab的综合能源系统优化调度仿真资源,重点实现了含光热电站、有机朗肯循环(ORC)和电含光热电站、有机有机朗肯循环、P2G的综合能源优化调度(Matlab代码实现)转气(P2G)技术的冷、热、电多能互补系统的优化调度模型。该模型充分考虑多种能源形式的协同转换与利用,通过Matlab代码构建系统架构、设定约束条件并求解优化目标,旨在提升综合能源系统的运行效率与经济性,同时兼顾灵活性供需不确定性下的储能优化配置问题。文中还提到了相关仿真技术支持,如YALMIP工具包的应用,适用于复杂能源系统的建模与求解。; 适合人群:具备一定Matlab编程基础和能源系统背景知识的科研人员、研究生及工程技术人员,尤其适合从事综合能源系统、可再生能源利用、电力系统优化等方向的研究者。; 使用场景及目标:①研究含光热、ORC和P2G的多能系统协调调度机制;②开展考虑不确定性的储能优化配置与经济调度仿真;③学习Matlab在能源系统优化中的建模与求解方法,复现高水平论文(如EI期刊)中的算法案例。; 阅读建议:建议读者结合文档提供的网盘资源,下载完整代码和案例文件,按照目录顺序逐步学习,重点关注模型构建逻辑、约束设置与求解器调用方式,并通过修改参数进行仿真实验,加深对综合能源系统优化调度的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值