[C++][Solution]ch.19 Drill 1.~14.

自己写的 书上 简单练习 的答案

书是这本书 :《C++ 程序设计原理与实践 第二版》 《Programming Principles and Practice Using C++ 》(Second Edition)[Bjarne Stroustrup] [1]

(中文) 第14章 向量 、 模板 和 异常 CHAPTER 19 Vector, Templates, and Exceptions

简单练习 1~14题 答案 Drill 1.~14. Solution

note 说明

  • 编程语言 : C++
  • 概念关键词: 模板 template、运算符重载<< >> =、保留字friend;
  • 简单练习这14道小题,就是14个步骤教你怎么完成这道题,这道题主要用来练习怎么使用template

SOLUTION 答案

code 源码

#include 
#include 
using namespace std;

template struct S
{
    S(T v) : val{v} {}
    T& get();
    const T& get() const;
    
    void set(const T&);
    void operator=(const T&);
    
    friend istream& operator>>(istream& ist, S& ss)
    {   
        return ist >> ss.val;
    } 
    
    friend ostream& operator<<(ostream& ost, S& ss)
    {   
        return ost << ss.val;
    }
    
    
 
private:
        T val;
};


template 
T& S::get() 
{   return val; }

template
const T& S::get() const
{   return val; }

template
void S::set(const T& t)
{   val = t;    }

template
void S::operator=(const T& t)
{   val = t;    }

template
read_val(T& v) 
{
    cin.clear();
    cin >> v;       
}

template
print_val(T& v)
{
    cout << v;
}

/*
    {val,val,val,val}
*/
template
istream& operator>>(istream& ist, vector& v) 
{
    char c1, c2;
    ist >> c1;
    if(c1 != '{') cout << "Invalid format";
    
    T t;
    while(ist >> t && ist >> c2) {
        v.push_back(t);
    }
    return ist; 
}

template
ostream& operator<<(ostream& ost, vector& v)
{
    for(int i = 0  ; i < v.size();++i)
    {
        if(i == 0 ) ost << "{";
        ost << v[i];    
        if(i < v.size() -1) ost << ",";
        else ost << "}";
    }
    return ost;
}


int main()
{ }

test code in main function 测试用代码

    S s1(1);
    S s2('a');
    S s3(3.14);
    S s4("Hello");
    S> s5(vector{1,2,3});
    
    cout << s1.get() << "\n";
    cout << s2.get() << "\n";
    cout << s3.get() << "\n";
    cout << s4.get() << "\n";
    
    vector v = s5.get();
    for(int vv : v)
        cout << vv << " ";
    cout << "\n";
    
    s1.set(2);
    cout << s1.get();
    cout << "\n";
    
    cout << s1.get() << "\n";
    
    s1 = 3;
    cout << s1.get();
    cout << "\n";
    
    const S s6("const version of get()");
    cout << s6.get(); 

    cout << "int: \n";
    int v_int;
    read_val(v_int);
    S s_int(v_int);
    cout << s_int.get() << "\n";
    
    cout << "char: \n";
    char v_char;
    read_val(v_char);
    S s_char(v_char);
    cout << s_char.get() << "\n";
    
    cout << "double: \n";
    double v_double;
    read_val(v_double);
    S s_double(v_double);
    cout << s_double.get() << "\n";
    
    cout << "string: \n";
    string v_string;
    read_val(v_string);
    S s_string(v_string);
    cout << s_string.get() << "\n";

    cout << "\n";
    cout << "int:\n";
    vector v_int;
    read_val(v_int);
    print_val(v_int);
    
    cout << "\n";
    cout << "double:\n";
    vector v_double;
    read_val(v_double);
    print_val(v_double);
    

    vector a;
    S> s(a);
    read_val(s);
    print_val(s);

example 举例

Q: 如何使用test code ? A : 复制到 int main() {}

int main()
{
    vector a;
    S> s(a);
    read_val(s);
    print_val(s);
}

输入输出 input output Dev-C++ 5.11

[C++][Solution]ch.19 Drill 1.~14._第1张图片
[C++][Programming][Solution]ch.19 Drill 1.~14. Output Dev-C++ 5.11
{1,2,3,4}
Ctrl+Z
{1,2,3,4} 
  • Dev-C++ 5.11
  • 键盘输入{1,2,3,4}
  • 重载运算符>>,读取数据格式为 {val,val,val,val}的输入数据(val个数任意);
  • 输入Ctrl+z停止输入;
  • 输出格式与输入格式一致,利用了重载运算符<<
  • 如果定义Vector类型,输入数据请使用 { hello , world },添加空格;

关于代码 My Code

模板、 返回引用

template 
T& S::get() 
{   return val; }
  • 模板是模板,写在前面,写在上面,就是这句 template;
  • 返回引用([2],[3]),就是在返回类型后加个 & 符号, 结合模板就是写成 T& ;
  • T 简单说就是随便什么类型,比如int这个关键词可以放的地方, T就可以放在那里;
  • S告诉S这个struct 你这次用的类型叫T ,还有 S这个名字是我们自己取的;
  • S::get() 中的双冒号::才可以呼应写在struct S{};里面的那个 get()定义;

reference 参考

[1] Programming -- Principles and Practice Using C++ (Second Edition) http://www.stroustrup.com/Programming/
[2] Is the practice of returning a C++ reference variable, evil?
https://stackoverflow.com/questions/752658/is-the-practice-of-returning-a-c-reference-variable-evil
[3] C++ Return by Reference https://www.programiz.com/cpp-programming/return-reference

你可能感兴趣的:([C++][Solution]ch.19 Drill 1.~14.)