Halo,这里是Ppeua。平时主要更新C语言,C++,数据结构算法…感兴趣就关注我bua!
终于放假啦,停更了一个月的博客也要重新拾起来了。近一个月都没怎么好好写代码,现在好多都看不懂了。在接下的时间里,会更新:算法题,ROS,C++,Linux相关内容。
String字符串是标准命名空间std的下的一个类。头文件为。是一个表示字符序列的类。学习STL模板库可以参考这个网站,自行查看传入参数以及返回参数等相关内容
constructor(构造函数),用于初始化string类对象。有以下几种函数重载:
default (1) | string(); |
---|---|
copy (2) | string (const string& str); |
substring (3) | string (const string& str, size_t pos, size_t len = npos); |
from c-string (4) | string (const char* s); |
from sequence (5) | string (const char* s, size_t n); |
fill (6) | string (size_t n, char c); |
我们仅介绍常用的,也就是:默认构造(1),拷贝构造(2)从const char*中构造string类,FILL(6)。
string s1;
这样就直接创建了一个空的String类对象s1,之后可以再用s1调用string类的接口
之后是拷贝构造函数:
cpp中类进行拷贝构造需要调用拷贝构造函数,传入参数都是const class&(具体为什么已经在之前介绍过了,忘记的可以看看这篇文章: 拷贝构造).
string (const string& s);
string s3(s2);
这里用s2对s3进行初始化.
从const char*中构造string类:
string (const char*);
string s2("hello world");
out:hello world
string类中包含n个字符c:
string (count,char);
string s4(5,'c');
out:ccccc
截取一定长度的字符:
第三个和第五个其实大差不差,虽然相较于第三个,第五个参数少了一个.但可以理解为,第五个是从0位置开始截取,而第三个是从pos位置开始截取.也可以说第三个是第五个构造函数的升级.(顺带一提在STL中,POS表示位置:position,LEN表示长度:length).
当然第五个只适用于char*,而第三个适用于string.有点绕,就是这两个没有什么必然的联系,理解起来可以按上面的来理解.
第三个:
string (const string& str, size_t pos, size_t len = npos);
string s2("hello world");
string s6(s2,6,-1);
out:world
第五个:
string (const char* s,size_t n);
string s5("hello world",5);
out:world
代码总览:
void constructor()
{
string s1;
string s2("hello world");
string s3(s2);
string s4(5,'c');
string s5("hello world",5);
string s6(s2,6,-1);
cout<<"s1:"<<s1.c_str()<<endl;
cout<<"s2:"<<s2.c_str()<<endl;//hello world
cout<<"s3:"<<s3.c_str()<<endl;//hello world
cout<<"s4:"<<s4.c_str()<<endl;//ccccc
cout<<"s5:"<<s5.c_str()<<endl;//hello
cout<<"s6:"<<s6.c_str()<<endl;//world
}
接下来我们来看看,有关String类的相关的容量操作.这里先提几个点:
size_t size() const;
size_t length() const;
Return length of string 返回字符串长度
这两个函数完全相同,引入size只是为了和其他STL容器接口保持一致.所以我们看size即可.
string s1("hello");
cout<<s1.size()<<endl;
cout<<s1.length()<<endl;
out:5
这里回顾下之前提到的第三点:'\0’的问题
string s1("hello");
s1+='\0';
s1+="!!!!!!!!";
cout<<"s1.size():"<<s1.size()<<endl;
const char * ch="hello'\0'!!!!!!!!";
cout<<"const char *:"<<strlen(ch)<<endl;
out:s1.size():14
out:const char* len:6
可以明显看出区别,这在之后模拟实现string会有大问题.
size_t capacity() const;
Return size of allocated storage
每一段字符串系统都会为他提前开辟好一段空间,而capacity则是返回这段空间的大小.
string s1("hello");
cout<<"s1.capacity():"<<s1.capacity()<<endl;
out:15
表示最开始时系统为s1这个字符串分配了15bit的空间
在不同的平台下,系统初始分配空间与每次扩容的大小是不同的.
bool empty() const;
Returns whether the string is empty (i.e. whether its length is 0
).
若string为空则返回true,否则返回false
string s1;
cout<<"s1.empty()"<<s1.empty()<<endl;
s1+="hello";
cout<<"s1.empty()"<<s1.empty()<<endl;
out:s1.empty():1
out:s1.empty():0
void clear();
无返回值,清楚string中的所有字符,令其size=0,但不改变已经分配的capacity
string s1("hello");
cout<<"s1:"<<s1<<endl;
cout<<"s1.size:"<<s1.size()<<endl;
cout<<"s1.capacity:"<<s1.capacity()<<endl;
s1.clear();
cout<<"s1:"<<s1<<endl;
cout<<"s1.size:"<<s1.size()<<endl;
cout<<"s1.capacity:"<<s1.capacity()<<endl;
out: s1:hello
s1.capacity:15
s1:
s1.size:0
s1.capacity:15
reserve():
void reserve (size_t n = 0);
改变capacity的大小,若n小于capacity则不改变,大于则不改变有效元素的个数仅扩充空间
string s1("hello");
cout<<"s1.capacity:"<<s1.capacity()<<endl;
s1.reserve(s1.capacity()-2);
cout<<"s1.capacity:"<<s1.capacity()<<endl;
s1.reserve(s1.capacity()+2);
cout<<"s1.capacity:"<<s1.capacity()<<endl;
out: s1.capacity:15
s1.capacity:15
s1.capacity:30
至于为什么最后一次不是17而是30,是这样解释的:
If n is greater than the current string capacity, the function causes the container to increase its capacity to n characters (or greater).
也就是会扩充到更大,至于大多少是由操作系统/编码平台来决定的.
resize():
void resize (size_t n);
void resize (size_t n, char c);
Resizes the string to a length of n characters.调整string长度为n的有效字符
有两个函数重载:当n
string s1("hello");
cout<<"s1.size:"<<s1.size()<<endl;
s1.resize(4);
cout<<"s1:"<<s1<<endl;
s1.resize(3,'c');
cout<<"s1:"<<s1<<endl;
s1.resize(8);
cout<<"s1:"<<s1<<endl;
s1.resize(16,'c');
cout<<"s1:"<<s1<<endl;
out: s1.size:5
s1:hell
s1:hel
s1:hel
s1.length():8
s1:helcccccccc
填充后的0是不显示的,但是存在的.
reserve与resize的区别:
先来看看下面这个代码有什么问题:
#include
#include
using namespace std;
int main()
{
string s1;
s1.reserve(10);
for(int i=0;i<10;i++)
{
s1[i]='a';
}
}
这个问题之前提到过:reserve是改变capacity,而通过[]去访问string需要size在这个范围里,也就是有效字符数在这个范围里.
#include
#include
using namespace std;
int main()
{
string s1;
s1.resize(10);
for(int i=0;i<10;i++)
{
s1[i]='a';
}
}
支持[]访问、迭代器(begin,end)与反向迭代器(rebegin,rend)
迭代器在这里可以看成一个指针,重点在实现string类中才会涉及。所以我们简单演示一下.
#include
#include
using namespace std;
int main()
{
string s1("hello");
string::iterator begin=s1.begin();
string::iterator end=s1.end();
for(;begin!=end;begin++)
{
cout<<*begin;
}
}
out:hello
#include
#include
using namespace std;
int main()
{
string s1("hello");
string::reverse_iterator rbegin=s1.rbegin();
string::reverse_iterator rend=s1.rend();
for(;rbegin!=rend;rbegin++)
{
cout<<*rbegin;
}
}
out:olleh
push_back() | 尾插一个字符 |
---|---|
append() | 尾插 |
operator+= | 尾插 |
c_str | 返回C格式的字符串 |
find+npos | pos开始往后找字符,返回位置 |
rfind | 从pos往前找字符,返回位置 |
substr | 从pos开始截取n个字符,然后返回 |
void push_back (char c);
Appends character c to the end of the string, increasing its length by one.
将一个字符插入到队尾,并将其长度+1
string s1;
s1.push_back('a');
cout<<"s1:"<<s1;
**out: **s1:a
append:
string (1) | string& append (const string& str); |
---|---|
substring (2) | string& append (const string& str, size_t subpos, size_t sublen); |
c-string (3) | string& append (const char* s); |
buffer (4) | string& append (const char* s, size_t n); |
fill (5) | string& append (size_t n, char c); |
其函数重载较多,我们也不都演示了.之前有过看源码的经历,所以较为轻松的能看出来每一个在干嘛:
(1) string
Appends a copy of str.
(2) substring
Appends a copy of a substring of str. The substring is the portion of str that begins at the character position subpos and spans sublen characters (or until the end of str, if either str is too short or if sublen is string::npos).
(3) c-string
Appends a copy of the string formed by the null-terminated character sequence (C-string) pointed by s.
(4) buffer
Appends a copy of the first n characters in the array of characters pointed by s.
(5) fill
Appends n consecutive copies of character c.
string s1;
s1.append("hello");
string s2;
s2.append(s1,1,-1);
cout<<"s1:"<<s1<<endl;
cout<<"s2:"<<s2<<endl;
string s3;
s3.append("const char *");
cout<<"s3:"<<s3<<endl;
string s4;
s4.append("const char *",5);
cout<<"s4:"<<s4<<endl;
out: s1:hello
s2:ello
s3:const char *
s4:const
operator+=:
是push_back与append的结合,既能追加单个字符也可以是一个string类型或是const char*
string s5;
s5+="h";
s5+="ello";
string s6(" world");
s5+=s6;
cout<<"s5:"<<s5<<endl;
out:s5:hello world
const char* c_str() const;
将string对象转换成const char*并返回
string s1("hello");
cout<<s1.c_str();
out:hello
这个函数的意义是:C++需要兼容C语言,很多接口只能读取C的char*类型,例如ROS中的消息传递,所以需要这么个函数.函数不复杂,但意义重大
string (1) | size_t find (const string& str, size_t pos = 0) const noexcept; |
---|---|
c-string (2) | size_t find (const char* s, size_t pos = 0) const; |
buffer (3) | size_t find (const char* s, size_t pos, size_type n) const; |
character (4) | size_t find (char c, size_t pos = 0) const noexcept; |
也是拥有多个重载:
若找到则返回对应位置,未找到则返回npos
string s1("hello hello world");
string s2("hello");
size_t pos=s1.find(s2);
cout<<"first s2 at:"<<pos<<endl;
pos=s1.find(s2,pos+1);
cout<<"second s2 at:"<<pos<<endl;
pos=s1.find("hello");
cout<<"first hello at:"<<pos<<endl;
pos=s1.find("hello",pos+1);
cout<<"second hello at:"<<pos<<endl;
pos=s1.find("hello",0,1);
cout<<"first h at:"<<pos<<endl;
out: first s2 at:0
second s2 at:6
first hello at:0
second hello at:6
first h at:0
rfind是从pos位置往前找:
string (1) | size_t rfind (const string& str, size_t pos = npos) const noexcept; |
---|---|
c-string (2) | size_t rfind (const char* s, size_t pos = npos) const; |
buffer (3) | size_t rfind (const char* s, size_t pos, size_t n) const; |
character (4) | size_t rfind (char c, size_t pos = npos) const noexcept; |
除了默认参数不同,其他都是相同的.这里就不演示了
string substr (size_t pos = 0, size_t len = npos) const;
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
返回一个对原字符串截取完的拷贝字符串.(所以是不会改变原字符串)
从pos位置开始截取len个长度
string s1("hello world");
string s2(s1.substr(6,-1));
cout<<"s2:"<<s2<<endl;
out: world
为了避免与库函数的string产生命名冲突,我们需要新建一个命名空间,在新的命名空间中模拟String:
namespace H{
class mystring{
}
}
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos;
const size_t mystring::npos = -1;
其有四个私有属性:
构造函数:
mystring(const char* str="")
:_str(new char[strlen(str) + 1])
, _size(strlen(str))
, _capacity(_size)
{
strcpy(_str, str);
}
注意:使用初始化列表需要与私有属性声明顺序要保持一致
拷贝构造函数:
mystring(const mystring& str)
{
_str = new char[str._capacity + 1];
//strcpy(_str, str._str);
memcpy(_str, str._str, str._size + 1);
_capacity = str._capacity;
_size = str._size;
}
这里不用strcpy,因为新加入的字符可能是’\0’,用strcpy会导致在’\0处’就停止.
析构函数:
~mystring() {
delete[]_str;
_size = _capacity = 0;
}
const char* c_str()const
{
return _str;
}
size_t size()const
{
return _size;
}
char& operator[](size_t pos)
{
assert(_size > pos);
return _str[pos];
}
const char& operator[](size_t pos)const
{
assert(_size > pos);
return _str[pos];
}
在这里 迭代器iterator可以直接看成一个指针,所以使用之前我们要进行一个typedef:
typedef char* iterator;
typedef const char* const_iterator;
之后就是正常的指针用法
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
const_iterator begin() const
{
return _str;
}
const_iterator end()const
{
return _str + _size;
}
写完迭代器后范围for就可以使用啦~
void reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
memcpy(tmp, _str, _size + 1);
delete[]_str;
_str = tmp;
_capacity = n;
}
}
这里也不能用strcpy.
void push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
_str[++_size] = '\0';
}
void append(const char* str)
{
size_t len = strlen(str);
if (_size + len >= _capacity)
{
reserve(_size+len);
}
strcpy(_str + _size, str);
_size += len;
}
先判断下是否会超过当前的capacity
mystring& operator+=(const char str)
{
push_back(str);
return *this;
}
mystring& operator+=(const char* str)
{
append(str);
return *this;
}
void insert(size_t pos, size_t n, char ch)
{
assert(pos <= _size);
if (_size + n > _capacity)
{
reserve(_size + n);
}
int end = _size;
while (end >= pos&&end!=npos)
{
_str[end + n] = _str[end];
end--;
}
for (size_t i = 0; i < n; i++)
{
_str[pos + i] = ch;
}
_size += n;
}
void insert(size_t pos, const char * ch)
{
size_t len = strlen(ch);
assert(pos<=_size);
if (_size+len > _capacity)
{
reserve(_size +len);
}
int end = _size;
while (end >= pos && end != npos)
{
_str[end + len] = _str[end];
end--;
}
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = ch[i];
}
_size += len;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos <= _size);
if (len == npos || pos + len >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t end = pos + len;
while (end <= _size)
{
_str[pos++] = _str[end];
end++;
}
_size -= len;
}
}
size_t find(char ch,size_t pos=0)
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t find(const char* str, size_t pos = 0)
{
assert(pos < _size);
const char* ptr = strstr(_str+pos, str);
if (ptr)
{
return ptr - _str;
}
else
return npos;
}
mystring substr(size_t pos = 0, size_t len = npos)
{
size_t n = len;
if (len == npos || pos + len > _size)
{
n = _size - pos;
}
mystring tmp;
tmp.reserve(n);
for (size_t i = pos; i < n+pos; i++)
{
tmp += _str[i];
}
return tmp;
}
void resize(size_t n,char ch='0')
{
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
}
void resize(size_t n,char ch='0')
{
if (n < _size)
{
_size = n;
_str[_size] = '\0';
}
else
{
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
void swap(mystring& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
提供两种写法 :
bool operator<(const mystring s1)const
{
size_t _i = 0;
size_t i = 0;
while (_i < _size && i < s1._size)
{
if (_str[_i] < s1._str[i])
{
return true;
}
else if (_str[_i] > s1._str[i])
{
return false;
}
else
{
_i++, i++;
}
}
if (_i == _size && i != s1._size)
return true;
return false;
}
bool operator<(const mystring s1)const
{
int ret = memcmp(_str, s1._str, _size < s1._size ? _size : s1._size);
return ret == 0 ? _size < s1._size : ret < 0;
}
之后所有的都可以由这个来改进
bool operator==(const mystring s)const
{
return _size == s._size && memcmp(_str, s._str, _size) == 0;
}
bool operator<=(const mystring s)const
{
return (*this < s) || (*this == s);
}
bool operator>=(const mystring s)const
{
return !(*this < s);
}
bool operator>(const mystring s)const
{
return !(*this <= s);
}
bool operator!=(const mystring s)const
{
return !(*this == s);
}
mystring& operator=( mystring tmp)
{
swap(tmp);
return *this;
}
这里很妙,先是调用拷贝构造,构造一个tmp对象.之后直接交换即可
ostream& operator<<(ostream& out,const H::mystring& s)
{
out << s.c_str();
return out;
}
istream& operator>>(istream& in, H::mystring& s)
{
s.clear();
char ch = in.get();
//处理buff之前的缓冲区
while (ch == ' ' || ch == '\n')
{
ch = in.get();
}
char buff[128];
int b = 0;
while (ch != ' ' && ch != '\n')
{
buff[b++] = ch;
if (b == 127)
{
buff[b] = '\0';
s += buff;
b = 0;
}
ch = in.get();
}
if (b != 0)
{
buff[b] = '\0';
s += buff;
}
return in;
}