
c/c++
爱学习的徐梦圆
这个作者很懒,什么都没留下…
展开
-
c++:运算符重载+
#include <iostream>using namespace std;class Dog{ public: int m_a; int m_b;};int main(){ Dog p1; p1.m_a = 10; p1.m_b = 20; Dog p2; p2.m_a = 10; p2.m_b = 20; Dog p3; p3 =p1 + p2; //没有与这些操.原创 2021-12-18 16:20:40 · 768 阅读 · 0 评论 -
c++:vector sort()排序
sort()函数:sort(begin, end, cmp),其中begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针,cmp参数为排序准则,如果没有的话,默认以非降序排序。实例:#include <iostream>#include <vector>#include<algorithm>using namespace std;bool cmp(int x,int y){原创 2021-12-16 14:06:05 · 60945 阅读 · 6 评论 -
c++:c++11 lambda
定义:lambda 匿名函数很简单,可以套用如下的语法格式:[外部变量访问方式说明符] (参数) mutable noexcept/throw() -> 返回值类型{ 函数体;};#include <iostream>#include <algorithm>using namespace std;int num1 =100;//全局变量class Lambda { public: void Lambda1();//用...原创 2021-12-13 17:17:26 · 977 阅读 · 0 评论 -
c++:static静态成员变量
代码:#include <iostream>using namespace std;class Student { public: Student(char *name,int age,float score); void display(); public: static int m_total; private: char *m_name; int m_age;原创 2021-12-11 13:00:40 · 518 阅读 · 0 评论 -
c++:STL list容器访问元素的几种方法
#include <iostream>#include <list>using namespace std;int main(){ /* 不同于之前学过的 STL 容器,访问 list 容器中存储元素的方式很有限,即要么使用 front() 和 back() 成员函数,要么使用 list 容器迭代器。 list 容器不支持随机访问,未提供下标操作符 [] 和 at() 成员函数,也没有提供 data() 成员函数。 通过 front().原创 2021-12-01 15:57:56 · 4630 阅读 · 0 评论 -
c++:stl中vector访问元素的几种方式
#include <iostream>#include <vector>using namespace std;class Vector{ public: void cycle1(); void cycle2(); void cycle3();};void Vector::cycle1(){ vector<int>values{1,2,3,4,5}; //获取容器中的首个元素 .原创 2021-11-26 11:35:59 · 1395 阅读 · 0 评论 -
c++:在c++中使用fopen函数
#include <iostream>#include <string>#include <cstdio>#include <cstdlib>#include <string.h>using namespace std;#define N 100class File{ public: void LodeFile(const string& path);};void File:.原创 2021-11-15 16:33:46 · 7339 阅读 · 0 评论 -
c++:多态
#include <iostream>using namespace std;class Shape{ protected: int weigth,heigth; public: Shape(int a = 0, int b = 0) //构造函数 { weigth = a; heigth = b; } .原创 2021-10-28 17:15:49 · 111 阅读 · 0 评论 -
c++:函数模板
#include <iostream>using namespace std;template<class T> void Swap(T &a, T &b) //交换变量的值{ T tmep = a; a = b; b = tmep;}template<class T> T compare(T &a, T &b,T &c) //选出最大值{ T max_num = a; i.原创 2021-10-27 14:28:01 · 158 阅读 · 0 评论 -
C++:几种智能指针之间的比较
unique_ptr持有对对象的独有权——两个unique_ptr不能指向一个对象,即 unique_ptr 不共享它所管理的对象。它无法复制到其他 unique_ptr,无法通过值传递到函数,也无法用于需要副本的任何标准模板库 (STL)算法。只能移动 unique_ptr,即对资源管理权限可以实现转移shared_ptr 是一个标准的共享所有权的智能指针,允许多个指针指向同一个对象,在使用引用计数的机制上提供了可以共享所有权的智能指针weak_ptr :它不具有普通指针的行为,没有重载 oper转载 2021-10-25 16:41:58 · 280 阅读 · 0 评论 -
C++编程:继承
#include <iostream>using namespace std;class Shape{ public: void setlength(int len) { length = len; } void setweigth(int wei) { weigth = wei; .原创 2021-10-25 14:18:44 · 128 阅读 · 0 评论 -
leecode刷题:剑指offer 58:左旋转字符串
字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。示例 1:输入: s = "abcdefg", k = 2输出:"cdefgab"示例 2:输入: s = "lrloseumgh", k = 6输出:"umghlrlose"限制:1 <= k < s.length <= 10000char* rev...原创 2021-08-03 23:38:09 · 134 阅读 · 0 评论 -
leecode刷题:LCP 01. 猜数字
小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次?输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。示例 1:输入:guess = [1,2,3], answer = [1,2,3]输出:3解释:小A 每次都猜对了。示例 2:输入:guess = [2,2,3], answer = [3,2,1]原创 2021-08-02 22:53:41 · 126 阅读 · 0 评论 -
leecode刷题:771.宝石和石头
给定字符串J代表石头中宝石的类型,和字符串S代表你拥有的石头。S中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。J中的字母不重复,J和S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。示例 1:输入: J = "aA", S = "aAAbbbb"输出: 3示例 2:输入: J = "z", S = "ZZ"输出: 0注意:S和J最多含有50个字母。J中的字符不重复。c解法:int numJ...原创 2021-08-01 19:59:55 · 111 阅读 · 0 评论 -
leecode刷题:面试题:合并排序的数组
给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳 B。 编写一个方法,将 B 合并入 A 并排序。初始化A 和 B 的元素数量分别为m 和 n。示例:输入:A = [1,2,3,0,0,0], m = 3B = [2,5,6], n = 3输出:[1,2,2,3,5,6]说明:A.length == n + mc解法:void merge(int* A, int ASize, int m, int* B, int BSize, in...原创 2021-08-01 16:36:30 · 97 阅读 · 0 评论 -
leecode刷题:1.两数之和
给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 target的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案。示例 1:输入:nums = [2,7,11,15], target = 9输出:[0,1]解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。示例 2:输入:nums = [3,2,4], targe...原创 2021-08-01 16:20:51 · 921 阅读 · 0 评论 -
c语言简单学会链表(2):双向链表
双向链表:数据结构:typedef struct doublelist { int data; //数据域 struct doublelist *prev; //指针域,前向指针 struct doublelist *next; //指针域,后向指针}DOUBLELIST;创建一个节点:DOUBLELIST *CreateList(int data){ DOUBLELIST *P = NULL; P = malloc(sizeof(DOUBLE原创 2021-07-08 19:58:58 · 145 阅读 · 0 评论 -
c语言简单学会链表(1):单链表
单链表:数据结构:struct NODE { int data; //数据域 struct NODE *next; //指针域};创建一个链表:NODE *Create_Node(){ NODE *head = NULL; //定义一个头指针 head = (NODE *)malloc(sizeof(NODE)); //分配空间 if (head == NULL) { printf("malloc failed\n");原创 2021-06-29 20:22:05 · 165 阅读 · 0 评论 -
C/C++内存管理
c语言的内存管理是对系统内存的分配,创建,使用等一系列操作首先,每一个应用程序的内存之间是相互独立的,正常情况下程序A是无法访问程序B的,但是有些手段(指针之类的)。比如现在有两个应用程序,他们在计算机系统中各自开辟了两块内存区域A,B,区域A和区域B是存在逻辑分割的...原创 2021-06-25 17:29:31 · 91 阅读 · 0 评论