C++标准库概述
一、C++标准库的主要组件:
1、标准C库
2、I/O流技术(对标准输入输出设备称为标准I/O,对在外磁盘上文件的输入输出称为文件I/O,对内存中指定的字符串存储空间的输入输出称为串I/O)
3、string类模版
4、容器(vector、list、queue、stack、deque、map、set和bitset)
5、算法
6、对国际化的支持
7、对数字处理的支持
8、诊断支持(3中报错方式:C的断言、错误号、例外)
二、I/O流技术
C++为实现数据的输入输出定义了一个庞大的类库,它包括的类主要有ios、istream、ostream、iostream、ifstream、ofstream、fstream、istrstream、ostrstream、strstream等,其中ios为根基类,其余的都是它的直接或者间接派生类。
ios直接派生四个类:输入流iostream、输出流ostream、文件流基类fstream和字符串基类strstream.
通过上面的介绍很容易理解C++中的I/O流库都包含在iostream、fstream、strstream这三个类库文件中。
C++不仅仅提供了上面的三个类库,还为用户提供了提供了标准I/O操作中的类对象,分别是cin、cout、cerr、clog
格式控制操作符:
#include <iostream> //其实iomanip中包含iostream,所以该行可省略
#include <iomanip>
using namespace std; int main(){
int x = 30, y = 300, z = 1024;
cout << x << ' ' << y << ' ' << z << endl;
//八进制输出
cout << oct << x << ' ' << y << ' ' << z << endl;
//十六进制输出
cout << hex << x << ' ' << y << ' ' << z << endl;
//设置提示符和字母大写输出
cout << setiosflags(ios::showbase | ios::uppercase);
cout << x << ' ' << y << ' ' << z << endl;
cout << resetiosflags(ios::showbase | ios::uppercase);
cout << x << ' ' << y << ' ' << z << endl;
//按十进制输出
cout << dec << x << ' ' << y << ' ' << z << endl; return 0;
}
自定义流操作符:
#include <iostream>
using namespace std; ostream &lin(ostream &myos){
return myos << "\n-----------------";
} int main(){
cout << lin << lin << lin << endl;
return 0;
}
I/O操作符重载:
#include <iostream>
#include <string.h>
using namespace std; class Student{
friend ostream& operator << (ostream& ot, Student& popup);
char name[10];
unsigned int age;
unsigned long num;
public:
Student(char *na, unsigned int al, unsigned long number):age(al),
num(number){
strcpy(name, na);
}
}; ostream& operator << (ostream& ot, Student& popup){
ot << "Name:" << popup.name << endl << "Age:" << popup.age << endl
<< "Number:" << popup.num << endl << "---------------------" << endl;
return ot;
} int main(){
Student a("Wang", 18, 1234), b("zhao", 19, 4312), c("liu", 20, 2341);
cout << a << b << c; return 0;
}
写入文件:
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std; int main(void){
//定义输出文件流,并打开相应的文件
ofstream f1("a:wr1.dat");
if(!f1){
cerr << "a:wr1.data file not open!" << endl;
}
for(int i=0; i<21 ; i++){
f1 << i << ' ';
}
f1.close();
return 0;
}
读文件内容:
#include <iostream>
#include <std.ib.h>
#include <fstream> int main(){
//规定打开的文件时输入文件,若文件不存在则返回打开失败信息
ifstream f1("wrl.dat", ios::in | ios::nocreate);
//当f1打开失败时进行错误处理
if(!f1){
cerr << "wr1.data file not open!" << endl;
exit(1);
}
int x;
while(f1 >> x)
cout << x << ' ';
cout << endl;
f1.close(); return 0;
}
输入输出流操作:
#include <iostream>
#include <strstream>
using namespace std; int main(){
char a[50];
char b[50];
istrstream sin(a); //定义一个输入字符串流sin,使用的字符数组为a
//定义一个输出字符串流sout,使用的字符数组为b
ostrstream sout(b, sizeof(b));
//从键盘上输入字符
cin.getline(a, sizeof(a));
char ch = ' ';
int x;
while(ch !='@'){
//使用'@'字符作为字符串流结束标志
if(ch >= 48 && ch <= 57){
//将字符压入流中
sin.putback(ch);
sin >> x;
//存入输出流
sout << x << ' ';
}
//每次取出一个字符
sin.get(ch);
}
sout << '@' << ends;
//输出输出流的内容
cout << b;
cout << endl; return 0;
}
构造字符串:
#include <string>
#include <iostream>
using namespace std; int main(){
string Mystring1(10, ' ');
string Mystring2 = "This is a string";
string Mystring3(Mystring2);
cout << "string1 is : " << Mystring1 << endl;
cout << "string2 is : " << Mystring2 << endl;
cout << "stirng3 is : " << Mystring3 << endl; return 0;
}
字符串判断函数:
1、empty()
2、length()
3、resize()改变长度
#include <iostream>
#include <string>
using namespace std; int main(){
string TestString = "ll11223344565666";
cout << TestString << "\n size: " << TestString.length() << endl;
TestString.resize(5);
cout << TestString << "\n size: " << TestString.size() << endl;
TestString.resize(10);
cout << TestString << "\n size: " << TestString.size() << endl;
TestString.resize(15, '6');
cout << TestString << "\n size: " << TestString.size() << endl; return 0;
}
4、append()
5、c_str()
#include <string>
#include <iostream>
using namespace std; int main(){ string str1("012");
string str2("345");
cout << "str1 = " << str1.c_str() << endl;
cout << "str2 = " << str2 << endl;
//把字符串str2增加到str1尾部
str1.append(str2);
cout << "str1 = " << str1 << endl;
//返回的是一个常量指针
const char* ch = str1.c_str();
for(int i=0; i<str1.length(); i++){
cout << ch[i] << ' ';
}
cout << endl;
str1.append(str2.c_str(), 2); //把字符串中的前两个元素插入到str1尾部
str1.append(1, 'A');
str1.append(str2.begin(), str2.end());
cout << "str1 = " << str1 << endl;
cout << endl; return 0;
}
字符和字符串连接
#include <string>
#include <iostream>
using namespace std; int main(){
string result;
string S1 = "ABC";
string S2 = "DEF";
char CP1[] = "GHI";
char C = 'J';
cout << "S1 is " << S1 << endl;
cout << "S2 is " << S2 << endl;
cout << "C is " << C << endl;
result = CP1 + S1;
cout << "CP1 + S1 is " << result << endl;
result = S1 + C;
cout << "S1 + C is " << result << endl;
result = S1 + S2;
cout << "S1 + S2 is " << result << endl;
result = CP1 + C + S1;
cout << "CP1 + C + S1 is " << result << endl;
result = S1 + CP1 + C;
cout << "S1 + CP1 + C is " << result << endl; return 0;
}
字符串迭代:
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; int main(){
const string hello("Hello, how are you?");
string s(hello.begin(), hello.end());
cout << "s : " << s << endl;
string::iterator pos;
for(pos = s.begin(); pos != s.end(); ++pos){
cout << *pos << ' ';
}
cout << endl;
//字符串翻转
reverse(s.begin(), s.end());
cout << "reverse: " << s << endl;
//去除重复元素
s.erase(unique(s.begin(), s.end()), s.end());
cout << "no duplictes: " << s << endl;
}
C++标准库概述的更多相关文章
- 【C】 06 - 标准库概述
任何程序都会有一些通用的功能需求,对这些需求的实现组成了库.它可以提高程序的复用性.健壮性和可移植性,这也是模块化设计的体现.C规范定义了一些通用接口库,这里只作概述性介绍,具体细节当然还是要查阅规范 ...
- C++标准库概述 [转]
C++标准库的所有头文件都没有扩展名. C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.<cname>形式的标准头文件[<complex>例外]其内 ...
- Python 3 学习笔记之——标准库概述
1. 操作系统接口 os 模块提供了一些与操作系统相关联的函数. >>> os.getcwd() # 获取当前工作目录 '/home/senius' >>> os. ...
- C 标准库系列之概述
基本上很多编程语言都会提供针对语言本身的一系列的标准库或者包,当然C语言同样也有提供标准库,C语言的标准库是一系列的头文件的集合:如assert.h.ctype.h.errno.h.float.h.l ...
- C语言-12-日期和时间处理标准库详细解析及示例
概述 标准库 提供了用于日期和时间处理的结构和函数 是C++语言日期和时间处理的基础 与时间相关的类型 clock_t,本质是:unsigned long typedef unsigned long ...
- Boost程序库完全开发指南——深入C++“准”标准库(第3版)
内容简介 · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...
- Python:标准库(包含下载地址及书本目录)
下载地址 英文版(文字版) 官方文档 The Python Standard Library <Python标准库>一书的目录 <python标准库> 译者序 序 前言 第1章 ...
- 一起学习Boost标准库--Boost.StringAlgorithms库
概述 在未使用Boost库时,使用STL的std::string处理一些字符串时,总是不顺手,特别是当用了C#/Python等语言后trim/split总要封装一个方法来处理.如果没有形成自己的com ...
- [技术] OIer的C++标准库 : STL入门
注: 本文主要摘取STL在OI中的常用技巧应用, 所以可能会重点说明容器部分和算法部分, 且不会讨论所有支持的函数/操作并主要讨论 C++11 前支持的特性. 如果需要详细完整的介绍请自行查阅标准文档 ...
随机推荐
- 洛谷P3195 [HNOI2008]玩具装箱TOY(单调队列优化DP)
题目描述 P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1...N的N件玩具, ...
- C#中Dictionary排序方式
转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...
- JS文字特效:彩色滚动变幻效果,只适合少量的文字。(过多对页面有影响)
JS代码如下: 代码具体是在哪里的我不知道但是我的有道云上有.如有哪位朋友知道,还望联系下,添加出处. <div id="chakhsu"></div> & ...
- [NOIP1999]进制位(搜索)
P1013 进制位 题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E ...
- WHU 1540 Fibonacci 递推
武大邀请赛的网络预选赛,就去做了个签到题,居然连这个递推都没推出来,真是惭愧. 而且好久没写矩阵乘法了,来回顾一下. 题意: 求Fibonacci数列的,前n项立方和. 思路: 可以求得一下递推公式: ...
- ARM官方《CMSIS-RTOS教程》之线程Threads
创建线程Creating Threads 一旦RTOS开始运行,就会有很多系统调用来管理和控制活跃的线程.默认情况下,main()函数自动被创建为第一个可运行的线程.在第一个例子里我们使用main() ...
- 【Henu ACM Round#20 F】 Arthur and Brackets
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 所给的li,ri是左括号从左到右的顺序给的. (且注意长度是2*n 现在我们先把第一个左括号放在第1个位置. 然后考虑第二个位置. ...
- Java实现断点续传。
http://www.cnblogs.com/liaojie970/p/5013790.html
- 什么是 "署名-非商业性使用-同样方式共享"
什么是 "署名-非商业性使用-同样方式共享" 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致& ...
- 灵活的运用Model类
1.定义接口 using System; using System.Collections.Generic; using System.Linq; using System.Web; namespac ...