String basics in C++
std::string owns its characters and hands you concatenation, search, and slicing as methods.
#include <string>
std::string order_label() {
std::string base = "vanilla";
std::string order = base + " swirl";
if (order.find("swirl") != std::string::npos) {
order += " cone";
}
return order.substr(0, base.size()) + ", size " +
std::to_string(order.size());
}
How it works
+joins strings into a new one;+=growsorderin place.findreturns a position, orstd::string::nposwhen the needle is missing.substr(0, n)copies a prefix;std::to_stringturns thesize()count into text.
Keywords and builtins used here
iforder_labelreturn
The run, in numbers
- Lines
- 11
- Characters to type
- 263
- Tokens
- 93
- Three-star pace
- 75 tpm
At the three-star pace of 75 tokens a minute, this run takes about 74 seconds.
Step 3 of 3 in Values & references, step 3 of 27 in Language basics.