Get vector data only for specified intervals

243 views Asked by At

I have a code which only retrieves all the elements in a vector. I want to retrieve only the vector elements for a specified range which would be given to the function. I have tried iterator incrementing but I am unable to check the end of the range for which I want the vector elements. Code:

void calculate(int start, int end)
{
  //Vector dupViewResults is already populated with another function
  //The definition of dupViewresults is vector<vector<string>> dupViewResults

  vector< vector<string> >::iterator row;
  for (row = dupViewResults.begin(); row != dupViewResults.end(); ++row)
  {
    std::advance(row, start); //Setting the iterator to the starting element.
    std::string filedata = readFileDataInStr(row->at(0));
    int x = boost::lexical_cast<int>( row->at(1) );
    long long int fileCRC32 = MurmurHash(filedata.c_str(), x, 0);
    std::string strCRC32 = std::to_string(fileCRC32);
    std::string query = "update duplicatesview set CRC32='" + strCRC32 + "' where Filepath='" + row->at(0) + "'";
    char* charQuery = &query[0];
    db.fireSQLQuery(charQuery, results);
  }
}

In the above code, I am not sure how to check the end position and then break. Thanks in advance for your help.

3

There are 3 answers

0
Idan Yehuda On

why not use this -

void calculate(int start, int end)
{
    for (int i = start ; i<end && i < dupViewResults.size(); i++)
    {
         //do your stuff with dupViewResults[i]
    }
}
2
BlackMamba On
int elementNumbers = dupViewResults.size();
vector< vector<string> >::iterator temp; 
if(end < elementNumbers)
     std::advance(temp, end);
else 
    temp = dupViewResults.end();

if(row > temp) //  check the end position and then brea
    break;
0
Xara On

Well I don't know much whether it is a good practice or not but you can retrieve the elements from a vector just like the way you extract elements from an array like this:

for(int k=0; k< 5; k++)
 cout << VectorName.at[k] << endl;