c++/python相关函数

C++

1.vector

vector是c++标准库中用于存储一串数据的容器。

# include <iostream>
# include <vector> //vector包
using namespace std;

int main()
{
    //创建 初始化
    vector<int> nums; //初始化存储int数据的空容器
    vector<int> nums(3); //初始化存储3个int数据的容器
    vector<int> nums(3, 1); //初始化存储3个int数据1的容器

    //获取容器大小
    size = nums.size();

    //判断是否为空容器
    nums.empty()

    //添加元素
    vector.push_back(2)

    //删除元素,删除最尾端元素
    vector.pop_back()

    //判断相等:需要两个容器内部的所有数据逐个比较;赋值:=,覆盖
}

c++使用vector创建二维数组的方法_Mocode的博客-CSDN博客_如何用vector创建二维数组

2.unordered_map

无序映射(Unordered maps)是用于存储键值映射值组合成的元素的关联容器,并允许基于其键快速检索各个元素。在unordered_map中,键值通常用于唯一地标识元素,而映射值是具有与该键关联的内容的对象。键的类型和映射的值可能会有所不同。

unordered_map内部实现了一个哈希表(也叫散列表,通过把关键码值映射到Hash表中一个位置来访问记录,查找的时间复杂度可达到O(1),其在海量数据处理中有着广泛应用)。因此,其元素的排列顺序是无序的。

# include <iostream>
# include <unordered_map> //头文件
# include <map>
# include <string>
using namespace std;

int main()
{
    //初始化
	unordered_map<int, int> maps1; 	//初始化空表
	unordered_map<int, string> maps2 = {{1, "aa"}, {2, "bb"}};	//利用括号赋值初始化 
	
	//遍历
	auto it = maps2.begin();
	while(it != maps2.end())
	{
		cout<<it->first<< it->second<<endl;
		it++;
	}
	
	//新增值
	maps2[2] = "nb";	//在2的位置插入值,如果有值则覆盖
	maps2.insert(pair<int,string>(3,"cc"));	//使用insert和pair插入
	
	//查找元素,根据键查找 
	auto it2 = maps2.find(2);
	if(it2 != maps2.end())
	{
		cout<<it2->first<<it2->second<<endl;
	} 
	auto it3 = maps2.find(3);
	if(it3 != maps2.end())
	{
		cout<<it3->first<<it3->second<<endl;
	} 
}

unordered_map使用详解_Blanche117的博客-CSDN博客_unordered_map

3.reverse

# include <algorithm>

//数组
reverse(a,a+n);//整个数组,n为长度
reverse(a+i,a+k);//翻转下标为i~(k-1)

//字符串
reverse(str.begin(), str.end());//翻转整个字符串
reverse(str.begin()+i,str.begin()+k);//翻转下标为i~(k-1)

//向量
reverse(vec.begin(),vec.end());

4.int <-->  string

#include <string>
int i;
string str;
str = to_string(i);

long long num = stoi(str);

5.string获取长度

#include <string>

string str;
str.length();    // 1
str.size();    // 2
  • length() 仅用于 求 字符串 长度

  • size() 用于 求 1.字符串 2.vector类型 长度

  • strlen() 用于 求 字符串/字符数组 的长度,直到空结束字符,但不包括空结束字符。

  • sizeof() 用于 求 对象所占内存空间的大小 (即 所占的字节数)

6.比较得到最大最小值

https://www.csdn.net/tags/NtzaAg4sMDEtYmxvZwO0O0OO0O0O.html

7.string字符串添加新字符

C++string中的insert()函数用法详解_Fmm-PMO的博客-CSDN博客_c++ insert函数

https://blog.csdn.net/weixin_34132725/article/details/109293031

8.链表

https://www.cnblogs.com/Sky6634/p/16532350.html

9.int<-->char

【c++】int与char相互转换_zou_albert的博客-CSDN博客_c++ int转char

10.sort排序

C++中的排序函数_conlaysmile2017的博客-CSDN博客_c++排序函数

11.unordered_set

c++ unordered_set详细操作_好人好事代表nxx的博客-CSDN博客_unordered_set


Python

1.dict()

Python字典是另一种可变容器模型,可存储任意类型对象。如字符串、数字、元组等其他容器模型
因为字典是无序的所以不支持索引和切片。

    # 定义
    myDic1 = {1: "zhangsan", 2: "lisi"}
    myDic2 = dict(zip([1, 2, 3, 'liu'], ['zhangsan', 'lisi', 'wangwu', 'liu']))
    emptyDic = {}
    print(myDic1)
    print(myDic2)
    print(emptyDic)

    # 修改键值对
    myDic1[2] = "lisi2"
    print(myDic1)
    myDic2.update(liu='liu2')
    print(myDic2)

    # 访问
    temp = myDic2.keys() # 返回所有键
    print(temp)
    temp =myDic2.values() # 返回所有值
    print(temp)
    temp =myDic2.items() # 返回所有键值对
    print(temp)

    # 查找 按照键
    print(myDic2.get(3))
    if "liu" in myDic2:  # 2
        print(myDic2['liu'])

2.enumerate()

对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),可利用enumerate函数同时获取对象的索引和值。

list = ["小明", "小红", "小黄"]
for index,name in enumerate(list, 1):
    print(index, name)

# 结果
1 小明
2 小红
3 小黄

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值