引用高级、引用高级增加
#include<iostream>
#include<stdlib.h>
// int a[10]
// int (&ra)[10]
// int a[2][5]
// int (&ra)[2][5]
void main1()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(&ra)[10](a);//引用就是给原来的变量有一个别名同一个地址
int i = 0;
for (auto data: ra)//C++11的循环
{
data = i + 5;
std::cout << data << std::endl;
}
std::cout << a << ra << std::endl;
std::cout << &a << &ra << std::endl;
system("pause");
}
void main2()
{
int a[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(&ra)[2][5](a);//引用就是给原来的变量有一个别名同一个地址
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
std::cout << " " << ra[i][j];
}
std::cout << "\n";
}
std::cout << a << ra << std::endl;
std::cout << &a << &ra << std::endl;
system("pause");
}
int jia(int a, int b)
{
return a + b;
}
int jian(int a, int b)
{
return a - b;
}
void change(int(* & rp)(int,int))
{
rp = jian;
}
void main3()
{
int(* p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
//int(*&rp)(int a, int b)(p);//引用函数指针
//rp=jian;//()仅仅适用于初始化
change(p);
std::cout << p(1, 2) << std::endl;
system("pause");
}
int(*& changep(int (*&rp)(int,int)))(int, int)
{
rp = jian;
return rp;
}
void main4()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
p = changep(p);
std::cout << p(1, 2) << std::endl;
system("pause");
}
void main5()
{
//int *p[4];
int a = 1, b = 2, c = 3;
int *px[3] = { &a, &b, &c };
//int && p [4] = {a,b,c };
//引用数组是非法的
}
struct mystr
{
int b;
double a;
char c;
//代码区的函数不计入结构体的sizeof
void go()
{
std::cout << "123456789" << std::endl;
}
};
class MyClass
{
char & a;
char & b;
char & c;//引用的本质是指针,直接sizeof引用,就是求引用的数据大小
//引用变量占据4个字节
};
void main6()
{
int num = 10;
int & rnum(num);
double db = 10.9;
double & rdb(db);//直接作用引用的变量
std::cout << sizeof(rnum) << std::endl;
std::cout << sizeof(rdb) << std::endl;
std::cout << sizeof(MyClass) << std::endl;
system("pause");
}
int getdata(int && num)//右值引用,节约内存拷贝,内存优化所必须
{
std::cout << num << std::endl;
num += 10;
return num;
}
void main7()
{
int a = 5;
int b = 4;
std::cout << getdata(a+1) << std::endl;
system("pause");
}
//左值,一般可以取地址就是左值
//右值某些情况可以,某些情况不可以
void main8()
{
int a = 3;
int b = a + 1;//右值->左值
std::cout << getdata(std::move(a) ) << std::endl;
//std::move将左值转换为右值,C++11
}
void main9()
{
//const int num(6);
char str[10]("hello");//限定字符串不被修改
const char *pc(str);//指向常量的指针限定了指向的数据无法修改,+1,+2,+3
str[3] = 'x';//可以,
//pc[3] = 'y';
//*(pc + 3) = 'y';
pc = "world";
system("pause");
}
void main10()
{
char str[10]("hello");
const char(&rstr)[10](str);//常量引用
const char(&rrstr)[10](rstr);//引用可以给另一个引用初始化
str[4] = 'X';
//rstr[4] = 'Y';
}
void main11()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
int(* const &rp)(int a, int b)(p);//引用函数指针
//rp=jian;//()仅仅适用于初始化
}
auto自动变量自动根据类型创建数据
#include <iostream>
#include<stdlib.h>
void main()
{
double db = 10.9;
double *pdb = &db;
auto num = pdb;//通用传入接口
std::cout << typeid(db).name() << std::endl;
std::cout << typeid(num).name() << std::endl;
std::cout << typeid(pdb).name() << std::endl;
//typeid(db).name() db2;
decltype(db) numA(10.9);//通用的备份接口
std::cout << sizeof(numA) <<" "<< numA << std::endl;
system("pause");
}
Bool
#include<iostream>
#include<stdlib.h>
void main()
{
bool bl = (1 && 1) || 2 || (-1 && 0);
std::cout << typeid(bl).name() << std::endl;
std::cout << bl << std::endl;
decltype(bl) bt(1 + 2 * 3 - 4 && 3 + 2 || -1);
std::cout << bt << std::endl;
system("pause");
}
Enum
C中为弱类型,不做类型检查。而c++为强类型,要求更加严格。
Enum.c
#include <stdio.h>
enum color{ red=11 ,yellow,green,white};
void main()
{
enum color color1;
color1 = 18;//不注重数据类型
color1 = red;
printf("%d", red);
printf("\n%d", yellow);
printf("\n%d", green);
getchar();
}
Enum.cpp
#include <iostream>
enum color:char{ red='a' , yellow, green, white };
void main()
{
color mycolor = red;
mycolor = yellow;
//mycolor = 'A';//确保在枚举的范围的之内不出错
mycolor = color::white;//新语法
color mycolor1(red);
color mycolor2(color::red);
printf("%d,%c\n", red,red);
printf("%d,%c\n", yellow,yellow);
system("pause");
}
newdelete全局
#include<iostream>
#include<stdlib.h>
//全局的new delete监视所有释放分配
//局部的new delete监视某个类的所有分配释放
void *operator new(size_t size)
{
if (size == 0)
{
return 0;
}
void *p = malloc(size);
std::cout << "全局被调用内存被分配"<<p<<std::endl;
return p;
}
void operator delete (void *p)
{
std::cout << "全局被调用内存被释放" << p << std::endl;
free(p);
}
void *operator new[](size_t size)
{
return operator new(size);//每个对象挨个调用已经重载好的new,调用构造
}
void operator delete[](void*p)
{
return operator delete(p);//每个对象挨个调用已经重载好的delete,调用析构
}
class tansheng
{
public:
static int jishuqi;//静态
int *p;
int length;
public:
tansheng()//构建的时候初始化
{
std::cout << "谭胜被创建" << std::endl;
}
~tansheng()//删除的时候释放内存
{
std::cout << "谭胜被销毁" << std::endl;
}
static void * operator new(size_t size)
{
jishuqi += 1;
std::cout << "对象被创建" << std::endl;
tansheng *ptemp = ::new tansheng;//劫持
return ptemp;
}
static void * operator new[](size_t size)
{
std::cout << "对象数组被创建" << std::endl;
return operator new(size);
}
static void operator delete(void *p)
{
jishuqi -= 1;
std::cout << "对象被销毁" << std::endl;
::delete p;//::全局
}
static void operator delete[](void *p)
{
std::cout << "对象数组被销毁" << std::endl;
return operator delete(p);
}
};
int tansheng::jishuqi = 0;
void mai01n()
{
//int *p = new int[10];
//delete[]p;
tansheng *p1 = new tansheng[5];
delete []p1;
system("pause");
}
void main()
{
int *p = new int(8);
delete p;
system("pause");
}
大数据乘法与结构体
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
//C语言声明变量需要加上stuct
//C语言结构内部不可以有函数
//C语言结构体没有公有,私有,继承
struct MyStruct
{
int num1;
int num2;
};
struct MyStruct MyStruct1;
/// 加法,减法
/// 1234.567 ×12345.987
/// 15K+ 除法,
void getbigdata(char *dataa,char* datab)
{
int lengtha = strlen(dataa);
int lengthb = strlen(datab);
int *pres = (int *)malloc(sizeof(int)*(lengtha + lengthb));
memset(pres, 0, sizeof(int)*(lengtha + lengthb));//初始化
//累乘
for (int i = 0; i < lengtha;i++)
{
for (int j = 0; j < lengthb;j++)
{
pres[i+j+1]+=(dataa[i] - '0')*(datab[j] - '0');
}
}
//进位
for (int i = lengtha + lengthb-1;i>=0;i--)
{
if (pres[i]>=10)//进位
{
pres[i - 1] += pres[i] / 10;//进位
pres[i] %= 10;//取出个位数
}
}
int i = 0;
while (pres[i]==0)
{
i++;//恰好不为0的位置
}
char *lastres = malloc(sizeof(char)*(lengtha + lengthb));
int j;
for (j = 0; j < lengtha + lengthb; j++, i++)
{
lastres[j] = pres[i] + '0';
}
lastres[j] = '\0';
printf("last结果=%s",lastres);
}
void main()
{
char str1[100] = { 0 };
char str2[100] = { 0 };
scanf("%s%s", str1, str2);
printf("str1=%s,str2=%s", str1, str2);//打印结果
getbigdata(str1, str2);
system("pause");
}
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdlib.h>
#include<string.h>
//除了数据还有函数
struct bigdatacom
{
protected://内部私有
char dataa[100];
char datab[100];
public://公有公开
void init(const char *str1, const char *str2)
{
std::cout << typeid(*this).name() << std::endl;
strcpy(this->dataa, str1);
strcpy(this->datab, str2);
}
char * getbigdata()
{
int lengtha = strlen(dataa);
int lengthb = strlen(datab);
int *pres = (int *)malloc(sizeof(int)*(lengtha + lengthb));
memset(pres, 0, sizeof(int)*(lengtha + lengthb));//初始化
//累乘
for (int i = 0; i < lengtha; i++)
{
for (int j = 0; j < lengthb; j++)
{
pres[i + j + 1] += (dataa[i] - '0')*(datab[j] - '0');
}
}
//进位
for (int i = lengtha + lengthb - 1; i >= 0; i--)
{
if (pres[i] >= 10)//进位
{
pres[i - 1] += pres[i] / 10;//进位
pres[i] %= 10;//取出个位数
}
}
int i = 0;
while (pres[i] == 0)
{
i++;//恰好不为0的位置
}
char *lastres =(char*) malloc(sizeof(char)*(lengtha + lengthb));
int j;
for (j = 0; j < lengtha + lengthb; j++, i++)
{
lastres[j] = pres[i] + '0';
}
lastres[j] = '\0';
return lastres;
//printf("last结果=%s", lastres);
}
};
struct myclass :public bigdatacom //继承
{
void coutstr()//新增
{
std::cout << this->dataa << this ->datab << std::endl;
}
};
void main()
{
myclass class1;
class1.init("12345", "1000");
std::cout << class1.getbigdata() << std::endl;
class1.coutstr();
system("pause");
}
void main1()
{
bigdatacom big1;//C++结构体可要可不要struct
big1.init("123123", "456456");//调用内部函数
std::cout << big1.getbigdata() << std::endl;
system("pause");
}
函数模板与auto自动变量
函数模板
#include<stdlib.h>
#include<iostream>
#include<cstdarg>
//函数模板 可变参数
//参数至少要有一个是模板类型
template<typename NT>
NT sum(int count,NT data1 ...)//累加
{
va_list arg_ptr;//参数列表的指针
va_start(arg_ptr, count);//限定从count开始,限定多少个参数
NT sumres(0);
for (int i = 0; i < count;i++)
{
sumres += va_arg(arg_ptr, NT);
}
va_end(arg_ptr);//结束
return sumres;
}
//T通用数据类型
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 = p[i];
}
}
return maxdata;
}
int getmax(int *p, int n)
{
int max(0);
max = p[0];//假定第一个数位最大
for (int i = 1; i < 10; i++)
{
if (max<p[i])//确保max>=p[i]
{
max = p[i];//
}
}
return max;
}
double getmax(double *p, int n)
{
double max(0);
max = p[0];//假定第一个数位最大
for (int i = 1; i < 10; i++)
{
if (max < p[i])//确保max>=p[i]
{
max = p[i];//
}
}
return max;
}
void main2()
{
std::cout << sum(5,1,2,3,4,5) << std::endl;
std::cout << sum(6, 1, 2, 3, 4, 5,6) << std::endl;
std::cout << sum(7, 1, 2, 3, 4, 5, 6,7) << std::endl;
std::cout << sum(7, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1) << std::endl;
std::cout << sum(6, 1.1, 2.1, 3.1, 4.1, 5.1, 6.1) << std::endl;
system("pause");
}
void main1()
{
double a[10] = { 2, 3, 4, 98, 77, 999.1, 87, 123, 0, 12 };
int b[10] = { 1, 2, 3, 4, 15, 6, 7, 8, 9, 10 };
std::cout << MAX(a,10) << std::endl;
std::cout << MAX(b, 10) << std::endl;
system("pause");
}
Auto与函数模板
#include<stdlib.h>
#include<iostream>
/*
auto get(int num, double data)->decltype(num*data)
{
}
*/
//自动数据类型,根据实际推导出类型,
template<class T1,class T2>//根据类型获取类型
auto get(T1 data, T2 bigdata)->decltype(data *bigdata)
{
return data*bigdata;
}
//函数参数不允许使用自动变量
//int putnum(auto num)
//{
//
//}
void main()
{
std::cout << typeid(get(12.0, 'A')).name() << std::endl;
std::cout << get(12.0, 'A') << std::endl;
std::cout << typeid(get(12, 'A')).name() << std::endl;
std::cout << get(12, 'A') << std::endl;
system("pause");
}
宽字符本地化
#include<iostream>
#include<stdlib.h>
#include<locale>
void main()
{
setlocale(LC_ALL, "chs");//设置本地化
wchar_t *p1 = L"123456123123qweqeqe";
std::wcout << p1 << std::endl;
wchar_t *p2 = L"北京123456";
std::wcout << p2 << std::endl;
system("pause");
}
inline 内联函数
#include <stdlib.h>
#include<iostream>
//替换
#define GETX3(N) N*N*N
//1+2*1+2*1+2
//函数
//inline只是对于编译器的建议
一般情况下,我们对内联函数做如下的限制:
//(1) 不能有递归
//(2) 不能包含静态数据
//(3) 不能包含循环
//(4) 不能包含switch和goto语句
//(5) 不能包含数组
//若一个内联函数定义不满足以上限制,则编译系统把它当作普通函数对待。
inline int getX3(int x);//内联函数,内部展开
inline int getX3(int x)//类型安全
{
return x*x*x;
}
template <class T>
inline T getX2(T x)//C++类型不匹配出错,不是单纯的替换
{
return x*x;
}
void main()
{
std::cout << GETX3(1 + 2) << std::endl;
std::cout << getX3(1 + 2) << std::endl;
std::cout << GETX3((1 + 2)) << std::endl;
std::cout << GETX3((2.0 + 2)) << std::endl;
system("pause");
}
CCPP不同
C特点
#include<stdio.h>
#include<stdlib.h>
int a;//C语言全局变量有声明与定义的差别
int a;
int a;
int a;
int a;
static int b;
static int b;
main()
{
//3-3 ? system("calc") : system("mspaint");
int num = 5 < 3?10:9;
printf("%d", num);
int a = 3;
//(a = 3) = 4;
int b = 5;
//(a > b ? a : b)=2;
//(++a)++
register int numa=1;
//&numa;
system("pause");
}
void test(int a, double, int)//编译不能通过
{
}
C++
#include<stdio.h>
#include<iostream>
//C++检测到右值在内存有实体,自动转换为左值
//C 语言不会吧右值转换为左值
//C++全局变量没有声明与定义的差别
//静态全局变量也没有声明与定义的差别
//C++是强类型系统,函数返回值必须要有类型
//register C++编译器做了优化,检测到取地址,就不会把它放到寄存器
//register 可以取地址
int a;
//int a;
static int b;
//static int b;
//C++编译器 编译的宽泛,
//为了修改源代码,后面留下拓展
//占位,占位参数,
void test(int a, double, int)
{
std::cout << a;
}
void main1()
{
int a = 3;
(a = 3) = 4;
int b = 5;
(a > b ? a : b) = 2;
(++a)++;
//(a + 1)++;
register int num(1);
std::cout << &num<< std::endl;
std::cout << a <<b<< std::endl;
test(1, 2.9, 3);
system("pause");
}
//detele以后尽量设置为NULL
//
void main2()
{
int *p = new int;
delete p;//防止重复删除
p = NULL;
delete p;
}
void main()
{
//int num;
//文件重定向输入输出,网页重定向CGI
char str[100] = { 0 };
std::cin>>str;
std::cout << str;
system(str);
}