After class questions of Chapter 2 of essential C + +

Time:2020-11-15

Exercises 2.2 and 2.3

Header file

#pragma once

#include<iostream>
#include<vector>

void Pentagonal(const std::vector<int> &inputs, int num);
inline bool IsValidNum(const int num, const std::vector<int> &v) {
    if (num <= 0 || num >= 1024 || num <= v.size()) {
        return false;
   }
   return true;
}

CC file

#include "practice_2_2.h"

void Pentagonal(std::vector<int> &inputs, int num) {
    //First judge whether num is legal
    if (!IsValidNum(num, inputs)) {
        return;
    }
    for (int i = inputs.size() + 1; i < num + 1;i++) {
        int value = i * (3 * i - 1) / 2;
        inputs.push_back(value);
    }
}

void PrintElements(const std::vector<int> * inputs) {
    for (int ele : *inputs) {
        std::cout << ele << std::endl;
    }
}

int main() {
    std::vector<int> v;
    Pentagonal(v, 10);
    Pentagonal(v, 20);
    PrintElements(&v);
    //Return 0 indicates that the program was successfully run
    return 0;
}

Exercise 2.4

Header file

#pragma once
#include<iostream>
#include<vector>

const std::vector<int> * GetPentagonal(int num);

int GetValueFromIndex(int index, const std::vector<int> * v);

CC file

#include "practice_2_4.h"
#include<string>

/*
    The reason why I started with the following is wrong because I just declared one
    Pointer to vector < int >, however, this pointer does not point to any
    Object, so if you directly call the - > size() function, you will definitely report an error
*/
// const std::vector<int> * GetPentagonal(int num) {
//// local static object pointer
//     static std::vector<int> * v;
//     if (num <= 0 || num > 1024) {
//         return v;
//     }
//     for (int i = v -> size() + 1; i < num + 1; i++) {
//         std::cout << "point" << i << std::endl;
//         v -> push_back(i * (3 * i - 1) / 2);
//     }
//     return v;
// }

const std::vector<int> * GetPentagonal(int num) {
    //A local static object initializes an object directly
    static std::vector<int> v;
    if (num <= 0 || num > 1024) {
        return &v;
    }
    for (int i = v.size() + 1; i < num + 1; i++) {
        v.push_back(i * (3 * i - 1) / 2);
    }
    return &v;
}

int GetValueFromIndex(int index, const std::vector<int> * v) {
    //Determine whether it is a null pointer
    if (v == 0) {
        return -1;
        std::cout << "Error : v is nullptr" << std::endl;
    }
    //Judge whether the index exceeds
    if (index >= v -> size()) {
        return -1;
        std::cout << "Error : index too large!" << std::endl;
    }
    return (*v)[index];
}

int main() {
    const std::vector<int> *v = GetPentagonal(10);
    //Determine whether it is a null pointer
    if (v == 0) {
        std::cout << "Error: v is nullptr" << std::endl;
    }
    std::cout << GetValueFromIndex(5, v) << std::endl;    
    //Make sure that the next null pointer is 0
    //Note that pointers can be compared to 0. In general, objects cannot be declared as pointers compared to 0
    std::string* check = nullptr;
    if (check == 0) {
        std::cout << "the value of nullptr is 0" << std::endl;
    }
    return 0;
}

Exercise 2.5 / 2.6

Header file

#pragma once
#include<iostream>
#include<vector>

int max(int a, int b);
float max(float a, float b);
std::string max(const std::string &a, const std::string &b);
int max(const std::vector<int> &v);
float max(const std::vector<float> &v);
std::string max(const std::vector<std::string> &v);
Int max (const int * arr()); // arrays can only be declared as pointers?
template <typename T> T TemplateMax(T a, T b);

CC file

#include "practice_2_5.h"
#include<string.h>

int max(int a, int b) {
    return (a > b) ? a : b;
}
float max(float a, float b) {
    return (a > b) ? a : b;
}
std::string max(const std::string &a, const std::string &b) {
    std::cout << "a:" << a << "," << "b:" << b << std::endl;
    int bigger = a.compare(b);
    return (bigger > 0) ? a : b;
}

template <typename T>
T TemplateMax(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    std::cout << max(1,2) << std::endl;
    std::cout << max(1.2f, 2.4f) << std::endl;
    std::cout << max("abc", "abcd") << std::endl;
    std::cout << max(110, 112) << std::endl;
    return 0;
}

Take out first

Recommended Today

Java’s functional programming is like this!

introduce Java language is an object-oriented programming language, which has nothing to do with functional programming. Starting with the lambda expression and other new features added from Java 8, Java also began to support functional programming. In functional programming, function is the first class citizen. Let’s take a look at the call before and after […]