vector & list
c++
#include<vector>
/* 声明、初始化 */
vector<int> a(10, 0);
vector<int> b(a);
vector<int> c(b.begin(), b.begin()+3);
vector<int> d(array, array+3);
/* 访问和读取 */
//s1
for(int i=0; i<a.size(); i++){
std::cout << a[i];
}
//s2
for(vector<int>::iterator it=a.begin(); it!=a.end(); it++){
cout << *it;
}
/* 常用函数 */
a.push_back();
a.pop_back();
a.insert(a.begin()+3, 5);
a.erase(a.begin()+3, a.begin()+5);
/* 常用算法 */
#include<algorithm>
sort(a.begin(), a.end(), cmp);
reverse(a.begin(), a.end());
find(a.begin(), a.end(), 5)
python
# 声明与赋值
l1 = []
l2 = l1 # 软
l2 = l1[:] # 硬
# 访问和读取
for n,l in enumerate(list):
print(l)
# 常用函数
len(list)
max(list)
min(list)
list.append(obj)
list.insert(index, obj)
list.pop([index=3])
del list[index]
list.remove(obj)
list.sort(cmp, key, reverse)
list.index(obj)
list.count(obj)
map & dict
c++
#include<map>
/* 声明 */
map<string,int> mp;
/* 访问 */
for(map<string,int>::iterator it=mp.begin(); it!=mp.end(); it++){
std::cout << it->first << it->second;
}
/* 插入 */
mp.insert(pair<string,int>("age", 3));
mp.insert(map<string,int>::value_type("age, 3"));
/* 查找 */
iter = mp.find(obj);
/* 删除 */
mp.erase(obj);
mp.erase(iter);
mp.erase(mp.begin(), mp.begin()+3);
python
# 初始化
dic = {}
# 常用函数
len(dic)
del dic['age']
pop(key)
dic.copy()
dict.keys()
dict.values()
rand & random
c++
srand() // seed
rand() // [0, 32767]
rand()%(a-b)+a // [a, b]
python
import random
random.random() # [0, 1.0)
random.uniform(a, b) # [a, b]
random.randint(a, b) # [a, b]
queue
c++
#include<queue>
/* 声明 */
queue<int> q;
/* 队列调整 */
q.push();
q.pop();
q.front();
q.back();
/* 常用函数 */
q.empty();
q.size();
python
import queue
# 初始化
q = queue.Queue(maxsize = 0)
# 队列调整
q.put()
q.get()
q.peek() # 队头项
q.add() # 队尾添加
q.clear()
# 常用函数
q.isEmpty()
len(q)
str(q)
stack
c++
#include<stack>
/* 声明 */
stack<int> s;
/* 栈调整 */
s.push();
s.pop();
s.top();
/* 常用函数 */
s.empty();
s.size();
python
# 列表模拟栈的实现
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return len(self.items)==0
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
if not self.isEmpty():
return self.items[len(self.items)-1]
def size(self):
return len(self.items)