Shifting from C to C++ strings
-
Avoid memory corruption errors
Easier to maintain
Simple to use
Should be fast (is reference counted)
Supports wide, narrow, anything-else characters
Standard
Cross-platform
Why change?
-
Available in two flavours
std::string
for storing narrow charactersstd::wstring
for storing wide characters
C++ std::basic_string
-
-
Not normally needed, but if wanted you can instantiate basic_string for your own character type
-
E.g.
basic_string<MyCharacterClass>
-
E.g.
Comparing C and C++
#include <string.h> #include <assert.h> int main() { char name[15]; // !!! strcpy(name, "David"); int len = strlen(name); strcat(name, " Moore"); name[5] = 'J'; assert(strcmp(name, "Oliver")); return 0; } #include <string> #include <assert.h> int main() { std::string name; name = "David"; int len = name.length(); name += " Moore"; name[5] = 'J'; assert(name != "Oliver"); return 0; }
-
-
A C++ string can be constructed from a C character pointer
-
-
char* p = "David";
std::string str = p;
-
-
Interoperability between C and C++
-
-
A C++ string can return a C character pointer to its contents. But...
-
Not possible to change the string with this pointer
-
Don't use the pointer after the string has been destroyed
-
std::string str = "Cocacola";
char* p = str.c_str();
-
-
Not possible to change the string with this pointer
-
Many different search functions
Find substring
find
rfind
Searching within a string
-
Find character
find_first_of
find_last_of
find_first_not_of
find_last_not_of
-
Special value
string::npos
means "not in the string"
-
std::string line = "abcd Apple butter chicken delicious $0.62"; // abcd string::size_type codeEnd = line.find_first_of(" \t"); string code = line.substr(0, codeEnd); // 0.62 string::size_type priceBegin = line.find_last_of('$'); Double price = atof(line.substr(priceBegin + 1).c_str()); // Apple butter chicken delicous string::size_type descBegin = line.find_first_not_of(" \t", codeEnd); string::size_type descEnd = line.find_last_not_of(" \t", priceBegin - 1); string desc = line.substr(descBegin, descEnd - descBegin + 1);
Searching example
-
substr(i, n)
returns a new string object of n characters starting from i
-
n defaults to being to the end of the string
-
std::wstring str = L"Cocacola";
std::wcout << str.substr(4) << std::endl;
-
Sub-strings
-
Easy to change the contents of a string
std::string name("David Moore"); name.replace(3, 2, "e"); // Dave Moore name.insert(0, "Mr. "); // Mr. Dave Moore name.erase(4, 5); // Mr. Moore
Replace / Insert / Erase
-
C++ strings support streaming
Output
std::cout << name << std::endl;
Input a word
std::cin >> name;
Input a whole line
getline(cin, line);
String I/O
std::string a = "Coca"; std::string b = "cola"; std::string c = a + b; c += "(R)"; int v = c.compare("Pepsi");
Some other operations
-
A C++ string object looks a lot like a C++ collection (of characters)
Has iterators,begin(), end(), push_back()
etc.
Can be used with generic algorithms
std::transform(str.begin(), str.end(), str.begin(), tolower);