string字符串库学习

string是c++开发中经常使用的的标准库的里类,本篇介绍string一些常的函数。

成员函数

成员函数

(构造函数)

构造 basic_string
(公开成员函数)

(析构函数)

销毁字符串,若使用内部存储则解分配它
(公开成员函数)

operator=

为字符串赋值
(公开成员函数)

assign

赋值字符给字符串
(公开成员函数)

示例代码:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void memberFunction()
{
    string str1;
    string str2("camel"); //构造函数初始化
    //(1) operator=(const basic_string&);
    str1 = str2;
    cout << std::quoted(str1) << ' ' //"camel"
         << std::quoted(str2) << endl;//"camel"
    //(2) operator=(basic_string&&);
    str1 = std::move(str2);
    cout << std::quoted(str1) << ' ' //"camel"
         << std::quoted(str2) << endl;//""或"camel"(未指定)
    //(3) operator=(const char*);
    str1 = "scott";
    cout << std::quoted(str1) << endl; //"scott"
    //(4) operator=(char);
    str1 = '?';
    cout << std::quoted(str1) << endl; // "?"
    //(5) operator=(std::initializer_list<char>);
    str1 = {'h', 'o', 'l', 'l', 'e'};
    cout << std::quoted(str1) << endl; //"hello"
    //(6) operator=(const T&);
    str1 = 35U; //equivalent to str1 = static_cast<char>(35U);
    cout << std::quoted(str1) << endl; //"#"(ASCII = 35)

    //assign示例
    string str;
    //assign(size_type count, char ch)
    str.assign(4, '$');
    cout << str << endl;
    string const c("scott");
    // assign(basic_string const& str)
    str.assign(c);
    cout << c << "==" << str << endl;
    // assign(basic_string const& str, size_type pos, size_type count)
    str.assign(c, 0, c.length() - 1);
    cout << str << endl;
    // assign(basic_string&& str)
    str.assign(std::string("scott ") + "camel");
    cout << str << endl;
    // assign(charT const* s, size_type count)
    str.assign("C-style string", 7);
    cout << str << endl; // "C-style"

    // assign(charT const* s)
    str.assign("C-style\0string");
    cout << str << endl; // "C-style"

    char mutable_c_str[] = "C-style string";
    // assign(InputIt first, InputIt last)
    str.assign(std::begin(mutable_c_str), std::end(mutable_c_str)-1);
    std::cout << str << endl; // "C-style string"

    // assign(std::initializer_list<charT> ilist)
    str.assign({ 'C', '-', 's', 't', 'y', 'l', 'e' });
    std::cout << str << endl; // "C-style"
}

int main()
{
    memberFunction();

    return 0;
}

运行结果:

 元素访问相关的API

元素访问

at

访问指定字符,有边界检查
(公开成员函数)

operator[]

访问指定字符
(公开成员函数)

front

访问首字符
(公开成员函数)

back

访问最后的字符
(公开成员函数)

data

返回指向字符串首字符的指针
(公开成员函数)

c_str

返回字符串的不可修改的 C 字符数组版本
(公开成员函数)

代码示例:

void elementAccess()
{
    string str("message");
    str.at(2) = 'y';
    cout << "str===========" << str << endl;
    cout << "str.at(2)=====" << str.at(2) << endl;
    cout << "str[2]========" << str[2] << endl;
    cout << "str.front()===" << str.front() << endl;
    cout << "str.back()====" << str.back() << endl;
    cout << "str.data()====" << str.data() << endl;
    cout << "str.c_str()===" << str.c_str() << endl;
}

int main()
{
    elementAccess();

    return 0;
}

运行结果:

容量

empty

检查字符串是否为空
(公开成员函数)

sizelength

返回字符数
(公开成员函数)

max_size

返回字符数的最大值
(公开成员函数)

reserve

保留存储
(公开成员函数)

capacity

返回当前对象分配的存储空间能保存的字符数量
(公开成员函数)

shrink_to_fit

通过释放不使用内存减少内存使用
(公开成员函数)
示例代码:
void capacityFuntion()
{
    string str("camel");
    cout << "str.empty()=========" << str.empty() << endl;
    cout << "str.size()==========" << str.size() << endl;
    cout << "str.length()========" << str.length() << endl;
    cout << "str.max_size()======" << str.max_size() << endl;
    cout << "str.capacity()======" << str.capacity() << endl;
    str.reserve(20);
    cout << "str.capacity()======" << str.capacity() << endl;
    str.shrink_to_fit();
    cout << "str.capacity()======" << str.capacity() << endl;
    cout << "str.data()==========" << str.data() << endl;
    cout << "str.c_str()=========" << str.c_str() << endl;
}

int main()
{
    capacityFuntion();
    return 0;
}

运行结果:

迭代器

begincbegin

(C++11)

返回指向起始的迭代器
(公开成员函数)

end cend

(C++11)

返回指向末尾的迭代器
(公开成员函数)

rbegin crbegin

(C++11)

返回指向起始的逆向迭代器
(公开成员函数)

rend crend

(C++11)

返回指向末尾的逆向迭代器
(公开成员函数)
示例代码
void iteratorsFuntion()
{
    string str("CameK");
    cout << "str.begin()=========" << *str.begin() << endl;
    cout << "str.cbegin()========" << *str.cbegin() << endl;
    cout << "str.end()===========" << *(--str.end()) << endl;//最后一个元素
    cout << "str.cend()==========" << *(--str.cend()) << endl;//最后一个元素
    cout << "str.rbegin()========" << *str.rbegin() << endl;//最后一个元素
    cout << "str.crbegin()=======" << *str.crbegin() << endl;//最后一个元素
    cout << "str.rend()===========" << *(--str.rend()) << endl;//第一个元素
    cout << "str.crend()==========" << *(--str.crend()) << endl;//第一个元素
}
int main()
{
    iteratorsFuntion();

    return 0;
}

运行结果:

 

参考:

https://zh.cppreference.com/w/cpp/header/string

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值