![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ prime
captionyao
这个作者很懒,什么都没留下…
展开
-
char s[]和char *s的区别
char *s1="hello";//声明一个指针指向常量"hello"char s2[]="hello";//在栈中开辟一个数组字符"hello";/** s2[]="hello" 相当于* char s2[6];* s2[0]='h';* s2[1]='e';* s2[2]='l';* s2[3]='l';* s2[4]='0';* s2[5]='\0';**/...原创 2020-02-19 20:49:35 · 303 阅读 · 0 评论 -
07/15仿函数
函数绑定:#include <iostream>#include <functional>//处理函数using namespace std;using namespace std::placeholders;struct MyStruct{ void add(int a) { cout << a << endl; ...原创 2020-02-19 16:29:14 · 138 阅读 · 0 评论 -
引用包装器
函数副本机制:#include <iostream>template<class T>void com(T arg){ arg++;}void main(){ int count = 10; com(count); std::cout << count << std::endl; system("pause");}...原创 2020-02-19 15:13:00 · 141 阅读 · 0 评论 -
自动数据类型
自动数据类型,根据实际推导出类型函数类型可以使用auto,但是参数不允许使用autotemplate<class T1,class T2>//根据类型获取类型#include <stdlib.h>#include <iostream>//获取变量的数据类型 变量->decltype/*auto get(int num,double...原创 2020-02-19 14:50:29 · 113 阅读 · 0 评论 -
函数模板以及自动类型
传统的函数定义,但若数据类型丰富,需要重载过多的函数。#include <stdlib.h>#include <iostream>//数组作为参数时,会退化为指针int getmax(int *p,int n){ int max(0); max = p[0]; for (int i = 0; i < 10; i++) { if (max&...原创 2020-02-19 14:36:04 · 170 阅读 · 0 评论 -
inline函数
inline函数:内联函数C语言使用define函数。这样的话,速度较慢。#include <stdlib.h>#include <iostream>//替换#define GETX3(N) N*N*N//1+2*1+2*1+2//函数 int getX3(int x){ return x*x*x;}//define可以实现泛...原创 2020-02-19 12:54:31 · 110 阅读 · 0 评论 -
C与CPP的别名
C的别名:#include <iostream>int add(int a, int b){ return a + b;}typedef int(*ADD)(int a, int b);void main(){ ADD p = add; std::cout << p(1, 2) << std::endl; std::cin.ge...原创 2020-02-14 17:06:54 · 166 阅读 · 0 评论 -
去掉-转义字符(启动exe文件)
#include <iostream>#include <string>#include <stdlib.h>void main(){ std::string path = R"("D:\QQ\Bin\QQScLauncher.exe")"; //R"()"去掉括号里面的转义字符 system(path.c_str()); system("p...原创 2020-02-14 12:35:01 · 84 阅读 · 0 评论 -
函数绑定
#include <iostream>#include <functional>//处理函数using namespace std;using namespace std::placeholders;struct MyStruct{ void add(int a) { cout << a << endl; } void a...原创 2020-02-14 12:12:29 · 79 阅读 · 0 评论 -
vector的二维数组
#include <iostream>#include <vector>#include <algorithm>void main1(){ std::vector<int> myvector; myvector.push_back(1); myvector.push_back(2); myvector.push_back(3);//...原创 2020-02-08 12:00:33 · 464 阅读 · 0 评论 -
C++11的新性能:lambda
#include <iostream>#include <vector>#include <algorithm>void main(){ std::vector<int> myvector; myvector.push_back(1); myvector.push_back(2); myvector.push_back(3);//统...原创 2020-02-08 11:23:03 · 133 阅读 · 0 评论 -
C++高级数组
#include <iostream>#include <array>#include <vector>/C++的标准库#include <string>//C++字符串using std::vector;//动态数组,堆上using std::array;//静态数组,栈上using std::string;//使用C++风格的数...原创 2020-02-08 10:27:38 · 243 阅读 · 0 评论 -
Cpp新类型数组
#include <iostream>#include <array>#include <string>#include <stdlib.h>void main1(){ double db[4] = { 1.1,2.2,3.3,4.4 }; //std::array数据类型,double元素类型,4为个数 std::array...原创 2020-02-07 21:47:14 · 177 阅读 · 0 评论 -
C++的数据类型转换
#include <iostream>#include <stdio.h>void main1(){ double db = 10.9; float fl = db; std::cin.get();}void main2(){ void *p = new int[10]; int *pint = (int*)p;//C语言风格 }void...原创 2020-02-07 19:52:08 · 125 阅读 · 0 评论 -
高级函数模板
Struct#include<iostream>struct info{ char name[40]; double db; int data;};template <typename T>void swap(T &a,T &b){ std::cout << "通用函数模板" << std::e...原创 2020-02-07 17:46:03 · 77 阅读 · 0 评论 -
函数模板
#include <stdlib.h>#include <iostream>template<typename T> T MAX(T*p, const int n){ T maxdata(p[0]); for (int i = 1; i < n; i++) { if (maxdata < p[i]) { maxdata...原创 2020-02-06 21:20:11 · 75 阅读 · 0 评论 -
bool变量
#include <iostream>#include <stdlib.h>void main(){ bool b1 = 1 && 1 || 2 || -1 && 0; std::cout << b1 <<std::endl; system("pause");}&&的优先级高于|...原创 2020-02-05 19:25:20 · 133 阅读 · 0 评论 -
auto
#include <iostream>void main(){ double db = 10.9; double *pdb = &db; auto num = pdb;//通用的传入接口 std::cout << typeid(db).name() << std::endl; std::cout << typeid(num)...原创 2020-02-05 17:15:29 · 159 阅读 · 0 评论 -
C、CPP的const
C和C++的const的区别本质上,C++的const为一个常量,所以可以初始化数组。而C的const为一个伪常量不可以初始化数组。C:#include <stdio.h>void main(){ const int num = 5;//C语言的const属于伪常量 int a[num];//所以此处报错}CPP:#include <iostre...原创 2020-02-04 20:10:19 · 143 阅读 · 0 评论 -
左值和右值
#include <iostream>void main(){ int num1(5); int num2(10); int *pnum(&num1); std::cout <<"pnum的地址:"<< pnum << std::endl; std::cout <<"num1的地址" <<&nu...原创 2020-02-04 16:14:18 · 100 阅读 · 0 评论 -
左值引用
#include <iostream>void main1(){ int a = 5; int & ra (a);//引用初始化,引用必须初始化 ra = 3; std::cout << a << std::endl; system("pause");}void change(int a){ a = 19; std::cou...原创 2020-02-04 15:21:52 · 81 阅读 · 0 评论 -
auto
#include <iostream>void main1(){ auto num = 10.9; auto numA = 10/2.0; std::cout << num << " " << numA << std::endl; system("pause");}void main(){ double num[...原创 2020-02-04 14:49:56 · 57 阅读 · 0 评论 -
C语言函数地址的打印
#include<iostream>#include<stdio.h>//参数的个数,参数的类型不同,顺序不同,返回值无关void go(int a){ std::cout << a;}void go(double a){ std::cout << a;}void go(double a, int b){ std::...原创 2020-02-03 16:41:11 · 5457 阅读 · 0 评论 -
函数重载在汇编里面
这也就是为什么C++可以实现函数重载,而C语言不可以实现函数重载原创 2020-02-03 16:06:07 · 124 阅读 · 0 评论 -
visual stduio多行注释
注释: 先CTRL+K,然后CTRL+C取消注释:先CTRL+K,然后CTRL+U原创 2020-02-03 15:55:53 · 178 阅读 · 0 评论 -
C语言无法使用全局变量
#include <stdio.h>int a = 1;void main(){ printf("%d", a); int a = 3; printf("%d", a);}原创 2020-02-03 14:45:50 · 810 阅读 · 0 评论 -
namespace 软件开发注意事项
#include <iostream>namespace runmove{ int y(5); int(*padd)(int, int);//函数指针,}int add(int a, int b){ return a + b;}int padd(int a, int b){ std::cout << a << b; return...原创 2020-02-03 14:21:56 · 135 阅读 · 0 评论 -
强类型
强类型:#include <stdio.h>void main(){ int *p1 = NULL; double *p2 = NULL; p2 = p1;}同一段代码,c语言可以,而cpp文件不可以。C++注重类型,强类型。而C会默认完成自动转换。...原创 2020-02-01 21:25:28 · 72 阅读 · 0 评论 -
类的单例代码实现
single.h:#ifndef SINGLE_H#define SINGLE_Hclass single{private: static single *p;protected: single();public: static single *makesignle(); static void releasesingle();};#...原创 2020-01-27 13:01:21 · 107 阅读 · 0 评论 -
类的单例
摘自百度百科:单例模式,属于创建类型的一种常用的软件设计模式。通过单例模式的方法创建的类在当前进程中只有一个实例(根据需要,也有可能一个线程中属于单例,如:仅线程上下文内使用同一个实例)简介:单例模式是设计模式中最简单的形式之一。这一模式的目的是使得类的一个对象成为系统中的唯一实例。要实现这一点,可以从客户端对其进行实例化开始。因此需要用一种只允许生成对象类的唯一实例的机制,“阻...原创 2020-01-27 12:53:00 · 219 阅读 · 0 评论 -
详细,Qt Creator快捷键大全,附快捷键配置方法
一、快捷键配置方法:进入“工具->选项->环境->键盘”即可配置快捷键。二、常用默认快捷键:编号 快捷键 功能1 Esc 切换到代码编辑状态2 F1 查看帮助(选中某一类或函数,按下F1,出现帮助文档)3 F2 在光标选中对象的声明和定义之间切换(和Ctrl+鼠标左键一样的效果,选中某一类或函数,按下F2,迅速定...转载 2020-01-27 12:01:08 · 243 阅读 · 0 评论 -
char *s1 和 char s2[] 的区别
最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区(认为无区别),导致有时出现“难以理解”的错误。一时也不能说得很明白,网上也搜了一下相关文章发现一些写的比较好的,综合了一下当教育资料备用。char *s1 = "hello";char s2[] = "hello";【区别所在】char *s1 的s1,而...转载 2020-01-25 21:01:55 · 4007 阅读 · 0 评论 -
命名空间:namespace
个人认为有一点要注意:先声明命名空间,再使用using namespace yao;只要能出现在全局作用域中的声明就能置于命名空间内,其他的命名空间的声明也可以定义在命名空间内。即为,命名空间可以嵌套。#include <iostream>using namespace std;namespace yao {void func(int i){ cout&...原创 2020-01-25 21:01:14 · 71 阅读 · 0 评论 -
函数参数:引用
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;void func(int a,int b){ printf("a=%d,b=%d",a,b);}int main(){ c...原创 2020-01-25 17:13:09 · 1258 阅读 · 0 评论 -
C语言文件读取
#include <stdio.h>int main(int argc,char *argv[]){ FILE *fp; //fp=fopen(argv[1],"w"); fp=fopen("yaoyang.txt","w"); fprintf(fp,"yaoyang\t%d\n",10); fclose(fp);...原创 2020-01-21 21:48:37 · 524 阅读 · 0 评论 -
指针作为参数
#include <iostream>#include <stdio.h>using namespace std;void ox_say(char *str){ printf("%s\n",str);}void dog_say(char *str){ printf("%s\n",str);}void man_say(char *str)...原创 2020-01-21 15:50:18 · 67 阅读 · 0 评论 -
数组指针等价于二维数组
数组指针:指针指向了一个数组指针数组:数组中存储的为,指针#include <stdio.h>//void fun(char a[][20],int n)直接传二维数组void fun(char(*a)[20], int n)//数组指针{ int i; for (i = 0; i < n; i++) scanf("%s", a[i]);}int ...原创 2020-01-20 21:25:04 · 334 阅读 · 0 评论 -
二重指针作为参数传参
#include <stdio.h> 2 3 void fun(char *arr[],int n) 4 { 5 int i; 6 for(i=0;i<n;i++) 7 printf("%s\n",arr[i]);...原创 2020-01-20 21:04:55 · 771 阅读 · 0 评论 -
二重指针等价于指针数组
#include <stdio.h> int main(int argc,char *argv[]) { int i; printf("%s\n%s\n",argv[0],argv[1]); for(i=0;i<argc;i++)...原创 2020-01-20 20:45:48 · 216 阅读 · 0 评论 -
指针与数组
#include <stdio.h>#include <iostream>using namespace std;void main(){ char *p[10]; printf("%d\t%d\n", sizeof(p), sizeof(*p));//sizeof(p),取出来的为一个数组=4*10,*p为指针,指的是:数组首元素的地址 ...原创 2020-01-20 19:49:01 · 85 阅读 · 0 评论