Samo da dodam. Mislim da u tvom primeru mogu da se izbegnu extra kopije sa:
Kod:
template<typename CharType>
std::vector<std::basic_string<CharType> >
split(std::basic_string<CharType> const& str, std::basic_string<CharType> const& delim)
{
std::vector<std::basic_string<CharType> > result;
std::size_t last_pos = 0;
std::size_t pos = 0;
while( ( pos = str.find(delim, pos) ) != str.npos ){
std::basic_string<CharType> current = str.substr(last_pos, pos-last_pos);
if(current.length() != 0)
result.push_back(std::move(current));
pos += delim.length();
last_pos = pos;
}
if(last_pos != str.length()){
std::basic_string<CharType> current = str.substr(last_pos, str.length()-last_pos);
result.push_back(std::move(current));
}
return result;
}
mislim da je ovde u redu da se "rip the guts out" od string-a (sto bi rekao Scott Meyers) posto se
current ne koristi nigde posle
push_back-a u tom scope-u?
Mada ovo zavisi od kompajlera ako podrzava "move semanitics".