如何在指针中保存向量元素的详细信息? [英] How to save the detail information of elements of a vector in a pointer?

查看:100
本文介绍了如何在指针中保存向量元素的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较2个二维向量,这两个向量在每52行中都包含10个像元.我正在尝试比较以前5行为参考,然后将所有其他行与这5行进行比较,然后最重要的是保存所有信息(确切地找到了参考单元的哪个单元)(参考ID和单元格的位置)与每一行的哪个单元格相似.在参考单元格中找不到的单元格应照原样打印.这就是我尝试过的:

int main(){
    vector <vector<string>> reference_one_cell;   /*stored all the cells of 5 reference lines */
    vector < vector<string> > input_array;    /*stored all the cells all 52  lines  */

                  /*comparison part*/   

          std::set<std::string> stringSet;
          for ( auto const& vs : reference_one_cell)
          for ( auto const& item : vs )
           stringSet.insert(item);

                 for (int f=0;f<52;f++){
                  for (int g=0;g<10;g++){

                    bool found = any_of(stringSet.begin(),
                                 stringSet.end(),
                                  [=](std::string const& item){return input_array[f][g] == item;});
                     if ( found )
                     {

                        outputFile << ",";
                     }
                     else
                     {

                        outputFile<<input_array[f][g];
                     }
                  }

               }

  }     

对于在参考行中找到的单元格,我可以打印出,".但是对于如何使用指针来存储我可以返回到再次恢复原始状态.预先感谢

解决方案

  1. 添加一个变量以跟踪找到匹配项的reference_one_cell索引.

    std::vector<std::pair<std::size_t, std::size_t>> indices;
    

  2. 一旦找到匹配项,请确保更新indices的内容.

  3. 在最外面的循环结束后使用indices.


int main(){

   ... 

   // Variable to keep track of matching indices.
   std::vector<std::pair<std::size_t, std::size_t>> indices;

   ...

   for (int f=0;f<52;f++){
      for (int g=0;g<10;g++){

         bool found = any_of(stringSet.begin(),
                             stringSet.end(),
                             [=](std::string const& item){return input_array[f][g] == item;});
         if ( found )
         {
            // Use more elaborate but explicit code.
            // indices.push_back(std::pair<std::size_t, std::size_t>{f, g});

            // Or use succinct but not so explicit code.
            indices.push_back({f, g});

            outputFile << ",";
         }
         else
         {
            outputFile<<input_array[f][g];
         }
      }
   }

   // Use indices

   ...
}

i am trying to compare 2 two dimensional vectors, both of which contain 10 cells inside every 52 lines. I am trying to compare taking first 5 lines as a reference and then compare all the other lines with those 5 lines and then most importantly save all the information (reference id and position of the cell) that exactly which cell of the reference cell was found similar with which cells of every lines. The cells which cant be found in the reference cell should be printed as it is. this is what i tried:

int main(){
    vector <vector<string>> reference_one_cell;   /*stored all the cells of 5 reference lines */
    vector < vector<string> > input_array;    /*stored all the cells all 52  lines  */

                  /*comparison part*/   

          std::set<std::string> stringSet;
          for ( auto const& vs : reference_one_cell)
          for ( auto const& item : vs )
           stringSet.insert(item);

                 for (int f=0;f<52;f++){
                  for (int g=0;g<10;g++){

                    bool found = any_of(stringSet.begin(),
                                 stringSet.end(),
                                  [=](std::string const& item){return input_array[f][g] == item;});
                     if ( found )
                     {

                        outputFile << ",";
                     }
                     else
                     {

                        outputFile<<input_array[f][g];
                     }
                  }

               }

  }     

i am able to print out "," for the cells that were found in the reference lines. But really stuck for few days with how to use pointer or pair to store all the detail information( reference id and position in the reference lines) that i can go back to the original condition again. thanks in advance

解决方案

  1. Add a variable to keep track of the indices of reference_one_cell where matching items were found.

    std::vector<std::pair<std::size_t, std::size_t>> indices;
    

  2. Make sure to update the contents of indices once you find a match.

  3. Use indices after the end of the outermost loop.


int main(){

   ... 

   // Variable to keep track of matching indices.
   std::vector<std::pair<std::size_t, std::size_t>> indices;

   ...

   for (int f=0;f<52;f++){
      for (int g=0;g<10;g++){

         bool found = any_of(stringSet.begin(),
                             stringSet.end(),
                             [=](std::string const& item){return input_array[f][g] == item;});
         if ( found )
         {
            // Use more elaborate but explicit code.
            // indices.push_back(std::pair<std::size_t, std::size_t>{f, g});

            // Or use succinct but not so explicit code.
            indices.push_back({f, g});

            outputFile << ",";
         }
         else
         {
            outputFile<<input_array[f][g];
         }
      }
   }

   // Use indices

   ...
}

这篇关于如何在指针中保存向量元素的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆