C++标准库string类型

//标准库的string类型

#include <string>
#include <iostream>

using namespace std;
//或者可以这样
//using std::string;
//using std::cin;
//using std::cout;

int main()
{
    //1.string的初始化有4种方式
    //string s1;
    //string s2(s1);
    //string s3("value");
    //string s4(5, 'c');
    //cout<<"s1="<<s1<<endl;
    //cout<<"s2="<<s2<<endl;
    //cout<<"s3="<<s3<<endl;
    //cout<<"s4="<<s4<<endl;

    //2.string对象的读写
    //string s;
    //cin >> s;   //这里有两个注意点:1.读取并忽略开头所有的空白字符(如空格,换行符,制表符)2.读取字符,直到再次遇到空白符,读取终止
    //cout<<s<<endl;

    //string s1, s2;
    //cin>>s1>>s2;
    //cout<<"s1="<<s1<<endl;
    //cout<<"s2="<<s2<<endl;

    //3.读入未知数目的string
    //string word;
    //while (cin >> word) //直到读到文件尾,跳出循环
    //{
    //    cout<<word<<endl;   
    //}

    //4.用getline读取整行文本
    //getline有两个参数,一个输入流对象和一个string对象
    //getline 函数从输入流的下一行读取,并保存读取的内容到string中,但不包括换行符。getline函数返回时会丢弃换行符
    //和输入操作符不一样的是,getline 并不忽略行开头的换行符。只要 getline 遇到换行符,即便它是输入的第一个字符,getline 也将停止读入并返回。
    //如果第一个字符就是换行符,则 string 参数将被置为空 string。
    //string line;
    //while (getline(cin, line))
    //{
    //    cout<<line<<endl;
    //}

    //5.string的size和empty操作
    //string st("hello tom\n");
    //cout<<"the size of st="<<st.size()<<endl;//should be 10, including \n
    //if (st.empty())
    //{
    //    //...
    //}

    //6.string::size_type类型

    //7.string关系操作符
    //关系操作符 ==, !=, <, <=, >, >=
    //关系操作符比较两个 string 对象时采用了和(大小写敏感的)字典排序相同的策略:

    //8.string对象的赋值
    //理论上存在效率问题,它必须先把 st1 占用的相关内存释放掉,然后再分配给 st2 足够存放 st2 副本的内存空间,
    //最后把 st2 中的所有字符复制到新分配的内存空间。
    //string st1, st2 = "The expense of spirit";
    //st1 = st2; // replace st1 by a copy of st2

    //9.两个string对象相加
    //string s1("hello, ");
    //string s2("world!");
    //string s = s1 + s2;
    //cout<<s<<endl;
    //s1 += s2;
    //cout<<s1<<endl;

    //10.和字符串字面值的连接
    //当进行 string 对象和字符串字面值混合连接操作时,+ 操作符的左右操作数必须至少有一个是 string 类型的:
    //string s1("hello");
    //string s2("world");
    //string s = s1 + " " + s2 + "!";
    //cout<<s<<endl;

    //string s1 = "hello"; // no punctuation
    //string s2 = "world";
    //string s3 = s1 + ", "; // ok: adding a string and a literal
    //string s4 = "hello" + ", "; // error: no string operand
    //string s5 = s1 + ", " + "world"; // ok: each + has string operand, s1 + "," returns a string operand
    //string s6 = "hello" + ", " + s2; // error: can't add string literals

    //11.从string对象获取字符
    //string str("some string");
    //for (string::size_type index = 0; index < str.size(); index++)
    //{
    //    cout<<str[index];
    //}

    //12.下标操作可用作左值
    //string str("some string");
    //for (string::size_type index = 0; index < str.size(); index++)
    //{
    //    str[index] = '*';
    //}
    //for (string::size_type index = 0; index < str.size(); index++)
    //{
    //    cout<<str[index];
    //}

    //13.计算下标值
    //标准库不要求检查索引值,所用索引的下标越界是没有定义的,这样往往会导致严重的运行时错误。

    //14.string对象中字符的处理
    //cctype头文件中的函数
    //    isalnum(c) 如果 c 是字母或数字,则为 True。
    //    isalpha(c) 如果 c 是字母,则为 true。
    //    iscntrl(c) 如果 c 是控制字符,则为 true
    //    isdigit(c) 如果 c 是数字,则为 true。
    //    isgraph(c) 如果 c 不是空格,但可打印,则为 true。
    //    islower(c) 如果 c 是小写字母,则为 true。
    //    isprint(c) 如果 c 是可打印的字符,则为 true。
    //    ispunct(c) 如果 c 是标点符号,则 true。
    //    isspace(c) 如果 c 是空白字符,则为 true。
    //    isupper(c) 如果 c 是大写字母,则 true。
    //    isxdigit(c) 如果是 c 十六进制数,则为 true。
    //    tolower(c) 如果 c 大写字母,返回其小写字母形式,否则直接返回 c。
    //    toupper(c) 如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。
    //string s("hello world!!!");
    //string::size_type punct_cnt = 0;
    //for (string::size_type index=0; index<s.size(); index++)
    //{
    //    if (ispunct(s[index]))
    //    {
    //        punct_cnt++;
    //    }
    //    s[index] = toupper(s[index]);
    //}
    //cout<<"punct of "<<s<<" is "<<punct_cnt<<endl;

    
//建议:采用 C 标准库头文件的 C++ 版本
//    C++ 标准库除了定义了一些选定于 C++ 的设施外,还包括 C 标准库。
//    C++ 中的头文件 cctype 其实就是利用了 C 标准库函数, 这些库函数就
//    定义在 C 标准库的 ctype.h 头文件中。
//    C 标准库头文件命名形式为 name 而 C++ 版本则命名为 cname ,少了
//    后缀, .h 而在头文件名前加了 c 表示这个头文件源自 C 标准库。因此,
//    cctype 与 ctype.h 文件的内容是一样的,只是采用了更适合 C++程序
//    的形式。特别地, cname 头文件中定义的名字都定义在命名空间 std 内,
//    而 .h 版本中的名字却不是这样。
//    通常,C++ 程序中应采用 cname 这种头文件的版本,而不采
//    用 name.h 版本,这样,标准库中的名字在命名空间 std 中保持一致。
//    使用 .h 版本会给程序员带来负担,因为他们必须记得哪些标准库名字
//    是从 C 继承来的,而哪些是 C++ 所特有的。

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

smart_cat

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

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

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

打赏作者

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

抵扣说明:

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

余额充值