Похожие чаты

I wrote a function like split method in javascript: vector<string> split(string

str, string ch) {

vector<string> v;
int i = 0;
int s = 0;
int e = str.find(ch, s);

while (e < str.length()) {
v.insert(v.begin() + i, str.substr(s, e - s));
s = e + ch.length();
e = str.find(ch, s);
i++;
}

v.insert(v.begin() + i, str.substr(s, str.length() - s));
return v;
}

since returning a vector by value is not efficient, I wonna know if there is any way to fix it or not.
there is some tricks like decalring vector in the split function static and then return it by reference, or passing a pointer to an empty vector to function, change it and then return it. but I wonna use a better way. Is there any?

3 ответов

6 просмотров

returning a std::vector by value is fairly efficent

std::move my dude

this is quite efficient. the compiler will see the return statement and convert the lvalue to an rvalue and perform move construction automatically.

Похожие вопросы

Обсуждают сегодня

Карта сайта