【C++】探索C++库函数的奇妙世界:深入了解如何发挥其强大功能

简介: 【C++】探索C++库函数的奇妙世界:深入了解如何发挥其强大功能

欢迎来到编程要点!这是一个揭示编程精髓的地方。在这里,我分享基础原则、高效技巧,以及实际项目中的宝贵经验。无论你经验如何,这里都将为你呈现编程的精妙之处。准备好了吗?让我们一同创造属于我们的编程奇迹吧!

前言:

#include <cstring>#include <string.h> 是在C++中用于包含C标准库中字符串处理相关函数的两个头文件。它们本质上是相同的,只是一个是C++标准库的版本,另一个是C标准库的版本。

<cmath><math.h> 都是C++中用于数学操作的头文件,但它们有一些区别。

  1. 命名空间:<cmath> 是C++标准库中的头文件,其所有内容位于std命名空间中。而 <math.h> 是C语言标准库中的头文件,其内容不在命名空间中,而是全局作用域。
  2. C++标准兼容性:<cmath> 是C++标准中引入的头文件,而 <math.h> 是C语言标准中的头文件。前者兼容后者。
  3. 异常处理:<cmath> 中的数学函数通常提供了异常处理机制,例如对于非法的输入,可能会抛出异常。而 <math.h> 中的数学函数通常使用C语言的错误处理机制,如设置全局变量 errno 来表示错误。


🌌1. cmath

以下是一些常见的 <cmath> 方法:

  1. sqrt: 计算平方根。
    double sqrt(double arg);
  2. pow: 计算指定数字的指定次方。
    double pow(double base, double exponent);
  3. fabs: 计算浮点数的绝对值。
    double fabs(double arg);
  4. sin, cos, tan: 计算正弦、余弦和正切值。
    double sin(double arg); double cos(double arg); double tan(double arg);
  5. log, log10: 分别计算自然对数和以10为底的对数。
    double log(double arg); double log10(double arg);
  6. exp: 计算指数函数。
    double exp(double arg);
  7. floor, ceil: 分别向下取整和向上取整。
    double floor(double arg); double ceil(double arg);
  8. round: 四舍五入到最近的整数。
    double round(double arg);
  9. fmod: 计算浮点数的余数。
    double fmod(double numerator, double denominator);

注意:

  • sqrt 接受的参数是 double 类型。如果传递一个整数给 sqrt,C++ 会自动进行类型转换,将整数转换为 double。这是因为 sqrt 函数是为浮点数设计的,C++通过自动类型转换确保了参数的正确类型。
  • 在使用其他函数时,也要确保传递的参数类型正确,避免因为类型不匹配而导致错误。
  • 对于一些数学函数,参数的范围可能有限,超出范围可能导致未定义的行为。在使用这些函数时,需要注意参数的有效范围。
  • 对于浮点数运算,注意舍入误差可能导致精度损失。在对精度要求高的场景中,可能需要采取额外的措施来处理这些误差。

示例程序:

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    // sqrt:计算平方根
    double squareRoot = sqrt(25.0);
    cout << "Square Root of 25: " << squareRoot << endl;
    // pow:计算指定数字的指定次方
    double powerResult = pow(2.0, 3.0);
    cout << "2^3: " << powerResult << endl;
    // fabs:计算浮点数的绝对值
    double absoluteValue = fabs(-10.5);
    cout << "Absolute Value of -10.5: " << absoluteValue << endl;
    // sin, cos, tan:计算正弦、余弦和正切值
    double angle = 45.0;
    cout << "Sin(45): " << sin(angle) << endl;
    cout << "Cos(45): " << cos(angle) << endl;
    cout << "Tan(45): " << tan(angle) << endl;
    // log, log10:计算自然对数和以10为底的对数
    double naturalLog = log(2.71828);
    cout << "Natural Log of 2.71828: " << naturalLog << endl;
    double logBase10 = log10(100.0);
    cout << "Log base 10 of 100: " << logBase10 << endl;
    // exp:计算指数函数
    double exponentResult = exp(1.0);
    cout << "e^1: " << exponentResult << endl;
    // floor, ceil:向下取整和向上取整
    double floorResult = floor(3.8);
    cout << "Floor of 3.8: " << floorResult << endl;
    double ceilResult = ceil(3.2);
    cout << "Ceil of 3.2: " << ceilResult << endl;
    // round:四舍五入到最近的整数
    double roundedValue = round(4.6);
    cout << "Rounded Value of 4.6: " << roundedValue << endl;
    // fmod:计算浮点数的余数
    double remainder = fmod(10.5, 3.0);
    cout << "Remainder of 10.5 / 3.0: " << remainder << endl;
    return 0;
}

程序结果:


🌌2. string.h

常用方法:

  1. puts函数:
    功能:输出字符串。
    注意事项:自动在输出的字符串末尾添加换行符。
  2. gets函数:
    功能:输入字符串。
    注意事项:不检查输入字符串的长度,可能导致缓冲区溢出,已被弃用,建议使用fgets。
  3. strcat函数:
    功能:连接两个字符串。
    注意事项:不会检查目标数组是否有足够的空间,可能导致溢出。
  4. strcpy函数:
    功能:复制一个字符串到另一个字符串。
    注意事项:不会检查目标数组是否有足够的空间,可能导致溢出。
  5. strcmp函数:
    功能:比较两个字符串。
    注意事项:返回值为0表示相同,大于0表示第一个不同字符在ASCII码中更大,小于0表示第一个不同字符在ASCII码中更小。
  6. strlwr函数:
    功能:将字符串转换为小写。
  7. strupr函数:
    功能:将字符串转换为大写。
#include <stdio.h>
#include <string.h>
int main() {
    // puts函数示例
    char str_puts[] = "Hello, World!";
    puts(str_puts);  //Hello, World!
    // gets函数示例
    char str_gets[100];
    printf("Enter a string: ");//输入:abc
    gets(str_gets);
    printf("You entered: %s\n", str_gets);//abc
    // strcat函数示例
    char dest_strcat[20] = "Hello, ";
    char src_strcat[] = "World!";
    strcat(dest_strcat, src_strcat);
    printf("strcat result: %s\n", dest_strcat);//Hello, World!
    // strcpy函数示例
    char dest_strcpy[20];
    char src_strcpy[] = "Hello, World!";
    strcpy(dest_strcpy, src_strcpy);
    printf("strcpy result: %s\n", dest_strcpy);//Hello, World!
    // strcmp函数示例
    char str1_strcmp[] = "apple";
    char str2_strcmp[] = "banana";
    int result_strcmp = strcmp(str1_strcmp, str2_strcmp);
    printf("strcmp result: %d\n", result_strcmp);//-1
    // strlwr函数示例
    char str_strlwr[] = "Hello, World!";
    strlwr(str_strlwr);
    printf("strlwr result: %s\n", str_strlwr);//strlwr result: hello, world!
    // strupr函数示例
    char str_strupr[] = "Hello, World!";
    strupr(str_strupr);
    printf("strupr result: %s\n", str_strupr);//strupr result: HELLO, WORLD!
    return 0;
}

🌍2.1 解决方案

strcat()字符数组的连接解决溢出方案

#include <stdio.h>
#include <string.h>
char * merge(char * p1,char * p2){
    char * result=malloc(strlen(p1)+strlen(p2)+1);
    strcpy(result,p1);
    strcat(result,p2);
    return result;
}
int main(){
    const int MaxSize=100;
    char ch1[MaxSize],ch2[MaxSize];
    fgets(ch1,MaxSize,stdin);
    fgets(ch2,MaxSize,stdin);
    // 去除换行符
    ch1[strcspn(ch1, "\n")] = '\0';
    ch2[strcspn(ch2, "\n")] = '\0';
    char *p1=ch1,*p2=ch2;
    char *result=merge(p1,p2);
    printf("%s",result);
}
目录
相关文章
|
20天前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
19 2
|
16天前
|
编译器 C语言 C++
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
19 0
|
18天前
|
C++
glog --- C++日志库
glog --- C++日志库
|
26天前
|
XML JSON JavaScript
推荐一个比较好用的c++版本http协议库-cpp-httplib
推荐一个比较好用的c++版本http协议库-cpp-httplib
38 1
|
27天前
|
存储 缓存 C++
C++链表常用的函数编写(增查删改)内附完整程序
C++链表常用的函数编写(增查删改)内附完整程序
|
29天前
|
存储 安全 编译器
【C++】类的六大默认成员函数及其特性(万字详解)
【C++】类的六大默认成员函数及其特性(万字详解)
35 3
|
1月前
|
安全 程序员 C++
【C++ 基本知识】现代C++内存管理:探究std::make_系列函数的力量
【C++ 基本知识】现代C++内存管理:探究std::make_系列函数的力量
102 0
|
1月前
|
设计模式 安全 C++
【C++ const 函数 的使用】C++ 中 const 成员函数与线程安全性:原理、案例与最佳实践
【C++ const 函数 的使用】C++ 中 const 成员函数与线程安全性:原理、案例与最佳实践
71 2
|
1月前
|
安全 网络性能优化 Android开发
深入解析:选择最佳C++ MQTT库的综合指南
深入解析:选择最佳C++ MQTT库的综合指南
90 0
|
1月前
|
XML 运维 监控
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
【深入探究 C++ 日志库清理策略】glog、log4cplus 和 spdlog 的日志文件管理策略
68 0