STL string 输入输出重载1

这篇博客展示了C++中使用string类进行字符串操作的方法,包括初始化、截取和输出。同时,还介绍了如何自定义一个类似于string的类,并实现基本的字符串功能。通过示例代码,读者可以理解并掌握C++中处理字符串的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、string函数使用(string.app)

#include<iostream>
#include<cstring>//string类。
using namespace std;//标准命名空间。
int main()
{  //创建对象
    string str;//NULL *m_str=NULL  ,  new char('\n')
    //const char* str1 = str.c_str();// new char('\n')
    string str1(5, 'a');
    cout << str1.c_str()<<endl;//size_type
    
    string str2("abnc");
    cout << str2.c_str() << endl;

    string str3("abcdfeh", 3);
    cout << str3.c_str() << endl;

    string str4(str3,1,3);//[1,3)下标
    cout << str4.c_str() << endl;

    string str5(str3);//[1,3)下标
    cout << str5.c_str() << endl;
    
    cout << str2 << endl;

    string str6("abcd");
    cout << str6<<endl;
    cin >> str6;
    cout << str6 << endl;
    system("pause");
    return 0;

}

2、自定义string函数(主函数Stringmain.app)

#include "Mystring.h"
int main()
{
    stu str;
    stu str1(5,'b');
    cout << str1.c_str()<<endl;
    
    stu str2("abcdef");
    cout << str2.c_str() << endl;
    
    stu str3("abcdefghehasdj",7);
    cout << str3.c_str() << endl;
    
    stu str4(str3,2,5);//[2,5)
    cout << str4.c_str() << endl;
    
    stu str5(str3);
    cout << str5.c_str() << endl;

    cout << str2<<endl;

    stu str6("abd");
    cout << str6 << endl;
    cin >> str6 ;
    cout << str6 << endl;
    system("pause");
    return 0;

}//(头文件与函数实现在string输入输出重载2中)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clown_30

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值