排序
1.对vector排序,并去除重复元素
Vector
1.vector去重:
vector<int>::iterator iter = unique(vec.begin(), vec.end());
//删除重复元素
vec.erase(iter, vec.end());
2.二维的vector:
vector<vector<bool>> flag(board.size(), vector<bool>(board[0].size()));
3.vector默认初始化的值
vector默认初始化的值为0;
vector<int> arr(10);
3.memset
在一段内存块中填充某个给定的值,它对较大的结构体或数组进行清零操作的一种最快方法.
Trie* next[26];
memset(next, 0, sizeof(next));
4.map的使用
https://www.zhaokeli.com/article/8309.html
map<string,int> cmap;
//第一种插入值 的方法,没有自动新加,有的话覆盖
cmap["op1"] = 1;
4.resize()
vector vec;
vec.resize(n,0); //将vec的值初始化为n个0.
Set
set的使用
被插入进set的元素都会在set里进行 自动排序和去重 ,这就有了很大的应用空间。集合set的元素只能使用迭代器迭代输出。set是由hog
#include<set>
vector<string> new_result;
set<string>::iterator it1;
for (it1 = set_result.begin(); it1 != set_result.end(); it1++){
new_result.push_back(*it1);
}
//set元素的插入操作
set<int>s;
s.insert(3);
s.insert(1);
s.insert(2);
s.insert(1);
Map
C++ Map是一种键值对容器。map:本质是一个红黑树,是一个可以自动排序的数据结构,所以map的元素都是有序排列的。
unordered_map:本质是哈希表,执行查找、存储操作很快,但是比较耗空间,其内部元素是无序的
https://blog.youkuaiyun.com/sevenjoin/article/details/81943864
判断Map中的一个键值是否存在:
#include<iostream>
#include<unordered_map>
using namespace std;
int main(){
unordered_map<string, string> umap;
umap["ztw"] = "ztw";
if (umap.find("ztw")!=umap.end()){
cout << "This is a exit" << endl;
}
system("pause");
}
map的大小查看插入元素的数量,可以使用size函数
map的遍历:
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
int main(){
unordered_map<string, vector<string>> umap;
umap["ztw"].emplace_back("ztw");
unordered_map<string, vector<string>>::iterator iter = umap.begin();
while (iter != umap.end()){
cout << iter->first << "";
iter++;
}
system("pause");
}
字符串
对字符串操作需要引入string头文件
- 将字符串变为整型
string a = "11";
#将字符串a变为了字符数组a_str
const char* a_str = a.c_str();
cout << atoi(a_str) << " ";
2.将整型变为字符串型
int a = 100;
cout<< to_string(a)<<" ";
3.单个字符变为整型
int main() {
char a = '9';
int b = a-'0';
cout << b << " ";
system("pause");
}
变量类型
https://www.cnblogs.com/0patrick/p/12864894.html(如何获取变量类型)
C++结构体和类
1.c++结构体和类的区别
https://blog.youkuaiyun.com/lailaiquququ11/article/details/82904406
2.C++写一个结构体
http://c.biancheng.net/view/1407.html
注意下述形式的描述:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
多态
多态首先是建立在继承的基础上的,先有继承才能有多态。多态是指不同的子类在继承父类后分别都重写覆盖了父类的方法,即父类同一个方法,在继承的子类中表现出不同的形式。
红黑树
https://blog.youkuaiyun.com/qq_36610462/article/details/83277524
队列
对于offer和add的区别:
https://blog.youkuaiyun.com/weixin_34334744/article/details/93761993
poll与pop的区别
https://blog.youkuaiyun.com/hellokitty_gaga/article/details/109180558
inline
在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数。
https://www.cnblogs.com/fnlingnzb-learner/p/6423917.html
数据类型
内存分配
malloc和calloc的区别
https://zhidao.baidu.com/question/40468782.html
node2str[0] = (char*)calloc(2, sizeof(char));
如果分配2,但是可以使用3的空间,但是有可能会有值覆盖的情况。
assert
//使用assert,如果其值为假(即为0),那么它先向stderr打印一条出错信息,然后通过调用 abort 来终止程序运行。