国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

對std::vector/list的iterator簡單包裝嘗試

2019-11-11 03:56:48
字體:
來源:轉載
供稿:網友

記錄一個對iterator的定義嘗試:

1. 使用typename定義直接推導最簡單

2. 使用iterator的基礎類型比較麻煩(使用allocator、std::iterator基礎定義),需要再深入看下stl源碼解析。

#include <vector>#include <list>#include <iostream>template <typename _Tp>class PackIntsVector{public:    // 賦值走copy構造     PackIntsVector(const std::vector<_Tp>& vec)    {        m_vecInts = vec;    };        // vector::iterator實現     // 方法一:遍歷類型指定: std::forward_iterator_tag,iterator的原始結構體類型:不支持++, --, !=等操作     // typedef std::iterator<std::forward_iterator_tag, _Tp, __int64, _Tp*, _Tp&> iterator;    // 方法二:分配類型指定:std::allocator<_Tp>, 并包裝iterator, 提供++,--,!=等方法     // 指定分配類型:std::allocator<_Tp>    // typedef typename __gnu_cxx::__alloc_traits< std::allocator<_Tp> >::template rebind<_Tp>::other _Tp_alloc_type;    typedef typename std::allocator<_Tp> _Tp_alloc_type;    // 獲取元素指針類型     // typedef typename __gnu_cxx::__alloc_traits<_Tp_alloc_type>::pointer pointer;    typedef typename _Tp_alloc_type::pointer pointer;    // 初始化iterator,提供了*, ->, !=, +=, -= 等方法     typedef __gnu_cxx::__normal_iterator<pointer, std::vector<_Tp> > iterator;        // 方法三:等效于下面這句話 (注意,必須有typename關鍵字說明,用于告訴編譯器推導)     // typedef typename std::vector<_Tp>::iterator iterator;        iterator begin()    {        return m_vecInts.begin();    };            iterator end()    {        return m_vecInts.end();    };        // list::iterator實現    // 方法一:     typedef typename std::_List_iterator<_Tp> iteratorLst;        // 方法二:     //typedef typename std::list<_Tp>::iterator iteratorLst;        iteratorLst beginLst()    {        return m_lstInts.begin();    };    iteratorLst endLst()    {        return m_lstInts.begin();    };PRivate:    std::vector<_Tp> m_vecInts;    std::list<_Tp> m_lstInts;};int main(){    std::vector<int> vecInts;    for (int i=0; i<10; ++i)    {        vecInts.push_back(i);    }        PackIntsVector<int> pack = vecInts;    for (PackIntsVector<int>::iterator iter = pack.begin(); iter != pack.end(); ++iter)    {        std::cout << *iter << std::endl;    }    for (PackIntsVector<int>::iteratorLst iter = pack.beginLst(); iter != pack.endLst(); ++iter)    {        std::cout << *iter << std::endl;    }    return 1;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太湖县| 拉萨市| 台北市| 吴堡县| 濉溪县| 泗洪县| 瑞丽市| 新化县| 西青区| 邯郸县| 旅游| 阿拉善右旗| 万荣县| 隆尧县| 五台县| 宁阳县| 磐安县| 新邵县| 老河口市| 通城县| 新闻| 胶南市| 都兰县| 晋中市| 霍山县| 合江县| 体育| 朝阳区| 上饶县| 满洲里市| 宁夏| 托克托县| 博兴县| 蕉岭县| 桂东县| 河津市| 通化县| 慈利县| 黄平县| 曲阳县| 石家庄市|