OutputIterator到底是什么,以及如何构建一个用于CGAL Kd_tree::search的OutputIte

What exactly is an OutputIterator and how to build one for use with CGAL Kd_tree::search?

本文关键字:Kd CGAL 用于 一个 tree OutputIte search 何构建 OutputIterator 构建 是什么      更新时间:2023-10-16

我使用CGAL的Kd-tree实现以及模糊球体作为查询对象,以获得以点为中心的半径为r_max的球体中包含的点。下面是这个最小的工作示例:

    #include <CGAL/Simple_cartesian.h>
    #include <CGAL/Kd_tree.h>
    #include <CGAL/Search_traits_2.h>
    #include <CGAL/Fuzzy_sphere.h>
    #include <iostream>
    #include <fstream>
    typedef CGAL::Simple_cartesian<double>  K;
    typedef K::Point_2                      Point;
    typedef CGAL::Search_traits_2<K>        TreeTraits;
    typedef CGAL::Kd_tree<TreeTraits>       Kd_tree;
    typedef Kd_tree::Tree                   Tree;
    typedef CGAL::Fuzzy_sphere<TreeTraits>  Sphere;
    int main(int argc, char* argv[])
    {
        double r_max;
        Tree tree;
        /* ... fill the tree with points, set the value of r_max ...*/
        // Report indices for the neighbors within a sphere
        unsigned int   idc_query = tree.size()/2;           // test index
        Tree::iterator kti       = idc_query + tree.begin();                                                                                
        Sphere s_query(*kti, r_max);                            
        // Print points
        tree.search(std::ostream_iterator<Point>(std::cout, "n"), s_query);
        return 0;
    }

我从CGAL的示例(我的版本是3.9)的Spatial_searching文件夹下的nearest_neighbor_searching.cpp文件中获取并改编了注释"Print points"下面的行。

问题是:是否有一种方法可以让我设置一个不同的OutputIterator(而不是std::ostream_iterator),将指针/迭代器/句柄存储到从排序容器中搜索得到的点,而不是将点的坐标打印到标准输出?谢谢你。

在c++标准库中,有五种迭代器:

    输入迭代器
  • 输出迭代器
  • 向前迭代器
  • 双向迭代器
  • 随机访问迭代器

更多信息请参见cplusplus.com

在您的例子中,您需要一个输出迭代器,即:,一个对象it,它可以被加(++it)和解引用(*it)来获得一个非const引用,可以被写入。

您可以使用std::back_inserter:

创建一个输出迭代器,将写入到它的所有项插入到容器的末尾
#include <iterator>
#include <vector>
...
std::vector<Point> points;
tree.search(std::back_inserter(points), s_query);

东西在CGAL中已经进化了,那就是你可以存储除了点以外的其他东西。请看用户手册中使用任意点类型和点属性映射的例子。