In this lesson, we’ll take a look at how to construct objects of std::string, as well as how to create strings from numbers and vice-versa.
String construction
The string classes have a number of constructors that can be used to create strings. We’ll take a look at each of them here.
Note: string::size_type resolves to size_t, which is the same unsigned integral type that is returned by the sizeof operator. Its actual size varies depending on environment. For purposes of this tutorial, envision it as an unsigned int.
string::string()
Sample code:
Output: |
string::string(const string& strString)
Sample code:
Output: my string |
string::string(const string& strString, size_type unIndex) string::string(const string& strString, size_type unIndex, size_type unLength)
Sample code:
Output: string stri |
string::string(const char *szCString)
Sample code:
Output: my string |
string::string(const char *szCString, size_type unLength)
Sample code:
Output: my s |
string::string(size_type nNum, char chChar)
Sample code:
Output: QQQQ |
template
No sample code for this one. It’s obscure enough you’ll probably never use it. |
string::~string()
String destruction
No sample code here either since the destructor isn’t called explicitly. |
Constructing strings from numbers
One notable omission in the std::string class is the lack of ability to create strings from numbers. For example:
std::string sFour{ 4 };
Produces the following error:
c:vcprojectstest2test2test.cpp(10) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'
Remember what I said about the string classes producing horrible looking errors? The relevant bit of information here is:
cannot convert parameter 1 from 'int' to 'std::basic_string
In other words, it tried to convert your int into a string but failed.
The easiest way to convert numbers into strings is to involve the std::ostringstream class. std::ostringstream is already set up to accept input from a variety of sources, including characters, numbers, strings, etc… It is also capable of outputting strings (either via the extraction operator>>, or via the str() function). For more information on std::ostringstream, see 23.4 -- Stream classes for strings.
Here’s a simple solution for creating std::string from various types of inputs:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
inline std::string ToString(T tX)
{
std::ostringstream oStream;
oStream << tX;
return oStream.str();
}
Here’s some sample code to test it:
int main()
{
std::string sFour{ ToString(4) };
std::string sSixPointSeven{ ToString(6.7) };
std::string sA{ ToString('A') };
std::cout << sFour << '\n';
std::cout << sSixPointSeven << '\n';
std::cout << sA << '\n';
}
And the output:
4 6.7 A
Note that this solution omits any error checking. It is possible that inserting tX into oStream could fail. An appropriate response would be to throw an exception if the conversion fails.
Converting strings to numbers
Similar to the solution above:
#include <iostream>
#include <sstream>
#include <string>
template <typename T>
inline bool FromString(const std::string& sString, T &tX)
{
std::istringstream iStream(sString);
return !(iStream >> tX).fail(); // extract value into tX, return success or not
}
Here’s some sample code to test it:
int main()
{
double dX;
if (FromString("3.4", dX))
cout << dX << '\n';
if (FromString("ABC", dX))
cout << dX << '\n';
}
And the output:
3.4
Note that the second conversion failed and returned false.