C++初入(四)

1.万能头文件

cpp 复制代码
#include <bits/stdc++.h>

里面包含了大量我们日常所需的头文件,如果使用它,我们就可以减少大量时间去写头文件,但是其实在平常练习和实际运用中,该头文件几乎没有实际价值,原因:1.里面虽然包含大量头文件,但是并不全面 2.对于我们学习过程中不利于养成好的习惯。所以,如果你是为了打比赛节省时间,那么使用该头文件不失为一种好的方法,但建议平时学习过程中还是自己动手为主,实在忘了头文件,可以去下面这个网址上查找,而非因小失大:

cplusplus.com - The C++ Resources Network

下面我们也把该头文件所包含的内容写出来,大家可以适当了解下!

cpp 复制代码
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
 
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

注:转载自【C++】万能头文件 <bits/stdc++.h> 的用法和优缺点-CSDN博客

2.取消同步流操作

相信大家很多人对此了解不是很多,什么是取消同步流呢?

说简单点就是加快cin/cout读取速度!

比较与printf/scanf,cin/cout读取速度是有明显的降低的,原因便在于:对于cin/cout来说,内部读写需要自动判断变量类型以及其他种种原因浪费大量时间,而printf/scanf则不同,它们是明确了类型的,所以读取效率相对较高,那么我们有没有解决办法呢?

sure,我们此时就可以通过取消同步流操作来加快读写速度!

看代码:

cpp 复制代码
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

如果你在实际需要中读写量非常大的情况下,可以通过取消同步流来进行优化,特别是在比赛用途上!

3.scanf知识扩展

如果我们想通过scanf读取一行数据,并且该数据是有空格存在的,该如何操作呢?

例如:读取:hello world!

此时我们就来补充下新的知识,我们可以用下面代码所示方法进行读取:

cpp 复制代码
char s[20];
scanf("%[^\n]", s);//[^\n]表示非换行继续读取
cpp 复制代码
int main()
{
	char s[20];
	scanf("%[^\n]", s);
	printf("%s", s);
	return 0;
}

结果:

当然,实际运用并不多,而且还麻烦,我们C++还是以cin读取为主,大家可以适当了解,知道有这个东西就行。

4.理解缓冲区概念

大家如果学习一段时间的编程语言,那么或多或少都会听过缓冲区这个术语,那么什么是缓冲区呢?

定义:缓冲区是指一个存储区域,可以保存数据,以便减少刷屏次数。

规则:读cin会刷新一次cout,在·程序非正常终止也会刷新cout。

作用:使我们可以看到程序运行结果不是一个个符号依次出现再刷屏得到下一个符号,使得一部分符号同时出现,大大方便了观察和实际运用。大家如果对这方面有兴趣,可以自行去深入学习,这里只是简单介绍。

最后,寒假冲啊!!!

相关推荐
加号34 小时前
【Qt】 应用程序发布:依赖库拷贝与部署指南
开发语言·qt
('-')5 小时前
八股复习2:Java Array list和Linked list
java·开发语言
小黄人软件5 小时前
C++读写编辑CSV文件示例源码 用于数据导入导出,比Excel好使
开发语言·c++·excel
郭涤生5 小时前
C++各个版本的性能和安全性总结
开发语言·c++
wljy16 小时前
二、静态库的制作和使用
linux·c语言·开发语言·c++
道剑剑非道6 小时前
FFmpeg 6.0 实战:用 C++ 封装摄像头采集与 RTSP 推流
开发语言·c++·ffmpeg
天天进步20157 小时前
Python全栈项目实战:基于深度学习的语音合成(TTS)系统
开发语言·python·深度学习
OctShop大型商城源码7 小时前
.NET线上商城源码_C#商城源码_技术赋能下的电商新生态
开发语言·c#·.net·商城系统源码
光电笑映7 小时前
从环境变量到进程虚拟地址空间——Linux 内存管理的底层脉络
linux·服务器·c++·c
IT猿手7 小时前
光伏模型参数估计:基于山羊优化算法(GOA )的光伏模型参数辨识问题求解研究,免费提供完整MATLAB代码链接
开发语言·算法·matlab·群智能优化算法·智能优化算法·光伏模型参数估计·光伏模型参数辨识