Use std::cin >> s and std::cout << s, as simple as handling an integer.
std::cin >> s ignore leading whitespaces? Does it read an entire line or just a sequence of non-whitespace characters? Do some experiments on it.std::getline(std::cin, s): Reads a string starting from the current character, and stops at the first '\n'.
'\n' consumed? Is it stored? Do some experiments.for loops.Example: Print all the uppercase letters in a string.
for (char c : s) // The range-based for loops
if (std::isupper(c)) // in <cctype>
std::cout << c;
std::cout << std::endl;
Equivalent way: Use subscripts, which is verbose and inconvenient.
for (std::size_t i = 0; i != s.size(); ++i)
if (std::isupper(s[i]))
std::cout << s[i];
std::cout << std::endl;
[Best practice] Use range-based for loops. They are modern, clear, simple, generic, and hence more recommended.
for loops in later lectures.
For a number x of any arithmetic type, std::to_string(x) returns a string representing it.
int ival = 42;
double dval = 3.14;
std::string s = std::to_string(ival) + std::to_string(dval);
std::cout << s << '\n'; // Possible output: 423.140000
std::stoi(s), std::stol(s), ...: Extracts the arithmetic value represented by s.
See this list.
<iostream>, std::cin >> x >> y, std::cout << x << y.std.<cxxx> instead of <xxx.h>.std::string
'\0'.s.size() returns the length. s.empty() returns whether s is empty.+ and += for concatenation. Use <, <=, >, >=, ==, != for comparison. Use = for copying.std::string
>> and << for IO, as well as std::getline.s[i] to access the elements.for loops to traverse a string.std::to_string and std::stoi, std::stol, ... for numeric conversions.std::string: https://en.cppreference.com/w/cpp/string/basic_string