实验目的:
本实验主要练习字符串I/O流和字符串string类的使用方法。
实验器材:
Code blocks
实验内容:
实验步骤:
测试一 :
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main(){
????????????????fstream in;
????????????????in.open("E:\\STL\\a.txt",ios::in);
????????????????in.write("Hello",5);
????????????????in.seekg(5,ios::beg);
????????????????fstream out;
????????????????out.open("E:\\STL\\a.txt",ios::app);
????????????????out<<endl;
????????????????out<<22<<endl;
????????????????cout<<in.rdbuf();
????????????????in.close(); ??????????????
?return 0;
}
测试二 :反解字符串给各变量赋值
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
int main(){
int n;
???????????????float f;
???????????????string strHello;
???????????????string strText="1 3.14 Hello";
???????????????istringstream s(strText);//输入流
???????????????s>>n;
???????????????s>>f;
???????????????s>>strHello;
???????????????cout<<"n="<<n<<endl;
???????????????cout<<"f="<<f<<endl;
???????????????cout<<"strHello="<<strHello<<endl;
return 0;
}
测试三 :字符串创建,插入,替换,查询,删除和比较的操作
#include<string>
#include<iostream>
using namespace std;
int main(int argc,char*argv[]){
????????????????cout<<"基本创建方式:"<<endl;
????????????????string s1;
????????????????string s2("How are you?");
????????????????string s3(s2);
????????????????string s4(s2,0,1);
????????????????string s7(s2,0,2);
????????????????string s8(s2,1,4);//第一个是起始位置,第二个是输出的个数
????????????????string s5="Fine";
????????????????string s6="Fine"+s2;
????????????????string s9=s2+"Fine";
????????????????cout<<s4<<'\t'<<s9<<'\t'<<s6<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"迭代器创建方式:"<<endl;
????????????????string ss1="How are you?";
????????????????string ss2(ss1.begin(),ss1.end());
????????????????string ss3(ss1.begin(),ss1.begin()+6);
????????????????cout<<ss2<<'\t'<<ss3<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"插入:"<<endl;
????????????????string s="do";
????????????????cout<<"inition size is:"<<s.size()<<endl;
????????????????s.insert(0,"How");
????????????????s.append("you");
????????????????s=s+"do?";
????????????????cout<<"final size is:"<<s.size()<<endl;
????????????????cout<<s<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"替换操作:"<<endl;
????????????????string v="What's you name?";
????????????????cout<<"替换前"<<v<<v.size()<<endl;
????????????????v.replace(7,4,"her ");
????????????????cout<<"替换后"<<v<<v.size()<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"查找:"<<endl;
????????????????string v1="What's your name? My name is TOM. How do you do? Fine,thanks.";
????????????????int n=v1.find("your");
????????????????cout<<"the first your pos:"<<n<<endl;
????????????????n=v1.find("you",15);
????????????????cout<<"the first your pos begin from 15:"<<n<<endl;
????????????????n=v1.find_first_of("abcde");
????????????????cout<<"find pos when character within abcde:"<<n<<endl;
????????????????n=v1.find_first_of("abcde",3);
????????????????cout<<"find pos begin from 2 when character within abcde:"<<n<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"删除:"<<endl;
????????????????string v2="How are you?";
????????????????v2.erase(v2.begin(),v2.begin()+3);
????????????????cout<<"erase 1:"<<v2<<endl;
????????????????string v3="Fine,thanks";
????????????????v3.erase(v3.begin(),v3.end());
????????????????cout<<"erase 2:"<<v3<<endl;
????????????????cout<<"************************"<<endl;
????????????????cout<<"比较:"<<endl;
????????????????string v4="this";
????????????????string v5="that";
????????????????if(v4>v5)cout<<"v4>v5"<<endl;
????????????????if(v4<v5)cout<<"v4<v5"<<endl;
????????????????if(v4==v5)cout<<"v4=v5"<<endl;
return 0;
}
测试四.实现自己定制的MyString类
#include <vector>
#include <string> ?
#include<iostream>
using namespace std;
class MyString {
private:
????string str;
public:
????MyString(const std::string& s = "") {
????????str = s;
????}
????// trim函数删除字符串两端的所有空格 ?
????MyString& trim() {
????????str.erase(0, str.find_first_not_of(" "));
????????str.erase(str.find_last_not_of(" ") + 1);
????????return *this;
????}
????// split函数按照指定的分隔符将字符串切分成多个子字符串,最多切分N次 ?
????vector<string> split(const string& separator, int N = -1) {
????????vector<string> result;
????????size_t pos = 0;
????????size_t next = str.find(separator, pos);
????????while (next != string::npos && (N == -1 || result.size() < N)) {
????????????result.push_back(str.substr(pos, next - pos));
????????????pos = next + separator.size();
????????????next = str.find(separator, pos);
????????}
????????result.push_back(str.substr(pos)); ?// 处理最后一个子字符串 ?
????????return result;
????}
????// 返回字符串本身 ?
????const string& getString() const {
????????return str;
????}
};
int main() {
????MyString s(" ??????hello, ann, how are you doing? ?????????");
????s.trim(); ?// 删除两端的空格 ?
????cout << s.getString() << endl; ?// 输出:"hello, ann, how are you doing?" ?
????vector<string> parts = s.split(", ");// 按逗号分隔字符串,不限制切分次数 ?
????for (const auto& part : parts) {
???????cout << part << endl; ?// 输出每个子字符串 ?
????}
????return 0;
}
实验结果(附数据和图表):
一.
二.
三.
四.
实验结果分析及结论:
实验心得体会和建议:
通过本次实验,我对 C++的输入输出流有了更深入的理解,了解了不同类型输入输出流的应用场景,为以后编程实践打下了基础。
实验使我认识到,编程不仅需要掌握理论知识,还需要动手实践,只有不断积累经验,才能提高编程水平。
加强理论知识的学习,让学生了解各种输入输出流的原理和适用场景,为实际编程提供指导。