cpp howto
grammar relative
compare between strings
cpp++ {cmd=true} const auto s1="hello world"; const auto s2="hello";
// equal by pointer ? s1 == s2
// equal by compare (char by char) s1.compare(s2) == 0 // equal !s1.compare(s2) // equal
s1.compare(s2) != 0 // not equal s1.compare(s2) // not equal
// contain s1.find(s2) != std::string::npos // contains s1.contains(s2) // contains, C++23
see:
- [Differences between C++ string == and compare()? - Stack Overflow](https://stackoverflow.com/questions/9158894/differences-between-c-string-and-compare)
- [Comparing two strings in C++ - GeeksforGeeks](https://www.geeksforgeeks.org/comparing-two-strings-cpp/)
### `optional`
cpp++ {cmd}
#include <string>
#include <functional>
#include <iostream>
#include <optional>
// optional can be used as the return type of a factory that may fail
std::optional<std::string> create(bool b) {
if (b)
return "Godzilla";
return {};
}
// std::nullopt can be used to create any (empty) std::optional
auto create2(bool b) {
return b ? std::optional<std::string>{"Godzilla"} : std::nullopt;
}
// std::reference_wrapper may be used to return a reference
auto create_ref(bool b) {
static std::string value = "Godzilla";
return b ? std::optional<std::reference_wrapper<std::string>>{value}
: std::nullopt;
}
int main()
{
std::cout << "create(false) returned "
<< create(false).value_or("empty") << '\n';
// optional-returning factory functions are usable as conditions of while and if
if (auto str = create2(true)) {
std::cout << "create2(true) returned " << *str << '\n';
}
if (auto str = create_ref(true)) {
// using get() to access the reference_wrapper's value
std::cout << "create_ref(true) returned " << str->get() << '\n';
str->get() = "Mothra";
std::cout << "modifying it changed it to " << str->get() << '\n';
}
}
see:
what's stdafx.h
(预编译头)
ref:
how to call C++ functions in C
ref: