lxy_蓝桥杯C++学习_系列二:函数用法及String相关知识

一、函数的常见用法
1.1 无参数无返回值的函数

点击查看代码
#include <iostream>
using namespace std;

void sayHello(){
    cout << "Hello,World!" << endl ;
}

int mmain(){
    sayHello();
    
    return 0;
}

1.2有参数有返回值的函数

点击查看代码
#include <iostream>
using namespace std;

// 计算两数的Max值
int getMax(int x, int y) {
    if (x > y) {
        return x;
    } else {
        return y;
    }
}

int main() {
    int a = 10, b = 20;
    int maxValue = getMax(a, b);
    cout << "最大值是:" << maxValue << endl;
    
    return 0;
}

1.3多参数函数

点击查看代码
#include <iostream>
using namespace std;

// 计算三个数的平均值
double calculateAverage(int num1, int num2, int num3) {
    double sum = num1 + num2 + num3;
    return sum / 3.0;
}

int main() {
    int a = 80, b = 90, c = 85;
    double avg = calculateAverage(a, b, c);
    cout << "平均分是:" << avg << endl;

    return 0;
}

1.4函数的声明与定义分离
有时我们需要先声明函数,后定义函数:

点击查看代码
#include <iostream>
using namespace std;

// 函数声明
int multiply(int a, int b);

int main() {
    int result = multiply(5, 6);
    cout << "5 × 6 = " << result << endl;
    return 0;
}

// 函数定义
int multiply(int a, int b) {
    return a * b;
}

1.5函数重载

点击查看代码
#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

double add(double a, double b) {
    return a + b;
}

int main() {
    cout << add(3, 4) << endl;        // 输出7
    cout << add(3.5, 4.5) << endl;    // 输出8.0

    return 0;
}

函数add根据传入参数的类型不同,表现出不同的行为。

二、String
2.1string简介
主要用于字符串处理,在使用string库记得#include
string与char[]的区别:string实现了高度封装,可以方便完成各种字符串操作,类于拼接,获取,匹配……

  1. 字符串管理:string封装了字符串的存储和管理。
  2. 动态大小调整:string可以根据需要自动调整字符串的天小。在添加或删除字符时,string会自动调整内部的存储容量,确保足够的空间来容纳字符串;
  3. 安全性:string提供了一些方法来确保字符串的安全性(越界访问检查);
  4. 迭代器支持:sfring支持迭代器,可以使用迭代器遍历字符串中的字符,进行字符级别的操作;
  5. 兼容性:string是C++标准库的一部分,因此在C++中广泛使用,并且与其他标准库组件和C++语言特性兼容。

2.2string声明与初始化

点击查看代码
#include <iostream>
#include <string>

int main(){
//声明并初始化一个空字符串
    std::string str1;
//使用字符串字面量初始化字符串
    std::string str2 = "Hello, world!";
    
//使用另一个 std;:string 对象来初始化字符串
    std::string str3 = str2;
//使用部分字符串初始化字符串
//substr(起始位置,长度)
    std::string str4= str2.substr(0,5);
//使用字符数组初始化字符串
    const char* charArray = "Hello";
    std::string str5(charArray);
//使用重复的字符初始化字符串
    std::string str6(5,'A');

// 输出字符串内容
    std::cout<<"str1:"<< str1 << std::endl;
    std::cout<<"str2:"<< str2<< std::endl;
    std::cout<<"str3:"<< str3 << std::endl;
    std::cout<<"str4:"<< str4 <<std::end1;
    std::cout<<"str5:"<< str5 << std::endl;
    std::cout<<"str6:"<< str6 << std::endl;

    return 0;

}

之前还提过另一种读入一行字符串的方式:getline(cin, s);

2.3string基本操作

在C++中,std::string类提供了一个成员函数c-str()用于返回一个指向以空字符结尾的C风格字符串即const char*类型).
在进行printf输出时,需要将string转换为C风格的字符串进行输出。

  1. 获取字符串长度(length/size);
  2. 拼接字符串(+ 或 append);
  3. 字符串查找(find);
  4. 字符串替换(replace);
  5. 提取子字符串(substr);
  6. 字符串比较(compare).
    Tip:字典序的比较方法是从小到大一个一个比较,相等的字符就确定大小关系。例如:azzzzzz<b

常用的遍历string的方法有两种:1.循环枚举下标;
2.auto枚举(其中&表示取引用类型,如果对i修改将会改变原来的值)。

posted @ 2025-12-01 19:47  Liiao  阅读(4)  评论(0)    收藏  举报