string 的基础用法

定义和初始化string 对象

直接初始化和拷贝初始化

 

  • 直接初始化,利用小括号来完成,推荐使用,可以提高程序的效率
  • 拷贝初始化,利用等号的初始化,以后使用类或者结构体中尽可能写减少这个使用方法
 
//定义和初始化string对象
string test1;    //空串

string test2 = "内容"; //使用=

string test3("内容");   //使用引用字符数组作为参数传给构造函数

string test4(test2); //用一个string初始化另一而string

string test5(test2,pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符

string test6 = test2 + "内容" + test3 //混合初始化

string test7 = test2.substr(pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符

string test8 = test2.substr(); //参数列表为空则会拷贝test2的整个对象(复制test2的简便方法)

string test9(num,ch); //拷贝num个字符型ch到test9

 

string 对象上的操作

 

读写string对象

  • 利用cin>> 来写入字符,是从第一个非空白字符开始,到一下一个空白结束
  • 如果想反复的读取字符的话可以利用while(cin<<),可以一直输入字符
    • 遇到非法输入会停止或者结束符
    • ctrl+d for unix/linux/ etc    
    • ctrl+z for windows/dos
  • 使用getline 来读取一整行
 
string s1;
cin >> s1; //输入第一个非空格,换行符,制表符等字符中,遇到空格就会停止。如" hello world"
cout << s1 << endl; //显示的是 hello

string s2, s3;
cin >> s2 >> s3; //输入"Hello world!"
cout << s2 << s3 << endl;//输出 "Helloworld"
 
string content;
while (cin >> content)    //一直输入,知道遇到文件结束符
{
    cout << content;
}

string::size_type类型

  • size_type是一个无符号类型的值
  • string::size_type从本质上来说,是一个整型数。关键是由于机器的环境,它的长度有可能不同。
  • 添加这种类型是为了几种配套的类型,这样实现了使用的时候就与机器无关
  • 可以通过 auto 或者decltype 来推断string类中的size()类型
  • 在使用的过程中切记不要与带符号的数混用,如与int 类型混用

实例练习:

  1. 查看size_type类型的定义
  2. 练习auto 或者decltype的使用
  3. 练习size_type 与int 混用的后果
 
//字符串的size_type类型
    string s1("abcdefgh");
    string::size_type t = s1.size();
    cout << "t = " << t << endl;

    auto at = s1.size();
    cout << "at = " << at << endl;

    decltype(at) x = 0;

//注意这个是 size_type 是一个无符号的数,如果与负数混合使用会带来如下的错误
    if (t < -1)
    {
        cout << "size is negative" << endl;
    }
    else
    {
        cout << "size is positive" << endl;
    }

比较string对象

  • 可以实现以下符合的比较 == ,!= ,<,<=,>,>= 
  • 区分大小写,规则如下
    • 根据长度不同时,如果对应位置字符相同时,较长的大于较短的
    • 如果对应位置不同时,直接比较第一个不同字符的结果

实例练习:

  1. "Hello", " Hello" "Hello world" "Hiya"
//字符串的比较运算
    string s1("Hello");
    string s2("HelloWorld");
    string s3("Hiya");

    if (s1 > s2)
    {
        cout << "s1:Hello > s2:HelloWorld" << endl;
    }
    else
    {
        cout << "s1:Hello < s2:HelloWorld" << endl;
    }

    if (s1 > s3)
    {
        cout << "s1:Hello > s3:Hiya" << endl;
    }
    else
    {
        cout << "s1:Hello < s3:Hiya" << endl;
    }

 

两个对象string相加运算

  • 两个对象相加,相当于将加号前后的内容追加在一起
  • s1 += s2, 将s2的内容追加到s1上面

字面量与string对象相加

  • 切记需要在等号两侧都需要出现一个string对象
  • 由于string 对象加上一个字面量,就相当于自动将字面量转换成string对象
  • 当字面量加上一个对象的时候,由于字符串字面量在前面,而其并不是标准类型的string 的对象,所以编译器不知道其实际的类型,会导致报错
  •  

实例练习:

  1. string a = " ,"+"hello"+s1等练习
//字符串的相加运算
string s1("abcd");
string s2("efgh");
s1 += s2;
cout << s1 << endl;
cout << s2 << endl;

string s3 = s1 + "ijk";
cout << s3 << endl;

string s4 = "ijk" + s1;
cout << s4 << endl;

string s5 = "abc"+ "lmn" + s4; // abc 和 lmn 都是字面量,而不是字符串,编译器不知道其类型
string s6 = s4 + "abc" + "lmn"; //s4 + "abc" 就会自动变成一个字符串,然后字符串再加上"lmn"

 

//习题3.2 一次输入一整行和一次输入一个词
string s;
while (getline(cin, s))
{
    cout << s << endl;
}

while (cin >> s)
{
    cout << s << endl;
}
 
// 习题3.4
    string s1("abc");
    string s2("abcd");
    if (s1 != s2)
    {
        if (s1.size() > s2.size())
        {
            cout << s1 << endl;
        }
        else
        {
            cout << s2 << endl;
        }
    }

    if (s1.size() != s2.size())
    {
        if (s1.size() > s2.size())
        {
            cout << s1 << endl;
        }
        else
        {
            cout << s2 << endl;
        }
    }
// 习题3.5
string s1, s2, s3;
cin >> s1;
cin >> s2;
cin >> s3;
string s = s1 + s2 + s3;
cout << s << endl;

string ss = s1 + " " + s2 + " " + s3;
cout << ss << endl;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值