c++ - 遍历 boost::shared_array

标签 c++ boost c++98

您将如何遍历 boost::shared_array 中的项目?您会对其执行 get() 并使用原始指针作为迭代器吗?

最佳答案

因为你已经在使用 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 << ',';
    }
}

无论如何,您需要自己记录数组的大小。

关于c++ - 遍历 boost::shared_array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15380589/

相关文章:

c++ - 在 C++ 中,在 STL 容器中存储具有重载 "operator&"的类对象是否合法?

c++ - g++ 编译器优化

c++ - boost::shared_ptr 和多线程访问

c++ - 是否允许限定函数类型作为模板参数 (C++98)

c++ - 我可以通过对象访问类中函数中的变量吗?

c++ - 我们可以在 Unix 编译器中使用 wmain() 还是它只能在 Windows 上运行?

c++ - 我想通过正则表达式处理一个字符串,但如何编写正确的正则表达式?

c++ - 使用 boost 程序选项通过配置文件/命令行解析自定义对象

c++ - 值初始化是 C++98 标准的一部分吗?如果不是,为什么在 C++03 标准中添加它?

c++ - 为什么在 SFINAE 期间需要参数中的指针?