文章目錄
0 前言
筆者兩年前用的C++,後來發現python這種神器,用的python較多,且用於刷leetcode,現在因爲工作原因重新拿起了C++,話說兩年前立志要把那本C++primer看完的決心又可以啓程了,廢話不多說,以下是刷leetcode時,用到C++的一些庫函數,也是基礎,是自己的筆記,但也是刷題必備呀有木有,簡直如虎添翼,不然跟C有啥區別的。
1 vector—基礎結構
1.1 初始化
#include <vector>
std::vector<int> newList;
std::vector<int> newList={1,2,3};
std::vector<int> newList(1,2);
1.2 預設空間
std::vector<int>test3;
test3.reserve(2); # 預設兩個位置
test3.resize(2,1); # 在test3的前兩個位置賦值-2個1,如果有值則不賦
1.3 erase用法
https://blog.csdn.net/u013654125/article/details/77321081
vector<int> iVec;
for (auto it = iVec.begin(); it != iVec.end();) {
if (*it % 3 == 0)
it = iVec.erase(it); //刪除元素,返回值指向已刪除元素的下一個位置
else
++it; //指向下一個位置
}
1.4 求最大值
https://blog.csdn.net/qxconverse/article/details/67638545
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> a = { 2,4,6,7,1,0,8,9,6,3,2 };
auto maxPosition = max_element(a.begin(), a.end());
cout << *maxPosition << " at the postion of " << maxPosition - a.begin() <<endl;
//cout << a[maxPosition - a.begin()] << " at the postion of " << distance(a.begin(), maxPosition) << endl;
system("pause");
return 0;
}
2 unordered_set/set—迅速找到某個值
https://blog.csdn.net/zhang14916/article/details/100859487
類似與python的set集合,有find的方法,另外有個特性就是裏面的元素沒有重複的。
2.1 初始化
#include <unordered_set>
std::unordered_set<std::string> c{"aaa"};
2.2 find
a.find(“eeee”):查找元素"eeee",返回結果爲a.end()則表明沒有找到,否則返回所對應元素。
- 注:這裏有個問題?set結構遍歷別亂用
unordered_set<int> set;
for (int i = 0; i < 10; i++) {
set.insert(set[i]);
}
for (unordered_set<int>::iterator i = set.begin(); i != set.end(); i++) {
cout << *i << endl;
}
cout << " find 3: " << *set.find(3) << endl;
cout << "count 5:" << set.count(5) << endl;
3 unordered_map—字典
https://blog.csdn.net/u012604810/article/details/79798082
https://leetcode-cn.com/problems/subarray-sums-divisible-by-k/solution/he-ke-bei-k-zheng-chu-de-zi-shu-zu-by-leetcode-sol
3.1 初始化
#include <unordered_map>
unordered_map<int, int> record = {{0, 1}};
- 注:結構裏沒有的key的值都爲0
3.2 find函數
iterator find ( const key_type& key );
如果key存在,則find返回key對應的迭代器,如果key不存在,則find返回unordered_map::end。因此可以通過
map.find(key) == map.end()來判斷,key是否存在於當前的unordered_map中。
3.3 Count函數
size_type count ( const key_type& key ) const
count函數用以統計key值在unordered_map中出現的次數。實際上,c++ unordered_map不允許有重複的key。因此,如果key存在,則count返回1,如果不存在,則count返回0.
4 string—字符串
https://blog.csdn.net/wallwind/article/details/6827863
4.1 初始化
#include <string>
string s(1, '2');
4.2 find
s.find(‘a’) 字符串 s查詢’a’字符或字符串 返回所在的位置
s.find(‘a’, n) 字符串s從n下標開始查詢’a’字符或字符串 返回所在的位置
如果找不到返回 string::npos(-1)
4.3 切片操作(python說法)
String.substr(first_idx, nums) # 從first_idx索引開始nums個字符
4.4 字符串、數字互轉
std::cout<<std::to_string(3)<<"\n";
std::cout<<std::stoi("123")<<"\n";
4.5 瞬間逆序
std::cout<<"======string====="<<"\n";
std::stringstr("asgdasfd");
std::strings(str.rbegin(),str.rend());
std::cout<<s<<"\n";
5 stack/queue—棧和隊列
5.1 用法
#include <stack>
#include <queue>
std::cout << "====stack=====" << "\n";
std::stack<int> test;
test.push(2);
test.push(5);
test.push(4);
int sdfdf = test.top();
std::cout << test.top() << "\n";
std::cout << "====queue=====" << "\n";
std::queue<int> t;
t.push(1);
std::cout << t.front() << "\n";
std::cout << t.back() << "\n";
注:均不可遍歷、不可賦值初始化
6 deque—雙向隊列(力薦)
個人感覺比較無敵,可以賦值初始化,遍歷,兩邊可進,可出。
std::cout << "========deque==========" << "\n";
std::deque<int> testD(6,3);
for (int i = 0; i < testD.size(); i++){
std::cout << testD[i] << " ";
}
(栗子待補)
7 排序
https://blog.csdn.net/w_linux/article/details/76222112
#include <iostream>
#include <algorithm>
boolmyfunction2(inti, intj)
{ return (i > j); }//降序排列
int main(){
int a[20] = {2, 4, 1, 23, 5, 76, 0, 43, 24, 65}, i;
for (i = 0; i < 20; i++)
cout << a[i] << endl;
sort(a, a + 20);
for (i = 0; i < 20; i++)
cout << a[i] << endl;
std::vector<int> a = {2, 4, 1, 23, 5, 76, 0, 43, 24, 65};
int i;
for (i = 0; i < 10; i++)
std::cout << a[i] << std::endl;
# std::sort(a.begin(), a.end()); 默認兩個參數升序排序
std::sort(a.begin(), a.end(), myfunction2);
for (i = 0; i < 10; i++)
std::cout << a[i] << std::endl;
return 0;
}
8 正無窮和負無窮
求數組的max和min的時候都需要假設初始值爲正無窮或者負無窮,當然也可以隨意從數組中取一個值作爲初始值,通常取第一個。
在c++中怎麼表示正無窮和負無窮呢?
如果是int,可以用INT_MAX表示正無窮,INT_MIN表示負無窮,需要包含limits.h。
如果是double,可以用DBL_MAX表示正無窮,-DBL_MAX表示負無窮(注意不是DBL_MIN),需要包含float.h。
9 最大最小值
9.1 表達
std::max
std::min
9.2 注意
只能比較兩個元素