From 1ec336e76ddf5f427c84c66ad8133f2b6cb6ea39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 27 Jan 2015 13:20:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0advance=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E5=8F=8C=E5=90=91=E8=BF=AD=E4=BB=A3=E5=99=A8=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 10 +++++++++- TinySTL/Test/AlgorithmTest.cpp | 6 +++++- TinySTL/Test/AlgorithmTest.h | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index 567eea5..c5610cc 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -446,11 +446,19 @@ namespace TinySTL{ namespace { template void _advance(InputIterator& it, Distance n, input_iterator_tag){ + assert(n >= 0); + while (n--){ + ++it; + } + } + template + void _advance(BidirectionIterator& it, Distance n, bidirectional_iterator_tag){ if (n < 0){ while (n++){ --it; } - }else{ + } + else{ while (n--){ ++it; } diff --git a/TinySTL/Test/AlgorithmTest.cpp b/TinySTL/Test/AlgorithmTest.cpp index 9bdd899..79b6afd 100644 --- a/TinySTL/Test/AlgorithmTest.cpp +++ b/TinySTL/Test/AlgorithmTest.cpp @@ -170,16 +170,20 @@ namespace TinySTL{ void testAdvance(){ TinySTL::vector v; TinySTL::list l; + TinySTL::binary_search_tree bst; for (auto i = 0; i != 10; ++i){ v.push_back(i); l.push_back(i); + bst.insert(i); } auto vit = v.begin(); auto lit = l.begin(); + auto bit = bst.cbegin(); TinySTL::advance(vit, 5); TinySTL::advance(lit, 5); - assert(*vit == 5 && *lit == 5); + TinySTL::advance(bit, 5); + assert(*vit == 5 && *lit == 5 && *bit == 5); TinySTL::advance(vit, -5); TinySTL::advance(lit, -5); diff --git a/TinySTL/Test/AlgorithmTest.h b/TinySTL/Test/AlgorithmTest.h index 775acd0..33c13a7 100644 --- a/TinySTL/Test/AlgorithmTest.h +++ b/TinySTL/Test/AlgorithmTest.h @@ -13,6 +13,7 @@ #include #include +#include "../BinarySearchTree.h" #include "../List.h" #include "../Vector.h"