在大一下学期学习了c语言,最近又想学一下c++,想提高一下编程功底,在此记录一下在有c基础(及其拙劣)的情况下,自己的编程进阶之路。
sizeof()
返回字节的大小
short a=10;
cout << sizeof(short)<< endl;//2
cout << sizeof(a) << endl;//2
cout << sizeof(int) << endl;//4
针对不同的操作系统,返回的值不同。
在float后面加 f
float a = 1.20f;
double b = 1.222;
创建字符型变量,用单引号,且只能一个字母;
char ch = 'a';
查找ASCII码:
A----65
a-----97
char ch = 'a';
cout << (int)ch<< endl;
制表符 \t 有对齐的作用
cout << "aaaa\thello"<< endl;
cout << "aa\thello" << endl;
cout << "aaaaaa\thello" << endl;
输出:

char str[] = "hello world";//c风格字符串
cout << str << endl;
string str2 = "hello c++"; //c++风格,
#include<string>
cout << str2 << endl;
逻辑与
&&
逻辑或
||
选择结构:
if()
{}
esle if()
{}
else
{}
三目运算符:
( ? : )
它可以进行赋值操作。
int a, b, c;
a = 10, b = 30;
c = (a > b ? a :b);
cout << "c" << c << endl;
(a > b ? a : b) = 100;
cout << b << endl;
switch选择结构。
switch
switch ()
{case x: xxxxxx;break;
case x: xxxxxx;break;
case x: xxxxxx;break;
default : xxxxxx;break;
}
cout << "输入评分" << endl;
int a;
cin >> a;
switch (a)
{
case 10: cout << "超级好" << endl; break;
case 9: cout<< "hen好" << endl; break;
case 8: cout<< "好" << endl; break;
default: cout << "la ji" << endl;
}
注意:
1.switch 只能对整形或者字符型进行判断。
2.记得写break。
执行效率高于if语句。
循环:
1.while循环
猜数字:
#include<iostream>
using namespace std;
#include<stdlib.h> //生成随机数 vs下不需要,Dev需要
#include<ctime>//随机数
srand((unsigned int)time(NULL));//数字种子
int num,a;
num = rand() % 100 + 1;//产生1-100的随机数,rand()%100是产生0-99的
while (1)
{
cout << "输入数字" << endl;
cin >> a;
if (a > num)
{
cout << "输入过大" << endl;
}
else if (a < num)
{
cout << "输入过小" << endl;
}
else
{
cout << "对!!!!" << endl;
break;
}
}
system("pause");
return 0;
}
do ……while
会先执行一次,示例如下:
do
{
循环语句
}
while(条件);
水仙花数
#include<iostream>
using namemspace std;
int main()
{
int i = 100, a, b, c;
do
{
a = i % 10;
c = i / 100;
b = (i / 10)%10;
if (a * a*a + b * b*b +c* c * c == i)
{
cout << "水仙花数" << i << endl;
}
i++;
} while (i <1000);
//for循环
for(i=100;i<1000;i++)
{
a = i % 10;
c = i / 100;
b = (i / 10) % 10;
if (a * a * a + b * b * b + c * c * c == i)
{
cout << "水仙花数" << i << endl;
}
}
system("pause");
return 0;
}
镶嵌循环
9*9乘法表
#include<iostream>
using namespace std;
int main()
{
int i,j;
for (i = 1; i < 10; i++)
{
for (j = 1; j <= i; j++)
{
cout << j << "x" << i << "=" << j * i <<" ";
}
cout << endl;
}
system("pause");
return 0;
}

continue:
跳出本次循环,进行下次循环
break:
跳出循环就不循环了。
##########
goto用法:
注意flag:(是冒号!)
#include<iostream>
using namespace std;
#include<string>
int main()
{
cout << "11111111" << endl;
cout << "2222222" << endl;
goto FLAG;
cout << "3333333333" << endl;
cout << "4444444444" << endl;
cout << "555555" << endl;
cout << "66666666" << endl;
FLAG: //冒号!
cout << "7777777" << endl;
system("pause");
return 0;
}
数组:
注意:数组,连续数据,同种类型的数据。
一。3种方式:
1.数据类型:数组名[长度]
2.数据类型:数组名[数组长度]={10,20,30,。。。。} // 如果后面数据没有出现,会用0替代。
3.数据类型:数组名[]={10,20,30,…} // 自动出现数组
#include<iostream>
using namespace std;
#include<string>
int main()
{
float arr[] = { 1,2.5,3,4,5 };
for (int i = 0; i < 5; i++)
cout << arr[i] << endl;
system("pause");
return 0;
}
二.一维数组名的用途:
1.统计数组占用内存的大小。
float arr[] = { 1,2.5,3,4,5 };
for (int i = 0; i < 5; i++)
{
cout << arr[i] << endl;
}
cout<<"占内存"<< sizeof(arr)<<endl;
cout<<"每个占内存"<<sizeof(arr[0])<<endl;
int a = sizeof(arr) / sizeof(arr[0]);
cout <<"数组大小"<< a << endl;//数组大小
2.一维数组名:也代表了数组的首地址。
cout << "首地址" << int(arr) << endl;//强制转换16进制到10进制。(返回的16进制看不懂)
cout << "第一个元素的地址" << int(&arr[0]) << endl;
cout << "第二个元素的地址" << int(&arr[1]) << endl;
输入自定义数字个数,并进行排序。
自己写的,蛮力
#include<iostream>
using namespace std;
#include<string>
int main()
{
int n,*arr;
cout << "输入的数字个数" << endl;
cin >> n;
arr=new int [n]; //new分配数组
for (int i = 0; i < n; i++)
{
cout << "输入arr[" << i << "]" << endl;
cin >> arr[i];
}
//float arr[] = {5,4,3,4,3,6,0};
int temp,b= n - 1;
for (int a = 0; a < b; a++)
{
for (int i = 0; i <b; i++)
{
if(arr[i+1]<arr[i])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
for (int q = 0; q < b + 1; q++)
{
cout << arr[q] << endl;
}
cout << "最大数字是" << arr[b] << endl;
system("pause");
return 0;
}
***调整后的冒泡排序***
```cpp
#include<iostream>
using namespace std;
int main()
{
int n,i,a,j;
float* p;
cout << "输入数组个数" << endl;
cin >> n;
p = new float [n];
cout << "输入数组" << endl;
for (i = 0; i < n; i++)
{
cin >> p[i];
}
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n - i- 1; j++)
{
if (p[j] > p[j + 1])
{
a = p[j];
p[j] = p[j + 1];
p[j + 1] = a;
}
}
}
cout << "end" << endl;
for (i = 0; i < n; i++)
{
cout << p[i] << " ";
}
cout << endl;
system("pause");
return 0;
}

数组反转
```cpp
#include<iostream>
using namespace std;
#include<string>
int main()
{
float* p;
int i, n;
cout << "输入数组个数" << endl;
cin >> n;
p = new float[n];
cout << "输入数组的元素,将逆序它" << endl;
for(i=0;i<n;i++)
{
cin >> p[i];
}
int start = 0, end = n-1,t;
while(start < end)
{
t = p[start];
p[start] = p[end];
p[end] =t;
end--; start++;
}
for (i = 0; i < n; i++)
{
cout << p[i] << endl;
}
system("pause");
return 0;
}
二维数组:
四种定义方式:
1.int a[3][5];
2.int a[2][3]={{1,2,3},{1,2,3}};
3.int a[2][3]={1,2,3,4,5,6};
4.int a[][3]={1,2,3,4,5,6}; ##写出列数,就可以了。
二维数组名字:
1.查看所占内存空间
2.查看首地址
int n,i,j;
int a[][3] = { 1,2,3,4,5,6 };
cout << sizeof(a) << endl;
cout << sizeof(a[0])<< endl;
cout << sizeof(a[1]) << endl;
cout<<(int)a<<endl;
cout << (int)a[0] << endl;
cout << (int)a[1] << endl;
cout << (int)&a[0][1] << endl;
int n,i,j;
int score[3][3] = {
{102,122,112},
{142,150,69 },
{112,123,131}
};
string names[3] = { "aa","dsd","dd" };
for (i = 0; i < 3; i++)
{
int sum = 0;
for (j = 0; j < 3; j++)
{
sum = sum + score[i][j];
}
cout << names[i] << "的分数是" << sum << endl;
}
函数:
1.定义
返回值类型 函数名 (参数)
{
语句
return 表达式;
}
2.调用。
**void jiaf(int a, int b)
{
int c ;
c = a;
a = b;
b = c;
return ;
}**
值传递时候,形参的改变,不会影响实参。
void 无返回值,写return.结尾。
A.函数常见形式
void text01()
{
cout << "无参无返回" << endl;
}
void text02(int a)
{
cout << "有参无返" <<a<< endl;
}
int test03()
{
cout << "无参有返" << endl;
return 10000;
}
int test04(int a)
{
cout << "有参有返" << endl;
return a;
}
函数的分文件编写:
有一天你编程能力牛逼了,就要分文件来处理大量代码:
1.创建头文件:
里写: 1.框架
2.函数声明
#include<iostream>
using namespace std;
int max(int a, int b);
2.创建源文件。
写 1.函数的定义
2.在前面写上你对应的头文件。
#include"1st.h"
int max(int a, int b)
{
return (a > b ? a : b);
}
之后就可以快乐的调用了。
在主函数的调用如下:
#include<iostream>
using namespace std;
#include"1st.h"
int main()
{
int c = max(1, 4);
cout << c << endl;
system("pause");
return 0;
}
————————————————————————————————
指针:
1.指针就是地址
2. * 表示解指针的意思。
int c = 20;
int* p;
p = &c;
前两行等价如下:
int*p=&c;
指针所占内存:
不管什么类型的指针:
在x86,32位编程下,指针占4个字节。
x64 64位编程下,指针占8个字节。
cout<<sizeof(int *)<< endl;
cout<<sizeof§<< endl;
A.空指针:
1.就是内存中编号为-0 的指针
2. 用途:初始化指针变量
3. 空指针指向的内存不可以访问。
原因是 : 0–255是系统使用的地址,不可使用。
int*p=NULL;
*p = 10;
////会报错的
cout << p<<" " << *p << endl;
野指针:
指针指向非法的内存空间
随便找了个内存就开始解引用。
会报错。
int*p=(int*)0x1100;
cout << p<<" " << *p << endl;
空指针和野指针都不是我们申请的空间,因此不要访问。
const修饰指针:
int a = 10, b = 20;
//指针常量,可修改内容,不可改指向。
int* const p = &a;
*p = b;
cout << *p << endl;
//常量指针,可修改地址,不可改内容。
const int* q = &a;
q = &b;
cout << *p << endl;
//既修饰指针,又修饰常量,都不可以改
const int* const w = &a;
数组指针:
(1) for(int i=0; i<100; i++)a[i]=0;
(2)for(int i=0; i<100; i++) {*b=0; b++;}
从原理上来讲,(1)是间接寻址再赋值,(2)是直接寻址赋值再累加;效率上(2)比(1)要高,速度更快。
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
for (int i = 0; i < 10; i++)
{
cout << *p << endl;
p++;
}
数组和函数:
void swap1(int a, int b)
{
//值传递,不会改变实参
int c;
c = a;
a = b;
b = c;
cout << "实参内部" << a << b<<endl;
}
void swap2(int* p, int* q)
{
//地址传递,会改变实参
int temp;
temp = *p;
*p = *q;
*q = temp;
cout << "实参内部" << *p << *q;
}
指针数组函数结合案例:
将数组进行升序排列:
#include<iostream>
using namespace std;
#include<string>
void Sortarr(int* p, int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (p[j] > p[j + 1])
{
int temp;
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
void Printarr(int*p,int len)
{
for (int i = 0; i < len; i++)
{
cout << p[i] << endl;
}
}
int main()
{
int arr[10] = { 1,5,3,6,3,6,2,5,7,4 }, len;
len = sizeof(arr) / sizeof(arr[0]);
Sortarr(arr, len);
Printarr(arr, len);
system("pause");
return 0;
}
——————————————————————————————————
结构体
1.创建结构体的三种方式:
#include<iostream>
using namespace std;
#include<string>
struct student
{
string name;
int score;
int age;
}s3;
int main()
{
//3种方式定义结构体
struct student s1;//stuct可以省略
s1.name = "发";
s1.age = 12;
s1.score = 100;
cout << s1.name << s1.age << " " << s1.score << endl;
//第二种
student s2 = { "sad",142,18 };
cout << s2.name << s2.age << " " << s2.score << endl;
//第三种在结构体后面
s3.name = "sf ";
s3.age = 14;
s3.score = 234;
cout << s3.name << s3.age << " " << s3.score << endl;
system("pause");
return 0;
}
2.结构体数组:
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
struct student
{
string name;
int score;
int age;
}s3;
int main()
{
//结构体数组
struct student stuArry[3] =
{
{"网易",132,15},
{"消息", 123, 16},
{"sad ",122,19 }
};
stuArry[2].age = 1;
stuArry[2].name = "adsadw";
for (int i = 0;i < 3; i++)
{
cout << stuArry[i].name << " " << stuArry[i].age
<< " " << stuArry[i].score << endl;
}
system("pause");
return 0;
}
#include //解决system不明确问题。
3.结构体指针。
//结构体 指针可以通过->访问元素
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
struct student
{
string name;
int score;
int age;
};
int main()
{
//结构体 指针可以通过->访问元素
struct student s1 = { "网易",132,15 };
student* p = &s1;
p->age = 100000;
cout << p->age << p->name << p->score << endl;
system("pause");
return 0;
}
结构体镶套结构体:
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
struct student
{
string name;
int score;
int age;
};
struct teacher
{
string name;
int id;
struct student s1;
};
int main()
{
struct teacher t;
t.id = 1;
t.name = "老王";
t.s1={ "网易",132,15 };
cout << "老师信息"<<t.id <<t.name << t.s1.age<<t.s1.name<<t.s1.score << endl;
system("pause");
return 0;
}
结构体做函数参数:
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
struct student
{
string name;
int score;
int age;
};
void y1(struct student s)
{
s.name = "阿小吉";
cout << "值传递函数的结果 " << s.name << " " << s.age << " " << s.score << endl;
}
void y2(struct student* s)
{
s->name = "啊小吉";
cout << "地址传递的结果 " << s->name << " " << s->age << " " << s->score << endl;
}
int main()
{
struct student s1 = { "阿吉",685,20 };
y1(s1);
cout << "主函数的结果 " << s1.name << " " << s1.age << " " << s1.score << endl;
y2(& s1);
cout << "主函数的结果 " << s1.name << " " << s1.age << " " << s1.score << endl;
system("pause");
return 0;
}
结果:

可以再次见证:
函数的值传递不可改变函数的数据,函数的地址传递可以改变函数的数据。
——————————
const 修饰结构体的使用场景
void y2( const student* s) //注意此处的格式,防止程序的误操作。
{
//使用地址传递可以节省空间,值传递得复制一大串,地址传递就不用,只用4个字节。
cout << "地址传递的结果 " << s->name << " " << s->age << " " << s->score << endl;
}
结构体案例1
一个老师带5个学生,一共仨老师,定义老师结构体*(姓名,带的学生信息)
学生结构体(姓名,得分),代码如下。
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
#include<ctime>
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student sArry[5];
};
void matchall(struct teacher tArry[], int len)//注意此处tArry[]!!!
{
string refer = "ABCDE";
for (int i = 0; i < len; i++)
{
tArry[i].name = "teacher_";
tArry[i].name += refer[i];
for (int j = 0; j < 5; j++)
{
tArry[i].sArry[j].name = "student_";
tArry[i].sArry[j].name+= refer[j];
tArry[i].sArry[j].score = rand() % 60 + 1 + 40;
}
}
}
void printfall(struct teacher tArry[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老师信息: 姓名" << tArry[i].name << endl;
cout << "\t学生信息: " << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t\t姓名" << tArry[i].sArry[j].name << " 得分"
<< tArry[i].sArry[j].score<<endl;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
struct teacher tArry[3];
int len = sizeof(tArry) / sizeof(tArry[0]);
matchall(tArry,len);
printfall(tArry, len);
system("pause");
return 0;
}
结果如下:

结构体案例2:
对5个英雄进行按照年龄的冒泡排序。代码如下:
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
#include<ctime>
struct hero
{
string name;
int age;
string sex;
};
void merge(struct hero onehero[])//注意此处tArry[]!!!
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4-i; j++)
{
struct hero temp;
if (onehero[j].age > onehero[j + 1].age)
{
temp = onehero[j];
onehero[j] = onehero[j + 1];
onehero[j + 1] = temp;
}
}
}
}
void prints(struct hero sa[])
{
for (int i = 0; i < 5; i++)
{
cout << sa[i].name << " " << sa[i].age << " " << sa[i].sex << endl;
}
}
int main()
{
struct hero heros[5] = { {"关羽",23,"男"},{"赵云",20,"男"},{"刘备",29,"男"},
{"张飞",23,"男"},{"貂蝉",19,"女"} };
merge(heros);
prints(heros);
system("pause");
return 0;
}
result:

本文详细介绍了C++编程的基础知识,包括数据类型、运算符、流程控制、数组、函数、指针、结构体等内容,通过实例演示了C++编程的实践技巧。
11万+





