diff --git a/Writerside/topics/C-Programming.md b/Writerside/topics/C-Programming.md index 8053ea8..9f5cfbf 100644 --- a/Writerside/topics/C-Programming.md +++ b/Writerside/topics/C-Programming.md @@ -687,7 +687,7 @@ really have to!

#### 4.4 Output Streams -

Cerr & Clog

+

cerr & clog

#include <iostream> @@ -791,20 +791,20 @@ int main() {
  • -

    When a type name is too long and a simpler alias makes the -code more readable, use it.

    +

    When a type name is too long and a simpler alias makes the + code more readable, use it.

  • -

    In libraries there is a common name for a type within each -class. Example:

    - -
  • -

    vector::iterator, map::iterator, string::iterator

    -
  • -
  • -

    vector::reference, map::reference, string::reference

    -
  • -
    +

    In libraries there is a common name for a type within each + class. Example:

    + +
  • +

    vector::iterator, map::iterator, string::iterator

    +
  • +
  • +

    vector::reference, map::reference, string::reference

    +
  • +
    @@ -820,85 +820,92 @@ class. Example:

    Remember to include < utility > and < tuple >

    -```C++ - std::pair price(3.4, 5); - - // make_pair/tuple (C++ 11) automatically deduces the type! - auto prices = std::make_pair(3.4, 5); - auto values = std::make_tuple(3, 4, "hi"); - - // access via get/set - prices.first = prices.second; // prices = {5, 5} - get<0>(values) = get<1>(values); // values = {4, 4, "hi"} +

    Examples:

    - // structured binding (C++ 17) - extract each binding - auto [a, b] = prices; // a = 5, b = 5 - const auto& [c, d, e] = values; // c = 4, d = 4, e = "hi" -``` + +std::pair<double, int> price(3.4, 5); +\/ +// make_pair/tuple (C++ 11) automatically deduces the type! +auto prices = std::make_pair(3.4, 5); +auto values = std::make_tuple(3, 4, "hi"); +\/ +// access via get/set +prices.first = prices.second; // prices = {5, 5} +get<0>(values) = get<1>(values); // values = {4, 4, "hi"} +\/ +// structured binding (C++ 17) - extract each binding +auto [a, b] = prices; // a = 5, b = 5 +const auto& [c, d, e] = values; // c = 4, d = 4, e = "hi" + #### 5.3 Conversions -```C++ -int v1 = static_cast(3.14); -double v2 = 6; -``` +

    Examples:

    -```C++ + +int v1 = static_cast<double>(3.14); // v1 = 3 + + + const int v3 = 3; -int* v4 = const_cast (&v3); -``` +int* v4 = const_cast<int*> (&v3); // v4 = 3 + #### 5.4 initializer_list -

    Definition: An initializer +

    Definition: An initializer list is a lightweight vector that can be used as a parameter.

    -```C++ -#include -#include -#include +

    Example:

    + +#include <iostream> +#include <vector> +#include <initializer_list> +\/ class MyContainer { private: - std::vector data; - + std::vector<int> data; +\/ public: // Constructor using initializer_list - MyContainer(std::initializer_list values) { + MyContainer(std::initializer_list<int> values) { // Iterate through the initializer_list and populate the vector for (int value : values) { data.push_back(value); } } - +\/ void print() const { for (int value : data) { - std::cout << value << " "; + std::cout << value << " "; } - std::cout << std::endl; + std::cout << std::endl; } }; - +\/ int main() { // Using initializer_list to initialize MyContainer - MyContainer container1 = {1, 2, 3, 4, 5}; - container1.print(); - + MyContainer container1 = {1, 2, 3, 4, 5}; + container1.print(); +\/ MyContainer container2{6, 7, 8}; container2.print(); - +\/ return 0; } -``` + -

    C++ 11 provides a uniform initialization syntax. Using the uniform + +

    C++ 11 provides a uniform initialization syntax. Using the uniform initialization syntax, the initializer list constructor is preferred -over constructor.

    +over constructor.

    + -```C++ -std::vector v1(3, 10) // v1 = {10, 10, 10} -std::vector v2{3, 10} // v2 = {3, 10} -``` + +std::vector<int> v1(3, 10) // v1 = {10, 10, 10} +std::vector<int> v2{3, 10} // v2 = {3, 10} + ## Ⅱ Standard Template Library (STL)