C++ 在创建对象之前确保数据的唯一性?

C++ 在创建对象之前确保数据的唯一性?,c++,C++,我有一个文件,里面装满了我需要处理的数据。该文件的每一行都包含如下所示的浮点数(当然是字符串): std::ifstream data_file("data.bin"); Point3d p1; Point3d p2; Point3d p3; // Input the first 3 points on the line. data_file >> p1; data_file >> p2; data_file >> p3;

我有一个文件,里面装满了我需要处理的数据。该文件的每一行都包含如下所示的浮点数(当然是字符串):

  std::ifstream data_file("data.bin");
  Point3d p1;
  Point3d p2;
  Point3d p3;
  // Input the first 3 points on the line.
  data_file >> p1;
  data_file >> p2;
  data_file >> p3;
  // Or:  data_file >> p1 >> p2 >> p3

  // Before creating a Foo:
  std::vector<Point3d> existing_points;
  std::vector<Point3d>::const_iterator iter;
  bool is_duplicate = false;
  for (iter = existing_points.begin();
       iter != existing_points.end();
       ++iter)
  {
      if (*iter == p1)
      {
         is_duplicate = true;
         break;
      }
  }
  if (! is_duplicate)
  {
     existing_points.push_back(p1);
     // Create a new Foo
  }
x1 y1 z1 x2 y2 z2 x3 y3 z3 r1 g1 b1 r2 g2 b2 r3 g3 b3

每行代表我需要创建的3个不同对象,如:
foofoo1(x1,y1,z1,r1,g1,b1)
Foo-foo2(x2,y2,z2,r2,g2,b2)
foofoo3(x3,y3,z3,r3,g3,b3)

关键在于:

  • 我只想创建尚未创建的对象。如果我已经用完全相同的6个浮点数创建了一个
    Foo
    ,而不是创建一个新的
    Foo
    ,我只想检索我已经创建的对象

  • 文件中的数据分隔不可靠——不同数字之间可能有任意数量的空格或制表符

  • 在我意识到这个间距问题之前,我将从文件中读取的每一行拆分为字符串,连接表示每个foo的字符串,然后使用
    std::map
    测试唯一性,其中,映射返回的int表示一个
    std::vector
    的索引,我将指针推送到我创建的每个
    Foo
    中。当间距不一致时,此操作失败

    我现在所能想到的就是这样做:
    (例如)但这似乎完全是胡说八道。我还试着制作一个由6个浮点值组成的结构,重载
    运算符>>
    ,将stringstream中的数据读入其中,然后制作一个
    映射
    ,但如果您只是想快速解决问题,我真的不知道如何重载
    运算符

    std::stringstream sIn(theline);
    std::stringstream sOut;
    
    std::string temp;
    
    while (sIn >> temp) sOut << temp << " ";
    
    std::string fixedline = sOut.str();
    
    std::stringstream sIn(theline);
    std::stringstream sOut;
    std::字符串温度;
    
    而(sIn>>temp)sOut如果您只想快速解决问题,请执行类似的操作

    std::stringstream sIn(theline);
    std::stringstream sOut;
    
    std::string temp;
    
    while (sIn >> temp) sOut << temp << " ";
    
    std::string fixedline = sOut.str();
    
    std::stringstream sIn(theline);
    std::stringstream sOut;
    std::字符串温度;
    while(sIn>>temp)sOut1:使用
    std::set
    记录您已经构建的内容。
    std::map
    +
    std::vector
    解决方案既不可靠,而且在修复后会造成过度杀伤力

    2:以下内容跳过输入中的任何前导空格:

    double x;
    std::cin >> x;
    
    对于任何
    std::istream

    1:使用
    std::set
    记录您已经构建的内容。
    std::map
    +
    std::vector
    解决方案既不可靠,而且在修复后会造成过度杀伤力

    2:以下内容跳过输入中的任何前导空格:

    double x;
    std::cin >> x;
    

    对于任何
    std::istream

    类似,我想我应该在比较之前将输入转换为实浮点。对于浮点数,字符串表示形式的差异不一定表示实际值的差异。举个例子,
    12345
    等于
    1.2345e4

    我想我应该在比较之前将输入转换成实浮点。对于浮点数,字符串表示形式的差异不一定表示实际值的差异。举个例子,
    12345
    等于
    1.2345e4

    我看到的问题是找到一个精确匹配,因为您使用的是浮点数

    我建议在
    Foo
    中添加
    操作符==(x,y,z,r,g,b)
    。将所有
    Foo
    对象放入一个容器中,并使用
    操作符==()
    比较它们,以查看是否已创建
    Foo
    对象

    另一种方法是将
    分解到父结构中。创建这些内容的容器。创建
    Foo
    对象时,将实例放入容器中。您可以在创建下一个
    Foo
    实例之前搜索容器


    我还建议转置数据文件,使每一行只包含一个x,y,z,r,g,b向量。对文件进行排序。在将其提供给您的程序之前删除重复项。使用程序外的应用程序执行此操作(首选)。Cygwin和Linux有一些很好的实用程序用于排序和删除重复项

    Edit1:带有输入运算符的3D点示例(未测试)
    ifndef POINT3D\U水电站
    #包括
    #包括
    类Point3d
    {
    公众:
    Point3d(双x=0.0,双y=0.0,双z=0.0)
    :mux(x),muy(y),muz(z)
    { ; }
    点3D(常量点3D和p3d)
    :m_x(p3d.m_x)、m_y(p3d.m_y)、m_z(p3d.m_z)
    { ; }
    friend std::istream&operator>>(std::istream&inp、Point3d&p3d);
    布尔运算符==(常量点3D&p)常量
    {
    返回(标准::晶圆厂(m_x-p.m_x)<5.0E-5)
    &&(标准::晶圆厂(m_y-p.m_y)<5.0E-5)
    &&(标准:晶圆厂(m_z-p.m_z)<5.0E-5);
    }
    私人:
    双m_x;
    双m_y;
    双m_z;
    };
    内联std::istream&
    操作员>>(标准::istream&inp、Point3d&p)
    {
    inp>>p.m_x;
    inp>>下午五点;
    inp>>p.m_z;
    返回inp;
    }
    #endif//POINT3D\u水电站
    福班
    :公共点3D
    {
    公众:
    Foo(双x,双y,双z,
    双r,双g,双b)
    :点3D(x,y,z),
    m_r(r)、m_g(g)、m_b(b)
    { ; }
    Foo(常数点3D和p3d,
    双r,双g,双b)
    :点3D(p3d),
    m_r(r)、m_g(g)、m_b(b)
    { ; }
    双m_r,m_g,m_b;
    };
    
    读取数据文件的过程如下所示:

      std::ifstream data_file("data.bin");
      Point3d p1;
      Point3d p2;
      Point3d p3;
      // Input the first 3 points on the line.
      data_file >> p1;
      data_file >> p2;
      data_file >> p3;
      // Or:  data_file >> p1 >> p2 >> p3
    
      // Before creating a Foo:
      std::vector<Point3d> existing_points;
      std::vector<Point3d>::const_iterator iter;
      bool is_duplicate = false;
      for (iter = existing_points.begin();
           iter != existing_points.end();
           ++iter)
      {
          if (*iter == p1)
          {
             is_duplicate = true;
             break;
          }
      }
      if (! is_duplicate)
      {
         existing_points.push_back(p1);
         // Create a new Foo
      }
    
    std::ifstream数据_文件(“data.bin”);
    点3d-p1;
    点3d-p2;
    点3d-p3;
    //在直线上输入前3个点。
    数据文件>>p1;
    数据文件>>p2;
    数据文件>>p3;
    //或:数据文件>>p1>>p2>>p3
    //在创建Foo之前:
    std::向量存在_点;
    std::vector::const_迭代器iter;
    bool is_duplicate=false;
    for(iter=现有的_点。begin();
    iter!=现有的_points.end();
    ++国际热核聚变实验堆(iter)
    {
    if(*iter==p1)
    {
    is_duplicate=true;
    打破
    }
    }
    如果(!是重复的)
    {
    现有_点。向后推_(p1);
    //创造