数组又来力

string nums[5] = {"a", "b", "c", "d", "e"};

// 这两种写法是等价的:
string *p1 = &nums[0];    // 取第一个元素的地址 ✅
string *p2 = nums;        // 数组名自动转换为首元素指针 ✅

cout << (p1 == p2);       // 输出 1 (true),它们指向同一个地址

🚫 引用的数组 - 不存在!

语法:int &refs[10] ❌

意思:创建一个数组,数组的每个元素都是引用

为什么不存在?

✅ 数组的引用 - 存在且有用

语法:int (&arrRef)[10] ✅

意思:创建一个引用,这个引用指向整个数组

指针竟然也能运算!

int rb[5] = {1,2,3,4,5};
int *luv1 = rb;
int *luv2 = *luv1 +4;
//luv1访问的是【0】;luv2访问的是【4】
begin();//数组的首元素
end();//数组的尾元素的下一个位置

解引用,在等式的右边;

int rb[] = "2,4,6,8,10";
int everlast = *(rb +4);//equals to everlast=10!

代替C风格字符串:

#include <string>
using namespace std;

string s1 = "Hello";
string s2 = "World";

s1.length();        // 代替 strlen
s1.compare(s2);     // 代替 strcmp  
s1 += s2;           // 代替 strcat
s1 = s2;            // 代替 strcpy

建立数组和vector的桥梁:

int rb[] = {1,2,3,4,5};
vector <int> hip(begin(rb),end(rb));//so hip completely equals to rb!

数组很多操作都是通过指针间接地完成的!所以有时我们能看到的arr其实是指向arr第一个元素的指针嘞。

PS我的漏洞:

如何用指针访问对象成员

#include <iostream>
#include <string>
using namespace std;

class Student {
public:
    string name;
    int score;
    
    void showInfo() {
        cout << name << "的成绩是:" << score << endl;
    }
};

int main() {
    // 创建对象和指针
    Student stu;
    Student *pStu = &stu;
    
    // 三种访问方式(效果相同):
    
    // 1. 对象直接访问
    stu.name = "张三";
    stu.score = 95;
    stu.showInfo();
    
    // 2. 先解引用再访问
    (*pStu).name = "李四";
    (*pStu).score = 88;
    (*pStu).showInfo();
    
    // 3. 箭头运算符(最简洁!)
    pStu->name = "王五";
    pStu->score = 92;
    pStu->showInfo();
    
    return 0;
}

char = 字符类型

2. case 值:

4. default:

  • 专门用来存一个字母、一个数字、一个符号

  • 比如:'A''b''5''!'' '(空格)

  • 常见陷阱:

    陷阱1:单引号 vs 双引号

  • char correct = 'A';      // ✅ 正确:单个字符用单引号
    char wrong = "A";        // ❌ 错误:"A"是字符串,不是字符

    陷阱2:字符 vs 数字

  • char asChar = '5';       // 存储字符'5'(ASCII值53)
    int asNumber = 5;        // 存储数字5
    
    cout << asChar + 1;      // 输出:54(53+1)
    cout << asNumber + 1;    // 输出:6(5+1)

    switch和case的使用(我简称为分类加一小游戏)

  • #include <iostream>
    using namespace std;
    
    int main() {
        char grade;
        int aCount = 0, bCount = 0, cCount = 0, otherCount = 0;
        
        cout << "输入成绩等级 (A/B/C),输入Q结束:";
        while (cin >> grade && grade != 'Q') {
            switch (grade) {
            case 'A':
            case 'a':           // 大小写都统计
                aCount++;
                cout << "优秀!" << endl;
                break;
            case 'B':
            case 'b':
                bCount++;
                cout << "良好!" << endl;
                break;
            case 'C':
            case 'c':
                cCount++;
                cout << "及格!" << endl;
                break;
            default:
                otherCount++;
                cout << "无效等级!" << endl;
                break;
            }
        }
        
        cout << "\n统计结果:" << endl;
        cout << "A级: " << aCount << endl;
        cout << "B级: " << bCount << endl;
        cout << "C级: " << cCount << endl;
        cout << "其他: " << otherCount << endl;
        
        return 0;
    }

    详细解释每个部分:

  • 1. switch (表达式)

  • 括号里的表达式会被计算一次

  • 结果与各个 case 的值进行比较

  • 如果表达式的值等于这个 case 的值,就执行后面的代码

  • 注意:值必须是常量(不能是变量)

  • 3. break;

  • 非常重要! 告诉程序:"到这里结束,跳出switch"

  • 如果没有 break,程序会继续执行下一个 case(这叫做"fall through")

  • 可选的"兜底"情况

  • 当所有 case 都不匹配时执行

  • 像if else里面的else

理解do while

//先do 后看condition
string rb;
do
{
  cout<<"do u want to continue?";
  cin>>rb;
 }
while(!rb.empty()&&rb[0] != 'n'&&rb[0] !='N');
//特别注意,rb[i]相当于一个char,可以用单引号嘞
    

continue语句

for (初始化; 条件; 更新) {
    // 一些代码...
    if (某个条件) {
        continue;  // 跳过这次循环的剩余部分
    }
    // 如果continue执行了,这里的代码不会运行
}

成绩处理(跳过不及格)

vector<int> scores = {85, 45, 90, 55, 78, 30};
int passCount = 0;

for (int score : scores) {
    if (score < 60) {
        continue;  // 跳过不及格成绩
    }
    passCount++;   // 只统计及格人数
    cout << "及格分数: " << score << endl;
}
cout << "及格人数: " << passCount << endl;

加深理解

if (85 < 60) → false  // 不执行continue
passCount++           // passCount变成1
cout << "及格分数: 85" // 输出

if (45 < 60) → true   // 执行continue
// continue跳过了后面的所有代码!
// passCount++ 被跳过!
// cout 被跳过!
// 直接进入第3次循环

continue 的作用就是:

"如果遇到我不想要的数据,就立即跳过这次循环的所有后续操作,直接处理下一个数据"

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐