Lets say I have a function
str_rev(string &s, int len) {}
which reverses the string s of length len
I want to reverse a substring of long string starting at index 5 and of length 10
for this I was forced to first call substring function and then call the str_rev function passing the substring
sub_string = long_str.substr(5, 10)
str_rev(sub_string, 10);
Is there any way to achieve this without actually creating a temporary object?
Make your function take iterators (or, rather, use std::reverse()
) and pass in iterators delimiting the substring.
Maybe you just want to do this:
std::string::iterator it = long_str.begin() + 5;
std::reverse(it, it+10);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With