. Advertisement .
..3..
. Advertisement .
..4..
A string class contains features of a bits stream that can handle a character with a single byte. In the programing language C++, a string might be retrieved but it should be examined about its way of retaining and transferring. Because the returning value of C++ is on the heap which has limited space, offering large components can cause errors and security faults.
Hence, returning a string object from a library of the standard template can provide us with a constant pointer for the string, which can ensure that the static memory will store the string. Therefore, this blog will show you how to return a string in C++ from a function. Read the details below!
How to return a string in C++ from a function
1. Use the Notation std::string fun() to Return a String in C++ from a Function
The most used method to return a string in C++ from a function is returning by value. Because the class std::string contain a move constructor, it is efficient for long strings to be returned by value. An object is supposed to be defined by move semantics if it contains the move constructor. The term move semantics indicates that copying an object to another location on the return of a function will not happen, hence, the time to execute the function is faster.
#include <iostream>
#include <algorithm>
#include <iterator>
using std::cout; using std::endl;
using std::string; using std::reverse;
string ReverseString(string &s){
string rev(s.rbegin(), s.rend());
return rev;
}
int main() {
string str = "This string shall be reversed";
cout << str << endl;
cout << ReverseString(str) << endl;
return EXIT_SUCCESS;
}
Output:
This string shall be reversed
desrever eb llahs gnirts sihT
2. Use the Notation std::string &fun() to Return a String in C++ from a Function
With this method, the same functionality above will be conducted by using the approach of returning by a notation of reference. Although this approach is regarded as the most effective way of returning large classes or structures, the extra overhead will not be imposed in comparison to the mentioned method above. Notably, you should not use the reference to change a declared local variable within the function because this will result in the dangling reference.
#include <iostream>
#include <algorithm>
#include <iterator>
using std::cout; using std::endl;
using std::string; using std::reverse;
string &ReverseString(string &s) {
reverse(s.begin(), s.end());
return s;
}
int main() {
string str = "Let this string be reversed";
cout << str << endl;
cout << ReverseString(str) << endl;
return EXIT_SUCCESS;
}
Output:
Let this string be reversed
desrever eb gnirts siht teL
3. Use the Notation char *func() to Return a String in C++ from a Function
An alternative method for returning a string in C++ from a function is using char*
. Keep in mind that characters are stored by the class std::string
as an ongoing array. Hence, by using the built-in method data()
, a pointer head to the first character element on that array can be returned. Nevertheless, guarantee that a similar method c_str()
will not be used when the object std::string
is returned an array of the null-terminated character, because the pointer const to the first character element will be replaced.
#include <iostream>
#include <algorithm>
#include <iterator>
using std::cout; using std::endl;
using std::string; using std::reverse;
char *ReverseString(string &s) {
reverse(s.begin(), s.end());
return s.data();
}
int main() {
string str = "This string must be reversed";
cout << str << endl;
cout << ReverseString(str) << endl;
return EXIT_SUCCESS;
}
Output:
This string must be reversed
desrever eb tsum gnirts siht
Conclusion
The class of strings in C++ contains all methods mentioned above. Through this blog, you have been provided with various methods to return a string in C++ from a function including using the notations std::string fun()
, std::string &fun()
, and char *func()
. Hope that the coding tutorials above will be a helping hand for you to solve this problem. If you expect to find out more IT solutions, you can read other posts on this website!
Leave a comment