STL封装之string类 超全整理

从8个方面来了解string类 :
1.输入输出
2.有关容量的操作
3.关系运算符
4.比较
5.字符串的加法(方法一,方法二)
6.取子串
7.对容器的操作
8.交换字符串的值

1. 输入输出
输入:cin,getline
输出:cout
cin与getline的区别:cin输入遇到空格、tab、回车会停止而getline 只有遇到回车才会停止
cin适合单个单词输入输出,getline适合多个单词输入输出

    string st;
    cin>>st;
    getline(cin, st);
    cout<<st;

2. 有关容量的操作
size/length:求字符串的长度
max_size:求字符串的最大长度
resize:重定义字符串长度
empty:测试字符串是否为空,是则返回true,否则返回false
clear:清除字符串

    string s;
    s = "string";
    cout<<"The length of s is:"<<s.size()<<endl;//The length of s is:6
    cout<<"The length of s is:"<<s.length()<<endl;//The length of s is:6
    cout<<"The max size of s is:"<<s.max_size()<<endl;//The max size of s is:2147483647
    s.resize(5);
    cout<<"The string s is:"<<s<<endl;//The string s is:strin
    cout<<s.empty()<<endl;//0
    s.clear();
    cout<<s.empty()<<endl;//1

3.关系运算符
根据字典序进行比较

    string foo = "alpha";
    string bar = "beta";
    if (foo==bar)
        cout << "foo and bar are equal\n";
    if (foo!=bar)
        cout << "foo and bar are not equal\n";
    if (foo< bar)
        cout << "foo is less than bar\n";
    if (foo> bar)
        cout << "foo is greater than bar\n";
    if (foo<=bar)
        cout << "foo is less than or equal to bar\n";
    if (foo>=bar)
        cout << "foo is greater than or equal to bar\n";
    //输出foo and bar are not equal
    //    foo is less than bar
    //    foo is less than or equal to bar

4.比较
相等:返回0
小于:返回小于0的值
大于:返回大于0的值

    string str4 ("green apple");
    string str5 ("red apple");
    if (str4.compare(str5) != 0)
        cout << str4 << " is not " << str5 << '\n';//green apple is not red apple
    if (str4.compare(6,5,"apple") == 0)
        cout << "still, " << str4 << " is an apple\n";//still, green apple is an apple
    if (str5.compare(str5.size() - 5,5,"apple") == 0)
        cout << "and " << str5 << " is also an apple\n";//and red apple is also an apple
    if (str4.compare(6,5,str5,4,5) == 0)
        cout << "therefore, both are apples\n";//therefore, both are apples

5.字符串的加法(方法一,方法二)
方法一:+=

    string name("Jack");
    string family("Lucy");
    name += " love ";
    name += family;
    name += '\n';
    cout<<name<<endl;

方法二:append
用法1:直接加上一个字符串str2,如str.append(str);
用法2:加上一个从a开始长度为b的字符子串,如str.append(str, a, b);
用法3:加上一个长度为len的字符子串(默认初始地址为s.at(0)),如str.append(“dots are cool”, 5);
用法4:输出n个c字符 如:str.append(10u,’.’);
用法5:取字符子串:地址从a到b,如:str.append(str3.begin()+8,str3.end());

    string str;
    string str2="Writing ";
    string str3="print 10 and then 5 more";
    str.append(str2);                       // "Writing "
    str.append(str3,6,3);                   // "10 "
    str.append("dots are cool",5);          // "dots "
    str.append("here: ");                   // "here: "
    str.append(10u,'.');                    // ".........."
    str.append(str3.begin()+8,str3.end());  // " and then 5 more"
    cout << str << '\n';

6.取子串substr

    string str="We think in generalities, but we live in details.";
    // (quoting Alfred N. Whitehead)
    string str2 = str.substr (3,5);     // "think"
    size_t pos = str.find("live");      // position of "live" in str
    string str3 = str.substr (pos);     // get from "live" to the end
    cout << str2 << ' ' << str3 << '\n';
    //输出think live in details.

7.对容器的操作
begin:容器的首个地址
end:容器的最后一个元素的下一位地址

    s = "string";
    for(string::iterator it = s.begin(); it != s.end(); it++)
        cout<<*it;
    cout<<""<<endl;//string
    cout<<"The begin of string s is:"<<s.at(0)<<endl;//The begin of string s is:s
    cout<<"The begin of string s is:"<<s[0]<<endl;//The begin of string s is:s
    cout<<"The end of string s is:"<<s.at(s.size() - 1)<<endl;//The end of string s is:g
    cout<<"The begin of string s is:"<<s[s.size() - 1]<<endl;//The begin of string s is:g

8.交换字符串的值

    string buys("moneys");
    string sells("goods");
    cout<<"Before the swap, buys has "<<buys<<" and sells has "<<sells<<endl;//Before the swap, buys has moneys and sells has goods
    swap(buys, sells);
    cout<<"After the swap, buys has "<<buys<<" and sells has "<<sells<<endl;//After the swap, buys has goods and sells has moneys

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值