2007년 3월 9일 금요일

After end iterator

enditr.cpp
#include <list>
#include <vector>
#include <deque>
#include <map>
#include <set>
#include <iostream>
#include <ctime>
using namespace std;

template<typename _T>
void
testSeqItr(void)
{
 _T some;
 typename _T::const_iterator ib;
 typename _T::const_iterator ie;
 typename _T::const_iterator it;

 for ( int i = 0; i < 1024; i++ )
 {
        some.push_back(rand());
 }

 ib = some.begin();
 ie = some.end();
 it = ie;
 ++it;

 cerr << __PRETTY_FUNCTION__ << endl;
 cerr << "begin equal? " << (ib==it) << endl;
 cerr << "end equal? " << (ie==it) << endl;
}

template<typename _T>
void
testAssItr(void)
{
 _T some;
 typename _T::const_iterator ib;
 typename _T::const_iterator ie;
 typename _T::const_iterator it;

 for ( int i = 0; i < 1024; i++ )
 {
        some.insert(make_pair(rand(),rand()));
 }

 ib = some.begin();
 ie = some.end();
 it = ie;
 ++it;

 cerr << __PRETTY_FUNCTION__ << endl;
 cerr << "begin equal? " << (ib==it) << endl;
 cerr << "end equal? " << (ie==it) << endl;
}

int
main(int,char**)
{
 srand((unsigned int)time(NULL));

 testSeqItr< basic_string<int> >();
 testSeqItr< list<int> >();
 testSeqItr< deque<int> >();
 testSeqItr< vector<int> >();

 testAssItr< map<int,int> >();
 testAssItr< multimap<int,int> >();

 return 0;
}
$ make enditr
g++ enditr.cpp -o enditr
./enditr
void testSeqItr() [with _T = std::basic_string<int, std::char_traits<int>, std::allocator<int> >]
begin equal? 0
end equal? 0
void testSeqItr() [with _T = std::list<int, std::allocator<int> >]
begin equal? 1
end equal? 0
void testSeqItr() [with _T = std::deque<int, std::allocator<int> >]
begin equal? 0
end equal? 0
void testSeqItr() [with _T = std::vector<int, std::allocator<int> >]
begin equal? 0
end equal? 0
void testAssItr() [with _T = std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]
begin equal? 0
end equal? 0
void testAssItr() [with _T = std::multimap<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >]
begin equal? 0
end equal? 0
/usr/include/c++/*/bits/stl_*.h를 헤집어보면 속도를 위해 바운더리 체크 아무것도 안 하는 것을 알 수 있다. orz OTL
뭐, 리스트(더블링크드리스트)는 꼬리에 처음을 머리로 해놨겠지. 암튼, 반복자 잘 관리해야할 듯.