std::string s;
vs. char s[10];
"jacaranda"
is a character array literal.
char prompt[5] = "hello";
string.h
utilities.
char s1[4] = "red"; char s2[4] = "red"; if (s1 == s2) std::cout << "yes\n";
<string>
.
==
compares contents, not addresses.
#include <string>
to define strings.
std::
prefix.
using std::string;
to forego the std::
prefix.
std::string name = "jacaranda";
[ ]
subscripts, just like arrays.
0
to n - 1
.
.length()
member function returns the number of characters in
a string.
[ ]
subscript access to strings is not checked.
.at()
subscript access is checked.
std::string word1 = "what?" std::string word2 word2 = word1 word1[4] = '!' // word2[4] == '?'
+
does string catenation.
+=
does appending.
filename += ".h";
==
works as expected.
<
define a lexicographic (dictionary) order.
>>
and <<
work as expected.
>>
eats white space; stops at white space.
getline()
function doesn't eat white space.
char line[1024]; std::cin >> line;
getline()
is defined in <string>
.
.getline()
istream member function.
c_str()
member function.
s
changes, so might s.c_str()
.
<string>
.
[ ]
subscript access.
This page last modified on 31 January 2003.