C++中的函数库

和C语言一样,在C++中也有各种各样的函数库,例如在C++程序中的第一行一般都会写成

 #include <iostream>

这便是包含C++中的标准库函数,在C++中,还提供了有关字符串操作的库函数,string,例如:

#include <iostream> //默认库函数
#include <string>  //包含有关字符串操作的库函数
using namespace std;

int main(){
	string s1 = "I am a C++ string."; //定义一个字符串s1,并赋值
	cout << s1 << endl;
	string s2 = "everybody with me can work more comfortable "; //再定义一个字符串s2,并赋值
	s2 += "than with C Language.";  //与C语言不同的是,字符串结合可直接用'+'而无需使用函数strcpy
	cout << s2 << endl;
}

输出结果:

也可以从键盘中输入字符串,如下面程序:

#include <iostream>
#include <string>
using namespace std;

int main(){
	string s;
	cout << "I am a empty string. " << s << endl; //若,s定义但未赋值,则为空字符串
	cout << "Please handle me: ";
	cin >> s; //输入字符串,回车结束
	cout << "now I am this string: " << s << endl;
}

输出结果:

但是,输入字符串时,不得包含空格键,否则空格之后的字符串将不会被赋值给该字符串变量。例如以上函数若是输入Hello, World!则输出结果如下:

如果需要读取整行则需要用到函数getline(cin, s);例如将上面的函数cin >> s 改为 getline(cin, s);
则输出结果:

C++中还提供了将一个数改为字符串的函数to_string,例如以下程序:

#include <iostream>
#include <string>
using namespace std;

int main(){
	int i = 1;
	float f = 2.3;
	string s = to_string(i);
	cout << s << endl;
	s = "i = " + to_string(i) + ", f = " + to_string(f);
	cout << s;
}

输出结果:

字符串流sstream,在C++中字符串可以按照字符串流输出,例如:

#include <iostream>
#include <string>
#include <sstream> //包含字符串流的类
using namepsace std;

int main(){
	int i = 1;
	float f = 2.3;
	string s = " i = " + to_string(i) + ", f = " + to_string(f);
	cout << s; //直接输出字符串s
	stringstream ss; //定义字符串流对象ss
	ss << endl << "i = " << i ", f = " << f;  //给对象ss赋值,里面包含字符和数。
	s = ss.str(); //ss.str()是返回该对象的具体内容,并赋给字符串s
	cout << s; 
}

输出结果:

C++中还提供了文件的读与写的类fstream。例如将数据流写入文本中:

#include <fstream>  //声明包含读写操作的类
#include <iostream>
#include <string>

int main(){
	int i = 1;
	int f = 2.3;
	string DataName = "Data01.txt";
	ofstream ofs; //创建一个写入数据的对象相当于C语言中的FILE
	ofs.open(DataName); /创建一个文件,名字为Data01.txt,若原来存在改名字文件,则删除原有的,创建新的
	ofs << "this is " << DataName << endl << "i = " << i << ", f = " << f;  //往文本中写入内容
	ofs.close(); //关闭文档
}

产生的可执行文件(.exe文件)中无任何东西,但是在该程序的子目录中会产生一个.txt文件,里面的内容为上面写入的内容,文件名为Data01.

同样地,如果将该程序中名字的后缀改为.doc,则会在该程序的子目录中产生一个word文档。

#include <fstream>  //声明包含读写操作的类
#include <iostream>
#include <string>
using namespace std;

int main(){
	int i = 1;
	int f = 2.3;
	string DataName = "Data01.doc";  //改为.doc后缀
	ofstream ofs; //创建一个写入数据的对象相当于C语言中的FILE
	ofs.open(DataName); //创建一个文件,名字为Data01.txt,若原来存在改名字文件,则删除原有的,创建新的
	ofs << "this is " << DataName << endl << "i = " << i << ", f = " << f;  //往文本中写入内容
	ofs.close(); //关闭文档
}


读取文件则需要用到类ifstream,例如读取刚刚创建的Data01.txt文本文档中的内容:

#include <fstream>  //声明包含读写操作的类
#include <iostream>
#include <string>
using namespace std;

int main(){
	string DataName = "Data01.txt";
	string row;
	ifstream ifs; //创建一个读取数据的对象相当于C语言中的FILE
	ifs.open(DataName); //打开该文件
	if(!ifs){
        cerr << DataName << "can not open!" << endl;
	}  //判断该文件是否存在,若不存在则报错,cerr是默认输出流出错时的输出
	while(getline(ifs,row)){//从刚刚的读取数据流中得到一行数据给row
        cout << row << endl;  //输出该数据
	}
	ifs.close(); //关闭文档
}

输出结果:

如果要打开的文档不存在,则报错:

如有错误,欢迎大家批评与指正!

猜你喜欢

转载自blog.csdn.net/WJ_SHI/article/details/82023736