【C++】string接口详解

目录

1.构造函数-vector

2.赋值运算符重载

3.迭代器-iterators

正向迭代器

begin()

end()

反向迭代器

rbegin()

rend()

4.容器(Capacity)

size()

length()

max_size()

resize()

capacity()

reserve()

clear()

empty()

5.元素访问(Element access)

operator[]

at()

6.对象修改

operator+=

append()

push_back()

assign()

insert()

erase()

replace()

swap()

7.字符串操作(String operations)

c_str()

data()

copy()

find()

rfind()

substr()

compare()

接口的模拟实现


1.构造函数-vector

//缺省构造函数
string ( );
//拷贝构造函数
string ( const string& str );
//用一个对象str的从pos位置开始的n个字符来构造对象
string ( const string& str, size_t pos, size_t n = npos );
//用字符串的前n个字符构造对象
string ( const char * s, size_t n );
//用一个字符串构造对象
string ( const char * s );
//用n个字符c构造对象
string ( size_t n, char c );
//用一段区间构造对象----区间内的对象不限类型
template<class InputIterator> string (InputIterator begin, InputIterator end);

2.赋值运算符重载

//对象 = 对象
string& operator= ( const string& str );
//对象 = 字符串
string& operator= ( const char* s );
//对象 = 字符
string& operator= ( char c );

3.迭代器-iterators

//string中迭代器就是char*指针重命名
typedef char* iterator;//普通迭代器
typedef const char* const_iterator;//const修饰的迭代器

正向迭代器

begin()

iterator begin();//普通类型
const_iterator begin() const;//const修饰类型

end()

iterator end();
const_iterator end() const;

反向迭代器

rbegin()

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

rend()

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
//实现原理较复杂

4.容器(Capacity

size()

//返回有效字符个数
size_t size() const;

length()

//等同size
size_t length() const;

max_size()

//返回能够存储的最大个数
size_t max_size ( ) const;

resize()

//设置对象的size的大小,比以前size大的部分用字符c填充
void resize ( size_t n, char c );
s("hello");
s.resize(10,'@');//s("hello@@@@@")
//设置对象的size的大小,比以前size大的部分用字符‘0’填充
void resize ( size_t n );
s.resize(10);//s("hello00000")

capacity()

//返回对象的容量
size_t capacity ( ) const;

reserve()

//设置对象的容量,一般用来扩容,如果传的参数小于已有的容量和size,那么他的容量不变,size变小
void reserve ( size_t res_arg=0 );

clear()

//清空对象size,不改变容量
void clear();

empty()

//判空
bool empty ( ) const;

5.元素访问(Element access

operator[]

//其实就是[]运算符重载,用来返回字符元素的引用,效率高,不具备下标月结检测
const char& operator[] ( size_t pos ) const;
      char& operator[] ( size_t pos );

at()

//和[]的功能一致,但有下标越界检测,效率不如[]
const char& at ( size_t pos ) const;
      char& at ( size_t pos );

6.对象修改

operator+=

//+=运算符重载,往对象的字符串后面续接
//续接一个对象
string& operator+= ( const string& str );
//续接一个字符串
string& operator+= ( const char* s );
//续接一个字符
string& operator+= ( char c );

append()

//和+=功能相同
//续接对象
string& append ( const string& str );
//续接一个对象从pos位置开始后的n个字符
string& append ( const string& str, size_t pos, size_t n );
//续接一个字符串的前n个字符
string& append ( const char* s, size_t n );
//续接字符串
string& append ( const char* s );
//续接n个字符c
string& append ( size_t n, char c );
//续接一段区间
template <class InputIterator>
   string& append ( InputIterator first, InputIterator last );

push_back()

//尾插
void push_back ( char c );

assign()

//用参数给对象赋值
//用对象赋值
string& assign ( const string& str );
//用一个对象从pos位置后n个字符赋值
string& assign ( const string& str, size_t pos, size_t n );
//用字符串的前n个字符赋值
string& assign ( const char* s, size_t n );
//用字符串赋值
string& assign ( const char* s );
//用n个字符c赋值
string& assign ( size_t n, char c );
//用一段区间赋值
template <class InputIterator>
   string& assign ( InputIterator first, InputIterator last );

insert()

 //指定位置插入
 //插入一个对象
 string& insert ( size_t pos1, const string& str );
 //插入一个对象的从pos2位置开始的n个字符
 string& insert ( size_t pos1, const string& str, size_t pos2, size_t n );
//插入一个字符串的前n个字符
 string& insert ( size_t pos1, const char* s, size_t n);
//插入一个字符串
 string& insert ( size_t pos1, const char* s );
//插入n个字符c
 string& insert ( size_t pos1, size_t n, char c );
//插入字符c
iterator insert ( iterator p, char c );
//在p所指向的位置插入n个字符c
    void insert ( iterator p, size_t n, char c );
//插入一段区间
template<class InputIterator>
    void insert ( iterator p, InputIterator first, InputIterator last );

erase()

//指定位置删除
//删除从此位置开始往后n个元素,如果pos缺省,就从头开始删除,如果n缺省,就删除到末尾
string& erase ( size_t pos = 0, size_t n = npos );
//删除元素
iterator erase ( iterator position );
//删除一段区间
iterator erase ( iterator first, iterator last );

replace()

//从指定位置开始替换
//用一个对象替换[pos,pos1+n1)
string& replace ( size_t pos1, size_t n1,   const string& str );
//用一个对象替换i1->i2这段区间
string& replace ( iterator i1, iterator i2, const string& str );

//用一个对象从pos2位置开始的n2个字符替换[pos1,pos+n1)
string& replace ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 );

//用一个字符串的前n个字符替换[pos,pos1+n1)
string& replace ( size_t pos1, size_t n1,   const char* s, size_t n2 );
//用一个字符串的前n个字符替换i1->i2这段区间
string& replace ( iterator i1, iterator i2, const char* s, size_t n2 );

//用一个字符串替换[pos,pos1+n1)
string& replace ( size_t pos1, size_t n1,   const char* s );
//用一个字符串替换i1->i2这段区间
string& replace ( iterator i1, iterator i2, const char* s );

//用n2个字符c替换[pos,pos1+n1)
string& replace ( size_t pos1, size_t n1,   size_t n2, char c );
//用n2个字符c替换i1->i2这段区间
string& replace ( iterator i1, iterator i2, size_t n2, char c );

//用一段区间替换一段区间
template<class InputIterator>
   string& replace ( iterator i1, iterator i2, InputIterator j1, InputIterator j2 );

swap()

//交换两个对象
void swap ( string& str );

7.字符串操作(String operations

c_str()

//返回对象字符串
const char* c_str ( ) const;

data()

//返回对象字符串中的数据
const char* data() const;

copy()

//把一个字符串从pos开始的n个字符复制给对象
size_t copy ( char* s, size_t n, size_t pos = 0) const;

find()

//正向查找
//从pos位置开始查找对象str
size_t find ( const string& str, size_t pos = 0 ) const;
//从pos位置开始查找字符串前n个字符
size_t find ( const char* s, size_t pos, size_t n ) const;
//从pos位置开始查找字符串
size_t find ( const char* s, size_t pos = 0 ) const;
//从pos位置开始查找字符c
size_t find ( char c, size_t pos = 0 ) const;

rfind()

//反向查找--用法和find一致
size_t rfind ( const string& str, size_t pos = npos ) const;
size_t rfind ( const char* s, size_t pos, size_t n ) const;
size_t rfind ( const char* s, size_t pos = npos ) const;
size_t rfind ( char c, size_t pos = npos ) const;

substr()

//从pos开始截取n个字符构造一个新对象
string substr ( size_t pos = 0, size_t n = npos ) const;

compare()

//用对象和参数比较
//和对象比较
int compare ( const string& str ) const;
//和字符串比较
int compare ( const char* s ) const;
//用对象的pos1位置开始的n1个字符的字符串和对象比较
int compare ( size_t pos1, size_t n1, const string& str ) const;
//c和字符串比较
int compare ( size_t pos1, size_t n1, const char* s) const;
用对象的pos1位置开始的n1个字符的字符串和对象的pos2位置开始的n2个字符的字符串比较
int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const;
//用对象的pos1位置开始的n1个字符的字符串和字符串的前n个字符组成的字符串比较
int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;

接口的模拟实现

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值