第三十三课:c++中的字符串类----------狄泰软件学院

一、字符串类

c++语言直接支持C语言的所有概念
c++语言没有原生的字符串类

c++标准库直接提供了string类型
1.string直接支持字符串连接
2.string直接支持字符串大小比较
3.string直接支持子串查找和提取
4.string直接支持字符串的插入和替换

#include <iostream>
#include <string>

using namespace std;

void string_sort(string a[], int len)//排序
{
    for(int i=0; i<len; i++)
    {
        for(int j=i; j<len; j++)
        {
            if( a[i] > a[j] )
            {
                swap(a[i], a[j]);
            }
        }
    }
}

string string_add(string a[], int len)//连接
{
    string ret = "";

    for(int i=0; i<len; i++)
    {
        ret += a[i] + "; ";
    }

    return ret;
}

int main()
{
    string sa[7] = 
    {
        "Hello World",
        "D.T.Software",
        "C#",
        "Java",
        "C++",
        "Python",
        "TypeScript"
    };

    string_sort(sa, 7);

    for(int i=0; i<7; i++)
    {
        cout << sa[i] << endl;
    }

    cout << endl;

    cout << string_add(sa, 7) << endl;

    return 0;
}

二、字符串和数字的转换:

1.标准库中提供了相关的类对字符串和数字进行转换
2.字符串流(sstream)用于string的转换
头文件:
字符输入流:istringstream
字符输出流:ostringstream
使用方法:
1.string ===》 数字
istringstream iss(“1234”)double的
double num;//double的
iss >> num;//传输成功返回true
2.数字 ===》string
ostringstream oss;
oss<<543.21
tring s = oss.str;

#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
    istringstream iss("1234");//double的
    double num;//double的
    if(iss>>num)//返回值为bool类型
    {
        cout<<num<<endl;
    }
    ostringstream oss;
    oss<<123.1;//返回值为左操作数,所以cout<<123<<"."<<1也可以
    string s = oss.str();
    cout<<s<<endl;
    return 0;
}   

实际工程中,定义全局函数,但是多种类型就要定义多个函数
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
bool to_num(const string& s, int& num)
{
    istringstream iss(s);
    return iss>>num;//return true when success
}

string to_string(int n)
{
    ostringstream oss;
    oss<<n;
    return oss.str();
}

int main()
{
    int num;
    cout<<to_num("123456", num)<<endl;
    cout<<num<<endl;


    cout<<to_string(123)<<endl;
    return 0;
}   

这里先用宏来实现

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

#define TO_NUMBER(s, n) (istringstream(s) >> n)//为了各自类型都适应,利用直接调用构造函数产生临时对象的方法
#define TO_STRING(n) (((ostringstream&)(ostringstream() << n)).str())//将数字n传到流后直接调用str函数,然后强制类型转换

int main()
{

    double n = 0;

    if( TO_NUMBER("234.567", n) )
    {
        cout << n << endl;    
    }

    string s = TO_STRING(12345);

    cout << s << endl;     

    return 0;
}

面试题分析:字符串循环右移如(abcdefg循环右移三位后得到efgabcd)
用子串的方法,提取后再拼接,但要注意如abc右移4位与右移1位一样。故要用取余的方法
1.定位
2.取子串
3.拼接

#include<iostream>
#include<sstream>
using namespace std;
string operator >>(const string& s, unsigned int n)
{
    string ret = "";
    unsigned int pos = 0;
    n = n%s.length();
    pos = s.length() - n;
    ret = s.substr(pos);
    ret += s.substr(0, pos);
    return ret;

    //abc>>4 ====> abc>>1
    //n = 4 % 3 =1
    //pos = 4-1 = 3 定位到c的位置
    //将c及后面的字符取出
    //将0到c之前的字符取出并且与之前取出的子串拼接起来
}
int main()
{
    string s = "abc";
    string r;
    r = (s >> 4);
    cout<<r << endl;
    return 0;
}

小结:

应用开发中大多数的情况都在进行字符串处理
c++中没有直接支持原生的字符串类型
标准库中通过string类支持字符串的概念
string类支持字符串和数字的互相转换
string类的应用使得问题的求解变得简单

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值