目錄
一、標(biāo)準(zhǔn)庫中的string類
1、簡單介紹string類
2、string類的常用接口注意事項(xiàng)
2.1、string類對象的常用構(gòu)造
2.2、string類對象的容量操作
2.3、string類對象的訪問及遍歷操作
2.4、string類對象的修改操作
二、string類的模擬實(shí)現(xiàn)
一、標(biāo)準(zhǔn)庫中的string類
1、簡單介紹string類
? ? ? ? (1)string是表示字符串的字符串類;
? ? ? ? (2)string類的接口與常規(guī)容器的接口基本相同,在添加了一些專門用來操作string的常規(guī)操作;
? ? ? ? (3)string的底層實(shí)際是:basic_string模板類的別名,typedef basic_string<char,char_traits,allocator> string;
? ? ? ? (4)不能操作多字節(jié)或者變長字符的序列。
2、string類的常用接口注意事項(xiàng)
2.1、string類對象的常用構(gòu)造
? ? ? ? 標(biāo)準(zhǔn)庫給出的string類對象常用的構(gòu)造函數(shù)有很多,我們經(jīng)常用到的主流構(gòu)造方式有三種:用模板提供的默認(rèn)構(gòu)造函數(shù)構(gòu)造空的string類對象、用常量字符串構(gòu)造string類對象以及用現(xiàn)有的string類對象進(jìn)行拷貝構(gòu)造string類對象。
2.2、string類對象的容量操作
? ? ? ? (1)size()與length()方法的底層實(shí)現(xiàn)原理完全相同,引入size()的原因是為了與其他容器的接口保持一致,一般情況先都是用size()。
? ? ? ? (2)clear()只是將string類對象中的有效字符清空,不改變底層空間大小。
? ? ? ? (3)resize(size_t n)與resize(size_t n,char c)都是將字符串中的有效字符個數(shù)改變成n個,不同的是當(dāng)字符個數(shù)增多時:resize(n)用0來填充多出來的元素空間,resize(size_t n,char c)是用字符c來填充多出來的元素空間。注意:resize()在改變元素個數(shù)時,如果是將元素個數(shù)增多,可能會改變底層容量的大小,如果是將元素個數(shù)減少,底層空間的總大小保持不變,并不會隨著元素個數(shù)的減少而縮小容量空間。
? ? ? ? (4)reserve(size_t res_arg=0):為string預(yù)留空間,不改變有效元素個數(shù),當(dāng)reserve的參數(shù)小于string的底層空間總大小時,reserve不會改變?nèi)萘康拇笮 ?/p>
2.3、string類對象的訪問及遍歷操作
? ? ? ? string類對象的訪問方式有三種:下標(biāo)訪問、迭代器訪問、范圍for訪問。這里主要討論迭代器訪問方式。
? ? ? ? 迭代器是一個類,現(xiàn)階段可以把迭代器當(dāng)成一個指針來使用(實(shí)際上不一定是指針),迭代器是在類的里邊定義的,即內(nèi)部類,使用方式如:string::iterator。string類中與迭代器搭配使用的成員函數(shù)包括begin()、end()、rbegin()、rend()。
2.4、string類對象的修改操作
文章來源:http://www.zghlxwxcb.cn/news/detail-791186.html
? ? ? ? string類提供了很多字符串修改接口,需要說的是:在string尾部追加自字符時,s.push_back(c)/s.append(1,c)/s+='c'三種實(shí)現(xiàn)方式幾乎一樣,一般情況下更多的選用+=操作,+=操作不僅可以連接單個字符,還可以連接字符串;對string操作時,如果能夠大概預(yù)估到待存儲字符串的長度,可以先通過reserve把空間預(yù)留好。文章來源地址http://www.zghlxwxcb.cn/news/detail-791186.html
二、string類的模擬實(shí)現(xiàn)
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
namespace lbj
{
class string
{
friend ostream& operator<<(ostream& _cout, const string& s);
friend istream& operator>>(istream& _cin, string& s);
typedef char* iterator;
public:
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
string(const string& s) : _str(nullptr), _size(0), _capacity(0)
{
string tmp(s._str);
this->swap(tmp);
}
string& operator=(const string& s)
{
if (this != &s)
{
string temp(s);
this->swap(temp);
}
return *this;
}
~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
}
}
//
// iterator
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
/
// modify
void push_back(char c)
{
if (_size == _capacity)
reserve(_capacity * 2);
_str[_size++] = c;
_str[_size] = '\0';
}
string& operator+=(char c)
{
push_back(c);
return *this;
}
void append(const char* str)
{
int len = strlen(str);
if (_size + len > _capacity)
{
reserve(_size + len);
//_capacity = _size + len;
}
strcpy(_str + _size, str); //strcpy()會將‘\0’也拷貝過來,所以不需要手動添加'\0'
_size += len;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
void clear()
{
_size = 0;
_str[_size] = '\0';
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
const char* C_Str()const
{
return _str;
}
/
// capacity
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
bool empty()const
{
return _size == 0;
}
void resize(size_t newSize, char c = '\0')
{
if (newSize > _size)
{
// 如果newSize大于底層空間大小,則需要重新開辟空間
if (newSize > _capacity)
{
reserve(newSize);
}
memset(_str + _size, c, newSize - _size);
}
_size = newSize;
_str[newSize] = '\0';
}
void reserve(size_t newCapacity)
{
// 如果新容量大于舊容量,則開辟空間
if (newCapacity > _capacity)
{
char* str = new char[newCapacity + 1];
strcpy(str, _str);
// 釋放原來舊空間,然后使用新空間
delete[] _str;
_str = str;
_capacity = newCapacity;
}
}
/
// access
char& operator[](size_t index)
{
assert(index < _size);
return _str[index];
}
const char& operator[](size_t index)const
{
assert(index < _size);
return _str[index];
}
/
//relational operators
bool operator<(const string& s)const
{
int res = strcmp(_str, s._str);
if (res < 0)
return true;
return false;
}
bool operator<=(const string& s)const
{
return !(*this > s);
}
bool operator>(const string& s)const
{
int res = strcmp(_str, s._str);
if (res > 0)
return true;
return false;
}
bool operator>=(const string& s)const
{
return !(*this < s);
}
bool operator==(const string& s)const
{
int res = strcmp(_str, s._str);
if (res == 0)
return true;
return false;
}
bool operator!=(const string& s)const
{
return !(*this == s);
}
// 返回c在string中第一次出現(xiàn)的位置
size_t find(char c, size_t pos = 0) const
{
for (size_t i = pos; i < _size; ++i)
{
if (_str[i] == c)
return i;//找到,返回下標(biāo)
}
return -1;//未找到
}
// 返回子串s在string中第一次出現(xiàn)的位置
size_t find(const char* s, size_t pos = 0) const
{
assert(s);
assert(pos < _size);
const char* src = _str + pos;
while (*src)
{
const char* match = s;//如果不匹配,返回子串起始處重新查找
const char* cur = src;
while (*match && *match == *cur)//結(jié)束條件
{
++match;
++cur;
}
if (*match == '\0')//找到子串
{
return src - _str;//返回下標(biāo)
}
else
{
++src;
}
}
return -1;//未找到
}
// 在pos位置上插入字符c/字符串str,并返回該字符的位置
string& insert(size_t pos, char c)
{
assert(pos <= _size);
if (_size > _capacity)
{
//擴(kuò)容
char* newstr = new char[_capacity * 2 + 1];//開空間
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity *= 2;
//Expand(_capacity * 2);
}
//移數(shù)據(jù)
for (int i = _size; i >= (int)pos; --i)
{
_str[i + 1] = _str[i];
}
_str[pos] = c;
_size++;
return *this;
}
string& insert(size_t pos, const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)//擴(kuò)容
{
//擴(kuò)容
char* newstr = new char[_capacity * 2 + 1];//開空間
strcpy(newstr, _str);
delete[] _str;
_str = newstr;
_capacity *= 2;
//Expand(_size + len);
}
//后移數(shù)據(jù)
for (int i = _size; i >= (int)pos; --i)
{
_str[len + i] = _str[i];
}
//拷貝字符串
while (*str != '\0')
{
_str[pos++] = *str++;
}
_size += len;
return *this;
}
// 刪除pos位置上的元素,并返回該元素的下一個位置
string& erase(size_t pos, size_t len)
{
assert(pos < _size);
if (pos + len >= _size)//pos位置之后全為0
{
_str[pos] = '\0';
_size = pos;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
return *this;
}
private:
char* _str;
size_t _capacity;
size_t _size;
};
//輸入流重載
istream& operator>>(istream& _cin, string& s)
{
//預(yù)分配100個空間
char* str = (char*)malloc(sizeof(char) * 100);
char* buf = str;
int i = 1;
//預(yù)處理:跳過流里面的所有空格和回車
while ((*buf = getchar()) == ' ' || (*buf == '\n'));
for (; ; ++i)
{
if (*buf == '\n') //回車跳出
{
*buf = '\0';
break;
}
else if (*buf == ' ') //空格跳出
{
*buf = '\0';
break;
}
else if (i % 100 == 0) //空間不足
{
i += 100; //追加100個空間
str = (char*)realloc(str, i);
}
else //每次getchar()一個值
{
buf = (str + i);//為了避免realloc返回首地址改變,不使用++buf,而是用str加上偏移.
//每次讀取一個字符
*buf = getchar();
}
}
//輸入完成,更新s
s._str = str;
s._capacity = s._size = i;
return _cin;
}
//輸出流重載
ostream& operator<<(ostream& _cout, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
_cout << s[i];
}
return _cout;
}
};
到了這里,關(guān)于【c++】string類的使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!