string in C++98 的用法总结

写在前面
string对象是表示字符串的对象,支持单字符操作,还有一些很好的函数,方便了编程。为了很好地使用这个类,这里做一下用法总结。
string类是basic_string类模板的一个实例,它的字符类型是char。string对象支持单 字节字符操作,不支持多字符编码或变长编码字符的处理。
谈到头文件的问题,string.h和cstring.h头文件支持对C-风格字符串进行操作的C库字符串函数。string类由头文件<string>支持。
用法总结
(1)构造函数
      default (1)	string();
         copy (2)	string (const string& str);
    substring (3)	string (const string& str, size_t pos, size_t len = npos);
from c-string (4)	string (const char* s);
from sequence (5)	string (const char* s, size_t n);
        fill (6)	string (size_t n, char c);
       range (7)	template <class InputIterator> string  (InputIterator first, InputIterator last);
    string str1 = "test compare fun!";
    string str2 = "test d";
    char chtest[] = "string test compare fun!";
    char chtest1[10] = "test";
    string strtest = "Telephone home.";

    vector<char> input(6);
    input[0] = 's',input[1] = 't',input[2] = 'r',input[3] = 'i';

    cout << "str1:" << str1 << endl << endl;

    cout << "str2:" <<str2 << endl << endl;

    cout << "chtest:" << chtest << endl << endl;

    cout<< "chtest1:" <<chtest1 << endl << endl;

    cout<< "strtest:" <<strtest << endl << endl;

    //string ctor function test

    string str3(strtest);
    cout << "str3(strtest) :"<< str3 << endl << endl;

    string str4(strtest,4);
    cout << "str4(strtest,4) :"<< str4 << endl << endl;

    string str5(strtest,4,5);
    cout << "str5(strtest,4,5) :"<< str5 << endl << endl;

    string str6(6,'c');
    cout << "str6(6,'c'):"<< str6 << endl << endl;

    string str7(chtest+7,chtest+12);
    cout << "str7(chtest+7,chtest+12):"<< str7 << endl << endl;


    string str8(input.begin(),input.end());
    cout << "str8(input.begin(),input.end()):"<< str8 << endl << endl;
(2)比较函数
string (1)	int compare (const string& str) const;
substrings (2)	int compare (size_t pos, size_t len, const string& str) const;
                int compare (size_t pos, size_t len, const string& str,size_t subpos, size_t sublen) const;
c-string (3)	int compare (const char* s) const;
                int compare (size_t pos, size_t len, const char* s) const;
buffer (4)	int compare (size_t pos, size_t len, const char* s, size_t n) const;

    //string compare function test
    //1 iAns should be -1
    int iAns = str1.compare(str2);
    cout << "1 str1.compare(str2):" << iAns << endl << endl;

    //2 iAns should be -1
    iAns = str1.compare(0,5,str2);
    cout << "2 str1.compare(0,5,str2):" << iAns << endl << endl;

    //3 iAns should be 0
    iAns = str1.compare(0,5,str2,0,5);
    cout << "3 str1.compare(0,5,str2,0,5):" << iAns << endl << endl;

    //4 iAns should be 1
    iAns = str1.compare(chtest);
    cout << "4 str1.compare(chtest):" << iAns << endl << endl;

    //5 iAns should be 0
    iAns = str1.compare(0,4,chtest1);
    cout << "5 str1.compare(0,4,chtest1):" << iAns << endl << endl;
(3)运行结果(codeblock + GUC)
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值