string常用接口

一、string

  • string是表示字符串的字符串类
  • 头文件#include <string>还得写using namespace std;
  • 底层typedef basic_string<char> string;
  • 不能操作多字节或者变长字符的序列

二、成员函数

1、构造函数

函数名称功能说明
string();构造一个空字符串
string(const string& str);拷贝构造函数
string(const string& str, size_t n);用s中前n个字符构造新的string对象
string(const char* s);

用C-string来构造string对象

string(size_t n, char c);string对象包含n个字符c
void TestString()
{
    string s1;             //构造空的string类对象
    string s2("hello bit");//用C格式字符串构造string类对象
    string s3(10, 'a');    //用10个字符'a'构造string类对象
    string s4(s2);         //拷贝构造
    string s5(s3, 5);      //用s3的前5个字符构造string类对象
}

2、迭代器

函数名称功能说明

iterator begin();

获取字符串的第一个字符位置的iterator

iterator end();

获取字符串最后一个字符的下一个位置的iterator

reverse_iterator rbegin();

获取字符串最后一个字符位置的reverse_iterator

reverse_iterator rend();

获取字符串第一个字符上一个位置的reverse_iterator
const_iterator cbegin() const noexcept;获取字符串第一个字符位置的const_iterator
const_iterator cend() const noexcept;获取字符串最后一个字符的下一个位置的const_iterator
void TestString()
{
    string s("hello bit");
    //正向迭代器
    for(string::iterator it = s.begin(); it != s.end(); ++it)
        cout<<*it<<" ";
    cout<<endl;
    //反向迭代器
    for(string::reverse_iterator rit = s.rbegin(); rit != s.rend; ++it)
        cout<<*rit<<" ";
    cout<<endl;
}

3、容量

函数名称功能说明
size_t size() const;返回字符串有效字符长度
size_t length() const;返回字符串有效字符长度
void resize(size_t n);将有效字符个数改为n个,其余用0填充
void resize(size_t n, char c);

将有效字符个数改为n个,其余用字符c填充

size_t capacity() const;返回空间总大小
void reserve(size_t res_arg = 0);为字符串预留空间
void clear();清空有效字符
bool empty() const;检查字符串是否为空,是返回true,否则返回false
void TestString1()
{
    string s("hello bit");
    cout<<s.length()<<endl;  //输出字符串有效元素个数
    cout<<s.size()<<endl;    //输出字符串有效元素个数
    cout<<s.capacity()<<endl;//输出字符串所占空间大小
    s.clear();               //将s的字符串清空,清空只是将size置0,不改变底层空间大小
    s.resize(10, 'a');       //将有效元素个数增加到10个,多出的元素用字符'a'填充
    s.resize(15);            //将有效元素个数增加到15个,多出的元素用缺省值'\0'填充
    s.resize(5);             //将有效元素个数缩小到5个
}
void TestString()
{
    string s;
    s.reserve(100);//只会改变底层空间大小不会改变有效元素个数
    s.reserve(50); //就算参数小于底层空间大小也不会将空间缩小
}

4、元素访问

函数名称功能说明

char& operator[](size_t pos);

const char& operator[](size_t pos) const;

返回pos位置的字符
void TestString()
{
    string s1("hello bit");
    const string s2("Hello bit");
    cout<<s1[0]<<" "<<s2[0]<<endl;
    s1[0] = 'H';
    cout<<s1<<endl;
    for(int i = 0; i < s1.size(); ++i)
        cout<<s1[i]<<endl;
    //s2[0] = 'h';代码编译失败,因为s2是cost类型对象
}

5、修饰符

函数名称功能说明
string& operator += (const string& str);在字符串后追加字符串str
string& operator += (const char* s);在字符串后追加字符串s
string& operator += (char c);在字符串后追加字符c
string& append(const char* s);在字符串后追加字符串s
void push_back(char c);在字符串尾后插入字符c
string& insert(size_t pos, const string& str);在字符串pos位置插入字符串str
string& insert(size_t pos, const char* s);在字符串pos位置插入字符串s
string& insert(size_t pos, const cahr* s, size_t n);在字符串pos位置插入字符串s的前n个字符
string& insert(size_t pos, size_t n, char c);在字符串pos位置插入n个字符c
string& erase(size_t pos = 0, size_t len = npos);字符串pos位置开始删除len个字符
void swap(string& str);交换两个字符串
void TestString()
{
    string str;
    str.append("hello");          //在str后插入字符串"hello"
    str.push_back(' ');           //在str后插入空格
    str += 'b';                   //在str后插入字符'b'
    str += "it";                  //在str后插入字符串"it"
    str.insert(5, "hi");          //在下标为5的位置插入字符串"hi"
    str.insert(5, ' ');           //在下标为5的位置插入空格
    str.insert(8, "bit hello", 3);//在下标为8的位置插入"bit"
    str.erase(5, 5);            //位置5开始删除5个字符
    string s("HELLO BIT");
    str.swap(s);                  //str与s的字符串交换
}

6、字符串操作

函数名称功能说明
const cahr* c_str() const;返回C格式字符串
size_t find(char c, size_t pos = 0) const;从字符串pos位置开始后前找字符c,返回该字符在字符串中的位置
size_t rfind(char c, size_t pos = npos) const;从字符串pos位置开始向前找字符c,返回该字符在字符串中的位置
string substr(size_t pos = 0, size_t len = npos) const;在字符串pos位置开始截取n个字符,然后将其返回
void TestString()
{
    string str("hello bit");
    cout<<s.c_str()<<endl;//以C语言的方式打印字符串
    //获取文件后缀
    string str1("string.cpp");
    size_t pos1 = str1.rfind('.');
    string name(str1.substr(pos1, str1.size() - pos1));
    cout<<name<<endl;
    //获取域名
    string str2("http://www.baidu.com");
    size_t pos2 = str2.find("://");
    if(pos2 == string::npos)//未找到
        return;
    pos2 += 3;
    size_t pos3 = find('/', pos2);
    string adress(str2.substr(pos2, pos3 - pos2));
    cout<<adress<<endl;
}

7、非成员函数

此接口了解即可,注意此swap与上述swap不同,该函数为void swap(string& x, string& y);我们一般推荐使用上述swap的方法。string还有其他操作,我们在使用时查找文档即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值