使用boost库中的iterator::counting_iterator实现基于数值范围的迭代器是一种十分方便和高效的方式

111 篇文章 ¥59.90 ¥99.00
本文介绍了如何利用boost库中的counting_iterator创建基于数值范围的迭代器,通过示例展示了其在遍历整数序列、自增操作及比较操作中的应用,强调了其在实际开发中的便利性和可扩展性。

使用boost库中的iterator::counting_iterator实现基于数值范围的迭代器是一种十分方便和高效的方式。下面我们来看一段简单的测试程序,了解一下counting_iterator的使用方法。

#include <iostream>
#include <boost/iterator/counting_iterator.hpp>

int main() {
    // 构造一个从0到9的迭代器
    auto begin = boost::make_counting_iterator(0);
    auto end = boost::make_counting_iterator(10);

    // 打印迭代器范围内的数字
    for (auto it = begin; it != end; ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

上面的代码中,我们通过make_counting_iterator函数构造了一个迭代器,它可以顺序遍历从0到9的整数。在循环中,我们使用该迭代器打印出了所有数字。

counting_iterator本身是一个递增的迭代器,每次对它进行自增操作时会返回它自己。同时,counting_iterator还支持比较操作,这允许我们在循环中使用迭代器范围进行循环,而不必担心越界等问题。

总的来说,boost库中的iterator::counting_iterator是一个方便易用的工具,可以帮助我们快速地构建基于数值范围的迭代器。在实际开发中,我们可以根据需要进行更多的定制和扩展,以满足特定的需求。

void ConvertMatToPointCloudThrust(cv::Mat& depth, pcl::PointCloud<pcl::PointXYZ>& cloud) { const float xInternal = 0.004916f * 4; const float yInternal = 0.005f * 4; const int width = depth.cols; const int height = depth.rows; // 1. 统一内存管理 thrust::device_vector<float> d_depth(height * width); thrust::device_vector<DevicePoint> d_cloud(height * width); thrust::device_vector<bool> d_validmask(height * width); thrust::copy_n(reinterpret_cast<float*>(depth.data), height * width, d_depth.begin()); // 2. 融合计算与压缩 auto zipped = thrust::make_zip_iterator( thrust::make_tuple(d_depth.begin(), thrust::make_counting_iterator(0))); thrust::transform( thrust::cuda::par, zipped, zipped + height * width, thrust::make_zip_iterator( thrust::make_tuple(d_cloud.begin(), d_validmask.begin())), [=] __device__(const auto & in) { const int idx = thrust::get<1>(in); const float z = thrust::get<0>(in); if (z > 800.0f) return thrust::make_tuple(DevicePoint(), false); const int x = idx % width; const int y = idx / width; return thrust::make_tuple( DevicePoint( (x - width / 2) * xInternal * z, (y - height / 2) * yInternal * z, z ), true ); }); // 3. 高效压缩 thrust::device_vector<DevicePoint> compressed_cloud(height * width); auto new_end = thrust::copy_if( thrust::cuda::par, d_cloud.begin(), d_cloud.end(), d_validmask.begin(), compressed_cloud.begin(), [](bool valid) { return valid; } ); // 4. 零拷贝传输 const int valid_count = thrust::distance(compressed_cloud.begin(), new_end); cloud.resize(valid_count); cudaMemcpy( cloud.points.data(), compressed_cloud.data().get(), valid_count * sizeof(pcl::PointXYZ), cudaMemcpyDevi
最新发布
03-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值