【问题标题】:Iterate over boost::shared_array迭代 boost::shared_array
【发布时间】:2013-03-13 08:49:18
【问题描述】:

您将如何迭代 boost::shared_array 中的项目?你会在上面做一个get() 并使用一个原始指针作为迭代器吗?

【问题讨论】:

    标签: c++ boost c++98


    【解决方案1】:

    既然你已经在使用 boost,可能是这样的:

    #include <boost/shared_array.hpp>
    #include <boost/range.hpp>
    #include <iostream>
    
    int main()
    {
        boost::shared_array<int> arr(new int[10]());
    
        int* ptr = arr.get();
        for (int i : boost::make_iterator_range(ptr, ptr+10))
        {
            std::cout << i << ',';
        }
    }
    

    在任何情况下,您都需要自己记录数组的大小。

    【讨论】:

      【解决方案2】:

      看到您已经知道数组的大小,因为在创建 boost::shared_array 之前必须对其进行分配,我看到迭代它的唯一方法是使用普通的 for 循环,然后使用operator[i]boost::shared_array 上获取元素。

      【讨论】:

        猜你喜欢
        • 2017-08-22
        • 1970-01-01
        • 2014-03-21
        • 1970-01-01
        • 2016-03-23
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        • 2014-11-16
        相关资源
        最近更新 更多