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。

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

最后,寒假冲啊!!!

相关推荐
Keven_1142 分钟前
算法札记:ACM适用的很骚的C++语法(持续更新)
开发语言·c++
mengzhi啊1 小时前
QPluginLoader 动态 DLL 插件版,支持插件之间通信,广播。请求和应答
c++·qt
脉动数据行情11 小时前
C# 实现德指 DAX 期货 WebSocket 实时行情监听
开发语言·websocket·c#
大圣编蚕2 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法
菜鸟~noob2332 小时前
【电子战】第02篇:能量检测器 vs. 相关检测器【含matlab代码】
开发语言·matlab
OpenAnolis小助手3 小时前
一句话看透 JVM,SysOM 诊断 Skill 新增 Java 应用诊断能力
java·开发语言·jvm·阿里云·操作系统控制台·sysom
小玮看世界3 小时前
[Python]OD算法在OD实际运用转化参考清单
开发语言·python·算法
ForteScarlet3 小时前
Kotlin 2.4.20 现已发布,新特性多不多?
android·java·开发语言·开源·kotlin·jetbrains
萧瑟余晖3 小时前
Java深入解析篇六十二之安全机制详解
java·开发语言
老赵的博客4 小时前
c++ QT之动态库加载问题
c++·qt