C++ Tutorial
C++ Strings
string holds text. Include <string>, then concatenate, measure length, and read a line.
Include string, then declare
A string holds a sequence of characters. Include <string> at the top. Then declare the variable with type string and a value in double quotes. <iostream>alone is not enough for this type.
Try this at /cpp/try. The C++ editor compiles with g++.
Concatenate with +
+ joins strings. At least one side must already be a string object."Hi" + " there" is two literals and will not compile. Store one piece in a stringfirst, then add the rest.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string first = "Ada";
string last = "Lovelace";
string full = first + " " + last;
cout << first << endl;
cout << full << endl;
cout << first + " wrote programs" << endl;
return 0;
}length and the first character
length() returns how many characters are in the string. Index [0] is the first character, type char. Indexes start at zero, so the last character is atlength() - 1 when the string is not empty.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string word = "C++";
cout << word.length() << endl;
cout << word[0] << endl;
cout << word[1] << endl;
return 0;
}Do not read word[0] when the string is empty. That is undefined behavior. Checklength() first, or only index strings you just filled with text.
cin stops at a space
cin >> name reads one word. It skips leading whitespace, then stops at the next space or newline. A full name such as Ada Lovelace would store only Ada.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout << "Type one word: ";
cin >> word;
cout << "Got: " << word << endl;
return 0;
}getline reads a whole line
getline(cin, line) reads from the current position until newline and stores everything except that newline. Spaces stay in the string. Use it when the answer can contain spaces.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
cout << "Type a full line:" << endl;
getline(cin, line);
cout << "You typed: " << line << endl;
cout << "length " << line.length() << endl;
return 0;
}If you mix cin >> and then getline, the leftover newline from the first read can make getline return immediately. For this chapter, use one style in a program: either>> for words, or getline for lines.
Single quotes versus double quotes
'A' is a char. "A" is a string literal. Concatenation, length(), and getline belong to string. Next: math functions in <cmath>.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string name = "StudyGrid";
string tag = name.substr(0, 5);
cout << name << endl;
cout << "first five: " << tag << endl;
cout << "find Grid: " << name.find("Grid") << endl;
return 0;
}Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Biology
A short DNA motif
DNA is a sequence of bases: A, T, C, G. A pairs with T, C with G. The string "ATGCCAT" is not a gene. It is a motif you might search for in a longer strand.
std::string holds the characters. dna[0] is the first base. size() is how many bases, not including a hidden terminator you have to manage yourself.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string dna = "ATGCCAT";
cout << "strand: " << dna << endl;
cout << "first base: " << dna[0] << endl;
return 0;
}Chemistry
A formula as text
H2SO4 is sulphuric acid. Subscripts are written as ordinary digits in a string because C++ has no chemical typesetter. The letters are not variables named H and S.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string formula = "H2SO4";
cout << "sulphuric acid: " << formula << endl;
return 0;
}