Prikaz jedne poruke
Stara 12.2.2012, 13:12   #20
Belphegor
V.I.P. Programiranje
 
Član od: 29.8.2007.
Lokacija: Valjevo
Poruke: 1.349
Zahvalnice: 983
Zahvaljeno 371 puta na 280 poruka
Određen forumom Re: How to... C++

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".

Poslednja ispravka: Belphegor (12.2.2012 u 19:46) Razlog: std::string --> std::basic_string<CharType> & unsigned --> std::size_t
Belphegor je offline   Odgovor sa citatom ove poruke
Sledećih 3 korisnika se zahvaljuje korisniku Belphegor na korisnoj poruci:
EclipsE (12.2.2012), Geomaster (12.2.2012), M.Silenus (12.2.2012)