STL-i/O流和string类

实验目的:

本实验主要练习字符串I/O流和字符串string类的使用方法。

实验器材:

Code blocks

实验内容:

  1. 练习课本第4章的例4.11及之后的例子,练习字符串I/O流的使用方法。用从文本文件中读取学生各科成绩的例子对第4章的所有类型输入输出流进行综合练习。
  2. 练习课本第5章的所有例子,练习字符串类string的创建、插入、替换、查询、删除等操作的使用方法。实现自己定制的MyString类,除支持string类的所有操作外
  1. .支持trim( )功能,可以删除字符串两端的所有空格
  2. .支持split(string separator, int N=-1)功能,可以按照原字符串中的子字符串separator将原字符串切分成多个字符串,最多切分N次, N默认值为-1, 即不限制切分次数。例如, s=”hello, ann, how are you doing?”,s.split(“,”)将s切分成3个子字符串”hello”,”ann”,”how are you doing”,s.split(“,”, 1)将s切分成2个子字符串”hello”,”ann, how are you doing”。

实验步骤:

测试一 :

#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;

}

实验结果(附数据和图表):

一.

二.

三.

四.

实验结果分析及结论:

  1. 创造s4对象,构造函数有三个参数,第一个是string类,第二,三的参数  类型为整型,分别表示偏移量及计数量
  2. insert():第一个参数表明插入串源的位置,第二个表明要插入的字符串
  3. append():仅输入一个参数,就可以在串尾追加该字符串
  4. +:字符串的连接
  5. size():包括空格的数量
  6. replace():第一个参数用于指示从什么位置开始改写,第二个用于从原字符串中删除几个字符,第三个是替换字符串的值
  7. find() 在一个字符串中查找指定的单个字符或字符组,如果找到,就返回第一个匹配字符的位置;一般有2个参数,一个是代查询的字符串,一个是查询的起始位置。
  8. find_first_of() 在一个字符串中进行查找,返回值是第一个与指定字符串中任何字符匹配的字符位置

实验心得体会和建议:

通过本次实验,我对 C++的输入输出流有了更深入的理解,了解了不同类型输入输出流的应用场景,为以后编程实践打下了基础。

实验使我认识到,编程不仅需要掌握理论知识,还需要动手实践,只有不断积累经验,才能提高编程水平。

加强理论知识的学习,让学生了解各种输入输出流的原理和适用场景,为实际编程提供指导。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值