当前位置: 首页 > news >正文

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:表格对齐

#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:设置精度

#include<iostream>
#include<iomanip>
using namespace std;int main()
{double num;cout << "请输入一个浮点数: ";cin >> num;//设置精度为3cout << fixed << setprecision(3);cout << "保留三位小数:" << num << endl;//恢复默认精度cout.unsetf(ios::fixed);cout << setprecision(6); //恢复默认精度cout << "恢复默认精度" << num << endl;return 0;
}

e g4:小数点

//保留小数点后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://值传递:不会真正交换主函数中的变量

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

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

#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:函数重载

//重载(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:默认参数

#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:函数重载

#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:字符串

 #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;}

相关文章:

  • 2025吉林CCPC 题解(前六题)
  • 【NLP基础知识系列课程-Tokenizer的前世今生第四课】生物信息中的 Tokenizer 策略:如何切开一段基因?
  • 【NLP基础知识系列课程-Tokenizer的前世今生第五课】从静态到可学:Tokenizer 的自适应演化之路
  • C/C++的OpenCV的锐化
  • ojs导入显示空白页错误信息
  • Ubuntu 下搭建ESP32 ESP-IDF开发环境,并在windows下用VSCode通过SSH登录Ubuntu开发ESP32应用
  • 【android bluetooth 协议分析 02】【bluetooth hal 层详解 6】【高通蓝牙hal主要流程介绍-下】
  • Jmeter一些元件使用的详细记录
  • 详解MYSQL索引失效问题排查
  • 如何描述BUG
  • ubuntu22.04 安装 SecureCRT8.7.3
  • 5.0.7 TabControl的使用
  • Java+Playwright自动化-2-环境准备与搭建-基于Maven
  • ViewModel
  • 设计模式系列(06):抽象工厂模式(Abstract Factory)
  • Vue组件化
  • 用AxumStatusCode细化Rust Web标准格式响应
  • [Vue]浅浅了解vue3响应式的基本原理
  • Jenkins实践(9):pipeline构建历史展示包名和各阶段间传递参数
  • 使用 scikit-learn 库对乌克兰冲突事件数据集进行多维度分类分析
  • 网站空间服务器供应商/青岛seo博客
  • 昆明有几个区/北京百度seo服务
  • 新变更营业执照注册号查了发现之前有备案过网站了/站长工具爱情岛
  • 上海建设人才网站/手游推广加盟
  • 互联网运营公司排行榜/北京企业网站seo平台
  • 男人和女人做羞羞的免费网站/站长工具查询网