C++基础语法入门

C++基本代码

#include <iostream>
using namespace std;

int main(){
    cout << "Hello World" << endl;
    printf("Hello World");
}

常量定义方式

#define宏常量

#define a 15

const修饰的变量

const int a = 15

标识符命名规则

  • 标识符不能是关键字
  • 标识符只能由字母、数字、下划线组成
  • 第一个字符必须为字母或下划线
  • 标识符中字母区分大小写
  • 命名时建议达到见名知意的效果

sizeof()用法

short num = 10;
cout << sizeof(num) << endl;
//变量num的字节数为2

整型结论:short < int <= long <= long long

转义字符

  • \n 换行符
  • \ \ 输出反斜杠
  • \t 水平制表符,可以整齐地输出数据

字符串型

//C风格
char str1[] = "aaa";

//C++风格(要包含头文件)
#include <string>
string str2 = "aaa";

三目运算符

//方式1
a > b ? c = a : c = b;
//方式2
c = a > b ? a : b;
//三目运算符返回的是变量
a > b ? a : b = 100

swich语句

switch(表达式)
{
  //若没有break,则该分支及以下分支的语句都会执行
  //只能判断字符型和整型,不可以是区间
  case 结果1:执行语句;break;
  case 结果2:执行语句;break;
  ......
  default:执行语句;break;
}

3位水仙花数

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

//3位水仙花
bool shui_xian_hua(int n){
    int new_n = n;
    vector<int> a;
    while(n){
        a.push_back(n % 10);
        n = n / 10;
    }
    int sum = 0;
    for(int i = 0;i < a.size();i++){
        sum = sum + pow(a[i],3);
    }
    //注:这里一定要和new_n比较,因为n已经为0了
    if(sum == new_n){
        return true;
    }

}

int main(){
    for(int i = 100;i <= 999;i++){
        if(shui_xian_hua(i)){
            cout << i << ' ';
        }
    }
}

三角型乘法表

#include <iostream>
using namespace std;

//乘法表
void Multi_table(){
    //i为行数,j为列数
    for(int i = 1;i <= 9;i++){
        for(int j = 1;j <= i;j++){
            cout << j << 'x' << i << '=' << j*i << ' ';
            if(i == j){
                cout << endl;
            }
        }
    }
}

int main(){
    Multi_table();
}

数组长度计算方法

int arr[5] = {1,2,3,4,5};
cout << sizeof(arr) / sizeof(arr[0]) << endl;

冒泡排序

#include <iostream>
using namespace std;

//冒泡排序:每一轮将该轮次的最大值冒泡
void mao_pao(int a[],int n){
    //i表示轮次
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n - i - 1;j++){
          //比较相邻元素
            if(a[j] > a[j + 1]){
                int t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
    for(int i = 0;i < n;i++){
        cout << a[i] << ' ';
    }
}

int main(){
    int a[9] = {4,2,8,0,6,7,1,3,9};
    //传数组名就可以
    mao_pao(a,9);
}

函数分文件编写

  1. 创建.h文件(要写#include 和using namespace std;等)
  2. 创建.cpp文件并在.cpp文件中引入.h文件
  3. 在.h文件中写函数声明
  4. 在.cpp文件中写函数内容

指针

int a = 10;
int b = 20;
//常量指针:不能给*p赋值(但能给a赋值),但能给p赋值
const int *p = &a;
//指针常量:能给*p赋值,但不能给p赋值
int * const p = &a;

指针和函数

#include <iostream>
using namespace std;

void swap(int *p1,int *p2){
    int t = *p1;
    *p1 = *p2;
    *p2 = t;
}

int main(){
    int a = 10;
    int b = 20;
    //地址传递
    swap(&a,&b);
    cout << a << endl << b;
    //a = 20,b = 10
}
#include <iostream>
using namespace std;

//指针形式:int *数组名
//数组形式:int 数组名[]
void func(int *a,int n){

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n - i - 1;j++){
            if(*(a + j) > *(a + j + 1)){
                int t = *(a + j);
                *(a + j) = *(a + j + 1);
                *(a + j + 1) = t;
            }
        }
    }

    for(int i = 0;i < n;i++){
        cout << *(a + i) << ' ';
    }
}

int main(){
    int a[10] = {4,3,6,9,1,2,10,8,7,5};

    func(a,10);
}

结构体

结构体的基本使用

#include <string>
//在main函数外定义结构体
struct Student{
  string name;
  int age;
  int score;
}s3;

//第一种定义结构体变量方式
//创建变量时,struct关键字可以省略
struct Student s1;  //Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;

//第二种定义结构体变量方式
struct Student s2 = {"李四",19,80};

//结构体数组
Student arr[] = {
  {"张三",18,100},
  {"李四",19,80}
};

//结构体指针
Student *p = &s2;
cout << p->name << endl;  //李四


结构体的嵌套使用

#include <string>
struct student{
  string name;
  int age;
  int score;
};
struct teacher{
  int id;
  string name;
  int age;
  struct student stu; //层层访问即可
};

posted @ 2022-02-19 21:32  优雅永不过时、  阅读(145)  评论(0)    收藏  举报