2012-03-21 61 views
2

我正在尝试开发一个类,它将允许我通过迭代器语义有效地访问容器/指针,当它可以正确完成时,以及迭代器不能转换为我想要的指针时将迭代器范围复制到临时缓冲区,然后返回该指针。要做到这一点,我写了下面的程序:专门针对可转换为指针的迭代器的类

#include <cassert> 
#include <vector> 
#include <deque> 
#include <list> 

// General case copies data to temporary vector, in case iterators are from a list or otherwise. 
template < typename Iterator, typename tag = std::iterator_traits <Iterator>::iterator_category > 
class IteratorBuffer 
{ 
    typedef typename std::iterator_traits <Iterator>::value_type T; 

    std::vector <T> temp; 
public: 
    IteratorBuffer(Iterator begin, Iterator end) : temp(std::distance(begin, end)) 
    { 
     std::copy(begin, end, temp.begin()); 
    } 

    const T * data() { return temp.data(); } 
}; 

// Special case should be invoked if Iterator can safely be treated as a pointer to the range. 
template < typename Iterator > 
class IteratorBuffer < Iterator, std::random_access_iterator_tag > 
{ 
    typedef typename std::iterator_traits <Iterator>::value_type T; 

    const T * temp; 
public: 
    IteratorBuffer(Iterator begin, Iterator end) : temp(&*begin) { } 

    const T * data() { return temp; } 
}; 

int main(int argc, char ** argv) 
{ 
    std::vector <int> test1(10); 
    IteratorBuffer < std::vector <int>::iterator > temp1(test1.begin(), test1.end()); 
    // This should be pointing to the data in test1. 
    assert(temp1.data() == test1.data()); 

    std::list <int> test2; 
    for(int i = 0; i < 10; ++i) 
     test2.push_back(i); 
    IteratorBuffer < std::list <int>::iterator > temp2(test2.begin(), test2.end()); 
    // This must not point to the beginning iterator. 
    assert(temp2.data() != &*test2.begin()); 

    int test3[10]; 
    IteratorBuffer < int * > temp3(&test3[0], &test3[10]); 
    // This should point to the array. 
    assert(temp3.data() == &test3[0]); 

    std::deque <int> test4; 
    for(int i = 0; i < 10; ++i) 
     test4.push_back(i); 
    IteratorBuffer < std::deque <int>::iterator > temp4(test4.begin(), test4.end()); 
    // This must not point to the beginning iterator, not safe. 
    assert(temp4.data() != &*test4.begin()); 
} 

这失败的最后一次测试,因为的std ::双端队列的迭代器有random_access_iterator_tag。

我该如何编写这个类,使它在一般情况下工作正常?

我想我应该提到我用VC++ 2010

编辑:正如亚当说,(我害怕这一点),这是不能直接成为可能。现在,我试图定义我自己的特征,使我能够做到这一点。看到我尝试以下:

template < typename Iterator > 
struct IteratorTraits 
{ 
    enum { IsPointerCompatible = false }; 
    typedef typename std::iterator_traits <Iterator>::value_type T; 
}; 
template < typename T > 
struct IteratorTraits < T * > 
{ 
    enum { IsPointerCompatible = true }; 
    typedef T T; 
}; 
template < typename T > 
struct IteratorTraits < const T * > 
{ 
    enum { IsPointerCompatible = true }; 
    typedef const T T; 
}; 
//template < typename T > 
//struct IteratorTraits < typename std::vector <T>::iterator > 
//{ 
// enum { IsPointerCompatible = true }; 
// typedef T T; 
//}; 
//template < typename T, size_t N > 
//struct IteratorTraits < typename std::array < T, N >::iterator > 
//{ 
// enum { IsPointerCompatible = true }; 
// typedef T T; 
//}; 

,因为它们非常类似于使用的std :: iterator_traits的那些我省略了IteratorBuffer类。

前两个专业化工作,但两个评论性状结构不起作用。我如何编写这些程序以使其能够工作,而不依赖于特定的STL实现?

+0

'&* test2.begin()'看起来很丑! – iammilind 2012-03-21 04:55:56

+0

它的丑陋,但它的作品... – dsharlet 2012-03-21 05:01:20

+1

这恐怕不是真的可能。唯一的可能性是专门针对特定的迭代器,例如'vector :: iterator'。请注意'vector :: reverse_iterator'不安全。 – 2012-03-21 06:33:22

回答

1

你可以明确地指定指针,然后对任何容器(你知道)哪些迭代器可以这样处理(这是std::vector的情况)。这并没有那么糟糕,因为没有一个容器的一般“特征”可以说它的迭代器可以用作指针。这是容器供应商必须明确规定的保证。


还要注意的IteratorBuffer普通版默默地承担至少前向迭代器类别,将只输入迭代器失败(因为它使用范围的两倍)。

+0

感谢您的回答。看到我的编辑,我试图实现你描述的内容,但是我很难理解如何为特定的迭代器类定义特化。 – dsharlet 2012-03-21 16:20:58

+0

我试过了,我失败了。相信没有合理的标准兼容解决方案。但我会尝试更多,如果我会找到任何我会在这里发布。 – 2012-03-22 12:49:32