c++复习_第一天(引用+小众考点)

https://en.cppreference.com/w/cpp/io/manip

参考一下,这一部分比较基础,所以就一遍过

eg1:转16进制

复制代码
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    int n;
    cout << "请输入一个整数:";
    cin >> n;

    cout << "十六进制输出为:" << showbase << hex << n << endl;
    return 0;
}

eg2:表格对齐

cpp 复制代码
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    // 表头
    cout << left << setw(10) << "姓名"
         << right << setw(5) << "年龄"
         << setw(8) << "成绩" << endl;

    // 学生信息
    cout << left << setw(10) << "张天一"
         << right << setw(5) << 19
         << setw(8) << 85.5 << endl;

    cout << left << setw(10) << "Alice"
         << right << setw(5) << 21
         << setw(8) << 90.0 << endl;

    cout << left << setw(10) << "欧阳中天"
         << right << setw(5) << 22
         << setw(8) << 78.2 << endl;

    return 0;
}

eg3:设置精度

cpp 复制代码
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    double num;
    cout << "请输入一个浮点数: ";
    cin >> num;

    //设置精度为3
    cout << fixed << setprecision(3);
    cout << "保留三位小数:" << num << endl;

    //恢复默认精度
    cout.unsetf(ios::fixed);
    cout << setprecision(6); //恢复默认精度
    cout << "恢复默认精度" << num << endl;
    return 0;
}

e g4:小数点

cpp 复制代码
//保留小数点后3位
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    double num;
    cout << "请输入一个浮点数:";
    cin >> num;

    cout << fixed << setprecision(3);
    cout << "保留3位小数的输出结果为:" << num << endl;

    return 0;
}

eg5://值传递:不会真正交换主函数中的变量

//地址传递,使用指针,交换主函数变量的值

//引用传递,最简单最安全的方式,直接交换主函数变量

cpp 复制代码
#include<iostream>
using namespace std;

//值传递:不会真正交换主函数中的变量
void swapByValue(int a ,int b){
    int temp = a;
    a = b;
    b = temp;
}

//地址传递,使用指针,交换主函数变量的值
void swapByAddress(int *a,int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

//引用传递,最简单最安全的方式,直接交换主函数变量
void swapByReference(int& a,int& b)
{
    int temp = a;
    a = b;
    b = temp;
}
int main()
{
    int x =10,y = 20;
    //值传递
    cout << "初值: x =  " << x <<", y = " << y << endl;
    swapByValue(x,y);
    cout << "main中值传递后: x =  " << x <<", y = " << y << endl;
    //地址传递
    swapByAddress(&x,&y);
    cout << "main中地址传递后: x =  " << x <<", y = " << y << endl;
    //引用传递
    swapByReference(x,y);
    cout << "main中引用传递后: x =  " << x <<", y = " << y << endl;

    return 0;
}

eg6:函数重载

cpp 复制代码
//重载(overload)函数就是在同一个作用域内几个函数名字相同但形参列表不同。一个函数名对应多个不同的代码。
#include<iostream>
using namespace std;

void add(int x,int y)
{
    cout << "int:" << x + y << endl;
}
void add(float x)
{
    cout << "float: " << 10 + x << endl;
}
double add(double x,double y)
{
    return x + y;
}

int main()
{
    add(10.2);
    add(1,3);
    return 0;
}

e g7:默认参数

cpp 复制代码
#include<iostream>
using namespace std;

void greetings(string name,string greeting = "Hello")
{
    cout << greeting << "," << name << endl;
}

int main()
{
    greetings("Alice");
    greetings("Bob","Hi");

    return 0;
}

e g8:函数重载

cpp 复制代码
#include<iostream>
#include<cmath>
#define PI 3.14
using namespace std;

double caculateArea(double radius)
{
    return PI * radius * radius;
}

double caculateArea(double length,double width)
{
    return length * width;
}

double caculateArea(double a ,double b,double c)
{
    double s = (a + b + c)/2.0;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

int main()
{
    cout << "圆的面积" << caculateArea(5.0) << endl;

    cout << "矩形面积" << caculateArea(4.0,6.0) << endl;

    cout << "三角形面积" << caculateArea(3.0,4.0,5.0) << endl;

    return 0;
}

eg9:字符串

cpp 复制代码
 #include<iostream>
 #include<string>
 using namespace std;
 int main()
 {
    string s;
    //cin >> s; //无法读取含有空格的字符串
    getline(cin,s); //这个就可以读取一行的
    cout << s.length() << endl;
    cout << s.front() << "," << s.back() << endl;
    for(auto& ch : s)
    if(ch == 'a') ch = 'A';
    cout << s << endl;
    return 0;

 }
相关推荐
行者962 分钟前
Flutter适配OpenHarmony:国际化i18n实现中的常见陷阱与解决方案
开发语言·javascript·flutter·harmonyos·鸿蒙
csbysj20204 分钟前
RSS 阅读器:全面解析与使用指南
开发语言
xie_pin_an8 分钟前
C++ 从入门到进阶:核心知识与实战指南
java·c++·算法
溪海莘11 分钟前
如何部署使用uv管理依赖的python项目 ?
开发语言·python·uv
Python_Study202511 分钟前
制造业数据采集系统选型指南:从技术挑战到架构实践
大数据·网络·数据结构·人工智能·架构
我送炭你添花13 分钟前
Python与串口:从基础到实际应用——以Pelco KBD300A模拟器项目为例
开发语言·python·自动化·运维开发
No0d1es13 分钟前
2025年12月 GESP CCF编程能力等级认证C++八级真题
开发语言·c++·青少年编程·gesp·ccf
hqwest32 分钟前
码上通QT实战10--监控页面02-绘制温度盘
开发语言·qt·自定义控件·qwidget·提升部件·qt绘图
fqbqrr34 分钟前
2601C++,概念与约束及推导本
c++
m0_6265352040 分钟前
快速排序学习 l方法 h方法
开发语言·python