Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
Take std::string word = "learnyard". A call on word can play one of three roles. An observer reports information, such as the number of characters or the position of a match. A mutator changes word itself. A producer returns a separate string while leaving word unchanged. The return value tells you what the call produced, but it does not by itself tell you what happened to word.
std::string word = "learnyard";
word.size(); // observer: reports 9
word.push_back('.'); // mutator: changes word
word.substr(5, 4); // producer: returns "yard", word stays changedYou should track two results after every string call: the value returned by the call and the current contents of word. size() returns a number and leaves word alone. push_back() changes word. substr() returns a new string and leaves word alone. Treating every call as a producer makes you miss changes, while treating every call as a mutator makes you expect changes that never happen.
For word = "learnyard", size() returns 9 because the string contains nine characters. The valid indices are 0 through 8, so the last valid index is always one less than the size. length() reports the same count as size(), and empty() reports false because word contains characters.
std::string word = "learnyard";
std::cout << word.size(); // 9
std::cout << word.length(); // 9
std::cout << word.empty(); // 0, meaning false
std::cout << word.front(); // l
std::cout << word.back(); // dfront() returns the character at the beginning, l, and back() returns the character at the end, d. Neither call removes or changes a character. An empty string has no first or last character, so calling front() or back() when empty() is true has undefined behavior in C++. Check emptiness before asking for either end.
For word = "learnyard", which set of results is correct for size(), the final valid index, front(), and back()?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
push_back() is a mutator: it adds one character to the end of the existing string. Starting with word = "learnyard", word.push_back('.') changes word to "learnyard.". The call does not return the added character for you to store. It changes the variable directly.
std::string word = "learnyard";
word.push_back('.');
std::cout << word; // learnyard.
if (!word.empty()) {
word.pop_back();
}
std::cout << word; // learnyardpop_back() is also a mutator. It removes the final character from word, so it restores "learnyard" after the period is added. pop_back() returns no character to the caller. Calling it on an empty string tries to remove a character that does not exist, which is undefined behavior in C++. The emptiness check prevents that invalid operation.
Starting from word = "learnyard", type the final value after push_back('.'), pop_back(), and pop_back().
word.push_back('.');
word.pop_back();
word.pop_back();Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
After word is "learnyard", find("yard") returns 5 because the match begins at index 5. The call reports a position but does not alter word. substr(5, 4) starts at index 5 and takes 4 characters, returning a separate string, "yard". The source remains "learnyard" after both calls.
std::string word = "learnyard";
std::size_t start = word.find("yard");
std::string part = word.substr(5, 4);
std::cout << start; // 5
std::cout << part; // yard
std::cout << word; // learnyardA search can fail. word.find("java") returns std::string::npos, a special value meaning that no match was found. Do not use that failed result as an index. Check it before indexing or passing it as the start position for another operation. For this word, find("java") fails, while word itself remains "learnyard".
std::size_t missing = word.find("java");
if (missing != std::string::npos) {
std::cout << word[missing];
}