ベクトルがあり、STL アルゴリズムを使用してベクトルの後半を別のベクトルに効率的に分割したいと考えています。これを行う方法の 1 つを次に示しますが、より効率的で簡潔な回答、または少なくとも stl アルゴリズムを使用する回答があることを期待しています。
std::vector<Entry> &entries = someFunction();
int numEntries = entries.size();
// Assume numEntries is greater than or equal to 2.
std::vector<Entry> secondEntries;
std::vector<Entry>::iterator halfway = entries.begin() + numEntries / 2;
std::vector<Entry>::iterator endItr = entries.end()
// Copy the second half of the first vector in the second vector:
secondEntries.insert(secondEntries.end(), halfway, endItr);
// Remove the copied entries from the first vector:
entries.erase(halfway, endItr);