STL string 输入输出重载1

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中)


版权声明:本文为qq_52119661原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。