Terminology (4)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Standard library
- Library facilities supplied as part of a language’s implementation or specified library environment.
- API contract
- The operation’s documented accepted inputs, matching rules, returned or changed values and failure behavior.
- Dependency manifest
- Project configuration naming required packages and the native resolver’s requirements.
- Resolved dependency
- The particular dependency implementation selected by the project’s resolver and configuration.
Using a library means choosing a contract
A library supplies reusable operations. Standard-library facilities come with the language implementation or runtime; a third-party dependency needs its own project setup. Importing a name selects code to use, while dependency resolution makes the required version available to the project.
The operation’s contract specifies its inputs, result and effects. For text processing, check whether it works on characters, substrings or whole items, how many matches it changes, and whether it returns new text or changes existing storage. Compose those choices around the application’s intended unit of change.
C++ standard-library declarations are supplied through the appropriate headers. A third-party library may also require include paths and linker inputs configured by the project’s build system. Including a header alone does not supply every required compiled definition.
01Label input
Read the labels people actually entered
A community noticeboard imports a line of labels, removes an optional tag: marker and applies an editor’s rename table. A tab should separate labels; a repeated marker should leave the second copy as data. A rename must keep its original target even when another rule uses the destination name.
The first line arrives from the noticeboard’s text entry. Before choosing a word-reading operation, compare what a literal space delimiter and a whitespace-word operation report for leading spaces, an interior tab and trailing spaces. This reduced input uses ASCII whitespace.
#include <iostream>
#include <sstream>
#include <string>
int main() {
const std::string text = " north\tsouth ";
std::istringstream literal(text);
std::istringstream words(text);
std::string word;
int fields = 0;
int count = 0;
while (std::getline(literal, word, ' ')) {
++fields;
}
while (words >> word) {
++count;
}
std::cout << fields << " " << count << "\n";
}
clang++ -std=c++2c -Wall -Wextra -pedantic main.cpp -o program && ./program
Save each complete example in a fresh folder under its selected filename and use the guarded command. The TypeScript workflow uses local TypeScript and Node types with ES2022/Node16 CommonJS emission; set package.json’s type field to commonjs, preserving its other fields. Rust uses edition 2024. These programs use native standard libraries only. The dependency commands describe separate local project setup; the browser runner does not install packages.
02Optional markers
Decide what counts as a leading marker
The board uses one exact leading marker to flag entered labels. These probes try related native operations on a repeated marker and an unmarked or interior occurrence. Predict the results before comparing their matching rules with the board’s policy.
#include <iostream>
#include <string>
int main() {
for (std::string label : {"pre:pre:wing", "pepper"}) {
label.erase(0, label.find_first_not_of("pre:"));
std::cout << "[" << label << "]\n";
}
}
03Rename requests
Keep renames attached to their original labels
Editors also rename labels. This reduced message contains oak alone, inside oakwood and again at the end. Observe the library’s substring operations before deciding how to apply a table of complete-label changes.
#include <iostream>
#include <string>
int main() {
std::string message = "oak oakwood oak";
const auto at = message.find("oak");
if (at != std::string::npos) {
message.replace(at, 3, "elm");
}
std::cout << message << "\n";
}
04Practice
Repair the noticeboard label desk
The label desk must read the entered words, interpret one optional leading marker and apply an editor’s simultaneous rename table. Each supplied complete file keeps earlier correct helpers and the input reader in place while you repair the indicated operation.
Three uses of one label pipeline
Read the entered labels, preserve marker data and reconcile complete-label changes against the original board.
Sign in to keep your read marks on every device. Sign in
Was this helpful?