How to save the detail information of elements of

2019-09-15 07:09发布

问题:

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:

  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

   ...
}