c++11 - C++ best way to split vector into n vector -
i have std::vector<std::string>
in vector push_back
string txt file, :
std::string line; std::vector<std::string> path; while(getline(fichier, line)) { path.push_back(line); }
i split path vector
n other vector
of 10 line example. if size of vector
25, want 2 other vector of 10 element , 1 vector
of 5 element.
what best way ?
best matter of opinion, following (with bunch_size
being 10
):
for(size_t = 0; < strings.size(); += bunch_size) { auto last = std::min(strings.size(), + bunch_size); bunches.emplace_back(strings.begin() + i, strings.begin() + last); }
if strings large , want avoid copying, can go move version:
for(size_t = 0; < strings.size(); += bunch_size) { auto last = std::min(strings.size(), + bunch_size); auto index = / bunch_size; auto& vec = bunches[index]; vec.reserve(last - i); move(strings.begin() + i, strings.begin() + last, back_inserter(vec)); }
Comments
Post a Comment