跳转至

Types and streams II

Stream II

getline reads till the newline character and also consume the newline character.

cin >> will skip the whitespace character without consuming them.

Example

C++
1
2
3
4
5
6
istringstream iss ("16.9\n 24");
double val;
string line;
iss >> val; // val = 16.9
getline(iss, line); // line = "";
getline(iss, line); // line = " 24"

We will get empty at the first getline.

We can also do that:

C++
1
2
3
4
5
6
istringstream iss ("16.9\n 24");
double val;
string line;
iss >> val; // val = 16.9
iss.ignore();
getline(iss, line); // line = " 24"

Morden C++ types

auto

alt text

pair/tuple

alt text

```cppvoid pairAndTuple() { // make_pair/tuple (C++11) automatically deduces the type! auto prices = make_pair(3.4, 5); // pair auto values = make_tuple(3, 4, "hi"); //tuple

Text Only
1
2
3
4
5
6
7
8
9
prices.first = prices.second;    // prices = {5.0, 5}
get<0>(values) = get<1>(values);  // values = {4, 4, "hi"}

// structured binding (C++17) - extract each component
auto [a, b] = prices;
const auto& [x, y, z] = values;

cout << "a: " << a << ", b: " << b << endl;
cout << "x: " << x << ", y: " << y << ", z: " << z << endl;

} ```

struct

alt text

references

alt text