我有一段代码,它只检索向量中的所有元素。我只想检索指定范围内的向量元素,该范围将被提供给函数。我已经尝试过递增迭代器,但是我无法检查我想要的向量元素的范围的末尾。代码:
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);
}
}在上面的代码中,我不确定如何检查结束位置,然后中断。提前感谢您的帮助。
发布于 2014-03-17 13:18:05
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;发布于 2014-03-17 13:48:22
好吧,我不知道这是不是一个好的实践,但是你可以从一个向量中检索元素,就像你从一个数组中提取元素一样,就像这样:
for(int k=0; k< 5; k++)
cout << VectorName.at[k] << endl;发布于 2014-03-17 14:19:25
为什么不用这个-
void calculate(int start, int end)
{
for (int i = start ; i<end && i < dupViewResults.size(); i++)
{
//do your stuff with dupViewResults[i]
}
}https://stackoverflow.com/questions/22447384
复制相似问题