c++ 为自定义类添加stl遍历器风格的遍历方式

为仿照stl的遍历风格,实现对自定义类型的遍历。

1. 需要遍历的基础结构:

 struct ConnectionPtr
{
int id_;
int port_;
string addr_; //std::set 需要排序,需要重载<
bool operator <(const ConnectionPtr &ptr)const {
return id_ < ptr.id_;
} void printPtr() //显示函数
{
cout << "ConnectionPtr :" << endl;
cout << "id = " << id_ << " port = " << port_ << " addr_ = " << addr_ << endl;
cout << endl;
}
};

2. 需要实现统一风格遍历的自定义结构:

 struct Region_connection
{
map<int, set<ConnectionPtr>> connections; //自定义结构

3. 遍历器的结构:

 class RegionIterator
{
public:
int key;
ConnectionPtr* ptr;
map<int, set<ConnectionPtr>>* pconnections; RegionIterator(){
clear();
} //赋值
RegionIterator& operator = (const RegionIterator &iter)
{
key = iter.key;
ptr = iter.ptr;
}
//不等于
bool operator != (const RegionIterator &iter)
{
return (key != iter.key) || (ptr != iter.ptr);
}
//等于
bool operator == (const RegionIterator &iter)
{
return (key == iter.key) && (ptr == iter.ptr);
}
//前缀自加
RegionIterator& operator ++ ()
{
RegionIterator itor = next(key, ptr);
this->key = itor.key;
this->ptr = itor.ptr;
return *this;
}
//后缀自加
RegionIterator operator ++ (int)
{
RegionIterator tmp = *this;
RegionIterator itor = next(key, ptr);
this->key = itor.key;
this->ptr = itor.ptr;
return tmp;
}
//取值
ConnectionPtr& operator * ()
{
return *ptr;
} private:
void clear()
{
key = -;
ptr = nullptr;
pconnections = nullptr;
} RegionIterator next(int key_tmp, ConnectionPtr* ptr_tmp)
{
assert(pconnections);
RegionIterator region;
auto iter = pconnections->find(key_tmp);
if (iter != pconnections->end()){
const set<ConnectionPtr>& sets = iter->second;
if (sets.size()){
if (ptr_tmp){
auto itr = sets.find(*ptr_tmp);
if (itr != sets.end()){
if (++itr != sets.end()){
region.key = key_tmp;
region.ptr = (ConnectionPtr*)&*(itr);
return region;
}else{
if (++iter != pconnections->end()){
key_tmp = iter->first;
return next(key_tmp, nullptr);
}else{
return region;
}
}
}
else{
return region;
}
}else{
region.key = key_tmp;
region.ptr = (ConnectionPtr*)&(*sets.begin());
return region;
} }else{
assert(ptr_tmp == nullptr);
key_tmp = (++iter)->first;
return next(key_tmp, ptr_tmp);
}
}
else{
return region;
}
}
};

4. 为实现要求,需要在自定义结构添加部分函数:

 struct Region_connection
{
map<int, set<ConnectionPtr>> connections; typedef RegionIterator iterator;
iterator begin(){
iterator itor;
itor.pconnections = &connections;
if (connections.size() > )
{
for (auto itr = connections.begin(); itr != connections.end(); itr++)
{
const set<ConnectionPtr>& sets = itr->second;
if (sets.size() > )
{
auto itr2 = sets.begin();
ConnectionPtr* p = (ConnectionPtr*)&(*itr2);
itor.key = itr->first;
itor.ptr = p;
return itor;
}
}
}
return iterator();
} iterator end(){
return iterator();
} ConnectionPtr& operator[](const RegionIterator& itor){
auto connect = connections.find(itor.key);
assert(connect != connections.end());
const set<ConnectionPtr>& sets = connect->second;
auto ptr = sets.find(*itor.ptr);
assert(ptr != sets.end());
return (ConnectionPtr&)*ptr;
} int size(){
int size = ;
for (auto itor : connections)
{
size += itor.second.size();
}
return size;
} };

5. 测试代码:

 #include "stdafx.h"

 #include <iostream>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include "mylterater.h"
#include "regiontest.h"
#include <time.h> using namespace std; #define random(x,y) (((double)rand()/RAND_MAX)*(y-x)+x) int _tmain(int argc, _TCHAR* argv[])
{ srand((int)time());
//构造Region_connection
Region_connection region;
int n = ;
for (int i = ; i < random(,); i++)
{
set<ConnectionPtr> sets;
int num = random(, );
for (int j = ; j < num; j++, n++)
{
ConnectionPtr ptr;
ptr.id_ = n;
ptr.port_ = + n;
sets.insert(ptr);
}
region.connections.insert(std::make_pair(i, sets));
} //遍历打印
clock_t starttim, endtim;
starttim = clock();
for (auto iter = region.begin(); iter != region.end(); iter++)
{
ConnectionPtr& ptr = region[iter];//*iter;
ptr.printPtr();
}
endtim = clock();
cout << "Total time : " << endtim - starttim << " ms" << endl; getchar(); return ;
}
上一篇:如何管理安卓android手机下google(谷歌)的通讯录联系人账户


下一篇:Objective C 快速入门学习一