1. 程式人生 > 實用技巧 >js老生常談之this,constructor ,prototype

js老生常談之this,constructor ,prototype

前言

\(\mathcal{STL}\)\(\mathcal{Standard}\) \(\mathcal{Template}\) \(\mathcal{Library}\) の簡稱,中文名標準模板庫

在C++標準中,STL被組織為下面的13個頭檔案:<algorithm><deque><functional><iterator><vector><list><map><memory.h><numeric><queue><set><stack>

<utility>

stack

標頭檔案:#include<stack>
定義:stack<int>s;
壓棧:s.push(x);
彈棧:s.pop();
訪問棧頂元素:x=s.top();
訪問棧中元素個數:x=s.size();

queue

標頭檔案:#include<queue>
定義:queue<int>q;
入隊:q.push(x);
出隊:q.pop();
訪問隊首元素:x=q.front();
訪問隊尾元素:x=q.back();

map

標頭檔案:#include<map>
定義:map<int,string>m;

(前者為下標型別,後者為儲存型別)
與陣列用法相同,示例:

#include<iostream>
#include<map>
using namespace std;
map<int,char>m;
int main()
{
    a[1]='q';
    a[2]='w';
    a[3]='q';
    for(int i=1;i<=3;++i)
        cout<<a[i];
    return 0;
}

vector

\(\mathcal{STL}\) 中的陣列,沒有確定容量,支援隨機訪問。
標頭檔案:#include<vector>


定義:vector<int>v;
插入元素:v.push_back(x);
訪問元素:x=v[y];

  • vector下標從\(0\)開始。
#include <bits/stdc++.h>    // <vector>
using namespace std;
vector<int>v;
int main(void)
{
    v.push_back(1);
    v.push_back(2);
    v.pop_back();
    for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
    return 0;
}

deque

雙端佇列,支援在兩端插入。
標頭檔案:#include<deque>
定義:deque<int>d;
在隊首插入元素:d.push_front(x);
在隊尾插入元素:d.push_back(x);
獲取佇列長度:x=d.size();
彈出隊首元素:d.pop_front();
彈出隊尾元素:d.pop_back();
清除所有元素:d.clear();

#include<deque>
#include<cstdio>
using namespace std;
deque<int>d;
int main()
{
    d.push_back(1);
    d.push_front(2);
    printf("size:%d\n",d.size());
    d.push_front(3);
    for(int i=1;i<=2;++i)
        printf("%d\n",d.front()),d.pop_front();
    d.clear();
    return 0;
}

優先佇列/堆

嗯對,優先佇列和堆在\(\mathcal{STL}\) 裡是一樣的。
標頭檔案:#include<queue>
小根堆/最小優先佇列定義:

priority_queue<int, vector<int>, greater<int> > q;

大根堆/最大優先佇列定義:

priority_queue< int >p;

插入元素:q.push(x);
彈出最大/小元素:q.pop();
訪問最大/小元素:x=q.top();

示例:

#include<vector>
#include<deque>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int n,x,y,ans;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;++i)
        scanf("%d",&x),q.push(x);
    for(int i=1;i<n;++i)
    {
        x=q.top();
        q.pop();
        y=q.top();
        q.pop();
        q.push(x+y);
        ans+=x+y;
    }
    printf("%d\n",ans);
    return 0;
}

The end.