LeetCode刷題小結之vector的使用

2021-08-15 09:19:18 字數 776 閱讀 8070

vector

1.特點:vector是c++標準模板庫中的內容,它具有可變長度、可訪問任意資料型別的特點,使用vector可以構建類似多維陣列的資料結構。

2.常用方法:

#include #include #include using namespace std;

int main()

; test.insert(test.begin(), list, list + 3); //vector中插入陣列

find(test.begin(), test.end(), 3); //在vector中查詢元素3是否存在,若存在返回指定迭代器;不存在返回test.end()

test.clear(); //清空test中元素

test.empty(); //test是否為空

}

3.自定義型別使用vector:

#include #include #include #include using namespace std;

struct student

student ():id(0), name(""){}

};bool cmpasc(const student& s1, const student& s2)

bool cmpdes(const student& s1, const student& s2)

int main()

參考:c++ vector

0710,0711 leetcode刷題小結

暑期目標 刷100題 其中 簡單題記0.5題 中等題記0.8題 困難題記1.5題 題目數量 簡單題 2 進度 1 100 231 2的冪,判斷乙個數是否為2的冪 我的解法 使用位運算 把輸入與1求 並加在count中 即,若為1 則 1,若為0,則不變 把輸入右移一位 繼續重複上一步,直至該數為0 ...

Leetcode刷題之括號

給定乙個只包括 的字串,判斷字串是否有效。有效字串需滿足 左括號必須用相同型別的右括號閉合。左括號必須以正確的順序閉合。注意空字串可被認為是有效字串。示例 1 輸入 輸出 true 示例 2 輸入 輸出 true 示例 3 輸入 輸出 false 示例 4 輸入 輸出 false 示例 5 輸入 輸...

leetcode刷題之堆

今天終於開啟的第二個專題的刷題之旅堆,不過第乙個專題棧還有乙個小問題沒解決就是利用遞減棧去解決接雨水的問題,雖然那道題我用動態規劃的問題解決出來了,我記得看到過一道面試題,問棧和堆有什麼區別。通過搜尋網上的資料總結如下。棧 stack 由系統分配記憶體,速度較快,但是自己無法掌握。堆 一般用兩種方法...