5. Study Note of Object Oriented Programming (OOP): STL II

STL With Overloading

Overview:

In this section, we did not discuss new STL data type. But we covered three ways of initializing data member of a class: default constructor, constructor with values, and initializer_list. Previous two types have been discussed in last section, this time the mainly explain the last one. Note there, the priority of three ways would be: default < constuctor < initializer_list.

Also, we also discuss two operator overloading: operator< and operator==. operator< usually for sorting, while operator== usually for find.

Initializing Data Member of A Class

Class Declaration:
class ThreeD{
public:
    int ht;
    int wid;
    int dep;

    // default constructor
    ThreeD() {
        ht = wid = dep = 0;
    }
    // know knid of constructor
    ThreeD(int i, int j, int k): ht(10*i), wid(10*j), dep(10*k) {}
    // highest priority
    ThreeD(const initializer_list<int>& I);
    int getVol() const { return ht * wid * dep; }

    bool operator<(const ThreeD& t) const { return getVol() < t.getVol();}
    bool operator==(const ThreeD& t) const{  return getVol() == t.getVol();}
};
Initializer_list definition
// highest priority
ThreeD::ThreeD(const initializer_list<int>& I){
    auto it = I.begin();
    ht = *it * 100;
    it++;
    wid = *it * 100;
    it++;
    dep = *it * 100;
}

Complete Code:

// initializing data member of a class
// default < constuctor < initializer_list
// operator< usually for sorting
// operator== usually for find
#include <iostream>
#include <list>
#include <vector>
#include <map>

using namespace std;

class ThreeD{
public:
    int ht;
    int wid;
    int dep;

    // default constructor
    ThreeD() {
        ht = wid = dep = 0;
    }
    // know knid of constructor
    ThreeD(int i, int j, int k): ht(10*i), wid(10*j), dep(10*k) {}
    // highest priority
    ThreeD(const initializer_list<int>& I);
    int getVol() const { return ht * wid * dep; }

    bool operator<(const ThreeD& t) const { return getVol() < t.getVol();}
    bool operator==(const ThreeD& t) const{  return getVol() == t.getVol();}
};

// highest priority
ThreeD::ThreeD(const initializer_list<int>& I){
    auto it = I.begin();
    ht = *it * 100;
    it++;
    wid = *it * 100;
    it++;
    dep = *it * 100;
}

class Node{
public:
    int value;
    Node* next;
    Node(){ next = nullptr; }
    Node(int i ):value(i), next(nullptr){}
};


class LinkedList {
public:
    Node* head;
    LinkedList(): head(nullptr){};
    LinkedList(const initializer_list<int>& I );
    void printList();
};
LinkedList::LinkedList(const initializer_list<int>& I ){
    head = nullptr;
    auto it = I.end() - 1;
    while(it != I.begin()-1 ){
        Node* p = new Node(*it);
        p->next = head;
        head = p;
        it--;
    }
}

void LinkedList::printList(){
    Node* p = head;
    while(p){
        cout << p->value << "->" ;
        p = p->next;
    }
    cout << endl;

}


ostream& operator<<(ostream& str, const ThreeD& T){
    str << "[" << t.ht << ", " << t.wid << ", " << t.dep << "]";
}

int main(){

    int A[5]{1 }; // {1, 0, 0, 0, 0}


    // t1(3,4,5) call the constructor                       ===> [30, 40, 50]
    // however, t2{10, 20, 30} call the initializer_list    ===> [1000, 2000, 3000]
    ThreeD t1(3,4,5), t2{10,20,30}, t{1,2,6};
    cout << t1 << " " << t2 << " " << t3 << endl;
 
    list<int> L1{7, 4,3,5,1};
    auto it11 = find(L1.begin(), L2.end(), 5);

    L1.sort();
    for(auto i : L1){
        cout << i << " ";
    }
    cout << endl;

    list<ThreeD>  L2{t1, t2, t3};
    //need to overload operator=
    auto it12 = find(L2.begin(), L2.end(), t2);
    L2.sort();
    for(auto i : L2) {
        cout << i << " ";
    }
    cout << endl;

    map<int, string> M1{ {25,"Dave"}, {11, "Nancy"}, {14, "Mary"} };
    // find is a member fucntion for map, but not for list or vector
    auto it13 = M1.find(25);

    for(auto i : M1) {
        cout << i.first << " " << i.second << "     ";
    }
    cout << endl;
    
    map<ThreeD, string> M2{ {t1, "Dave"}, {t2, "Nancy"}, {t3, "Mary"} };
    for(auto i : M2) {
        cout << i.first << " " << i.second << "     ";
    }
    cout << endl;

    LinkedList L5 { 4,5,6,7,8 };
    L5.printList();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值