其他分享
首页 > 其他分享> > 0224——LC开心

0224——LC开心

作者:互联网

BONUS:

1.树的深度和高度:

数楼高度:从下往上数

看水塘的深度:用木棍从上插进去看深度

 

对C++STL很好的归结网址:

https://blog.csdn.net/QW_sunny/article/details/80908647

 cmp比较的例子:

#include<bits/stdc++.h>
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
struct cmp1
{
    bool operator() (const int&a,const int&b)
    {
        return a>b;
    }
};
int main()
{
    vector<int> tmp={1,1,3,12,32,213,7,1,2};
    sort(tmp.begin(),tmp.end(),cmp);
    priority_queue<int,vector<int>,greater<int>> p;
    p.push(1);
    cout<<p.top();
    return 0;
}

set相关函数及用法尤其是lower_bound等algorithm用法进行了实践总结

#include<bits/stdc++.h>
using namespace std;
//STL special case
template <class T>
void print(T a)
{
    for(auto i:a)
    {
        cout<<i<<endl;
    }

}

template <class T>
void p(T a)
{
    cout<<a<<endl;
}

/*
1.set
size
clear

*/
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    priority_queue<int,vector<int>,cmp> p={1,21,242,14,34,13,241,3124,21};

    //set part
    set<int> exp1={1,3};
    multiset<int> exp2={2,2,3,3,4,4,8};

    p("SET PART:tree most logn");
    print(exp1);

    //size O(1)
    p("size part");
    p(exp1.size());

    //count O(logn+an)
    p("count part");
    p(exp1.count(3));

    //clear
    p("clear part");
    exp1.clear();
    print(exp1);

    p("\ninsert part O(logn)");
    exp1.insert(3);
    exp1.insert(4);
    print(exp1);

    p("iterator part O(1)");
    auto it=exp1.begin();
    p(*it);
    it++;
    p(*it);

    //begin end
    p("\nbegin() end() O(1)");
    p(*(--exp1.end()));

    p("\nerase(para:val/iter) O(logn)");
    exp1.erase(3);
    print(exp1);
    p("\nmultiset will erase all val equals to para,so if only delete one,use combine find\n");
    print(exp2);
    exp2.erase(exp2.find(2));
    print(exp2);
    p("find() return iterator");
    if(exp1.find(7)==exp1.end())
    {
        p("not find 7 in exp1");
        exp1.insert(7);
        if(exp1.find(7)!=exp1.end())
            p("7 now exist in exp1");
    }

    p("lower_bound & upper_bound & equal_range");
    auto it1=lower_bound(exp1.begin(),exp1.end(),7);
    auto it2=exp1.upper_bound(7);
    p("set base on tree,so no order/rank,only can insert in begin/end");
    exp1.insert(exp1.begin(),8);
    exp1.insert(exp1.end(),10);
    print(exp1);

    if(it2==exp1.end()) p("no val in exp1 big than 7");
    p(*it1);

    p("\nequal_range== .first:lower_bound .second:upper_bound");
    auto it3=exp1.equal_range(7);
    p("\nfirst");
    p(*it3.first);
    p("\nsecond");
    p(*it3.second);

    p("\nbinary_search part");
    bool ans=binary_search(exp1.begin(),exp1.end(),7);
    p(ans);

    p("\n min_element & big_element");
    p("min");
    auto it4=min_element(exp1.begin(),exp1.end());
    p(*it4);
    auto it5=max_element(exp1.begin(),exp1.end());
    p("max:");
    p(*it5);
    p("struct cmp self-define");

    //self-define cmp part
    p("self-define cmp part");


    //struct part
    p("struct part class part:aim to reality problem virtualization");
    struct Worker
    {
        int id;
        int cash;
        string name;
    };
//    bool cmp(Worker& a,Worker& b)
//    {
//        if(a.id==b.id)
//            return false;
//        return a.cash>b.cash;
//    };
    p("\nstruct initial method");
    /*first initial method:(initial list)
        initial in order,is former no ini,latter cannot
    */
    Worker wk1={0,4000,"Bill"};
    Worker wk2={1,3500};

    /*second initial method:(initial function)
        need ; in the last.
        except for main always need ; to tell compiler end msg.
    */
    struct hope
    {
        int num;
        vector<string> actual;
        hope(int num,vector<string> actual)
        {
            this->num=num;
            this->actual=actual;
        }
    };
    struct Worker2
    {
        int id;
        int val;
        string name;
//        hope hp1;
        Worker2(int id,int val,string name,hope hp1)
        {
            this->id=id;
            this->val=val;
            this->name=name;
//            this->hp1=hp1;
        }
    };
    vector<Worker2> store1;
    vector<string> wk3hl={"money","house","car"};
    hope wk3h(3,wk3hl);
//**************************************Question:how to initialize nested struct**********************
//    Worker2 wk3(3,4500,"Julio");
//    store1.push_back(wk3);

    //struct & rank method usage
    set<Worker2> store2;


    vector<int> test1={1,9,3,4,5,20,4,53,7};
    sort(test1.begin(),test1.end());
    print(test1);
    sort(test1.begin(),test1.end(),cmp);
    print(test1);






    return 0;

}

 

 

 

标签:开心,begin,end,LC,int,exp1,0224,part,print
来源: https://www.cnblogs.com/Marigolci/p/12359026.html