编程常用(c++ & python)

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) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值