一、关于string常用的接口
1.string类对象的创建
string s1;
string s2("hello");
string s3(s2);
string s4("hello", 2);
cout << s4 << endl;
string s5(s2, 2, 1);
cout << s5 << endl;
string s6(s2, 2);
cout << s6 << endl;
string s7(s2, 2,string::npos);
cout << s7 << endl;
string s8(10, 'a');
cout << s8 << endl;
2.reserve和resize
s1.reserve(100);
cout << s1.capacity() << endl;
s1.resize(3);
cout << s1 << endl;
s1.resize(20);
cout << s1 << endl;
s1.resize(20, '0');
cout << s1 << endl;
s2.resize(40, '0');
cout << s2 << endl;
3.insert和erase
s1.insert(s1.begin(), '0');
s1.insert(2, "3");
cout << s1 << endl;
s1.erase(2, 3);
cout << s1 << endl;
s2.erase(4);
cout << s2 << endl;
s2.erase(1, 100);
cout << s2 << endl;
4.c_str
string s1("hello");
const char* str = s1.c_str();
while (*str)
{
cout << *str << " ";
++str;
}
cout << endl;
cout << s1 << endl;
cout << s1.c_str() << endl;
s1 += '\0';
s1 += " world";
cout << s1 << endl;
cout << s1.c_str() << endl;
5.find(非算法中的find)
string url("https://www.icourse163.org/home.htm?userId=1531618954#/home/course");
size_t i1 = url.find(':');
if (i1 != string::npos)
{
cout << url.substr(0, i1) << endl;
}
size_t i2 = url.find('/',i1 + 3);
if (i2 != string::npos)
{
cout << url.substr(i1 + 3, i2 - (i1 + 3)) << endl;
}
cout << url.substr(i2 + 1) << endl;
6.geline
string s;
getline(cin, s);
size_t pos = s.rfind(' ');
if (pos != string::npos)
{
cout << s.size() - (pos + 1) << endl;
}
二、string的模拟实现
1.模拟实现
namespace 9TSe
{
class string
{
friend istream& operator>>(istream& in, string& s);
public:
string(const char* str = "")
:_str(new char[strlen(str) + 1])
{
strcpy(_str, str);
_size = strlen(str);
_capacity = _size;
}
string(const string& s)
:_str(new char[strlen(s._str) + 1])
{
strcpy(_str, s._str);
_capacity = s._capacity;
_size = s._size;
}
~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
size_t size()const
{
return _size;
}
size_t capacity()const
{
return _capacity;
}
char& operator[](size_t i)
{
assert(i < _size);
return _str[i];
}
const char& operator[](size_t i)const
{
assert(i < _size);
return _str[i];
}
const char* c_str()
{
return _str;
}
void reserve(size_t i)
{
size_t newcapacity = i;
char* tmp = new char[newcapacity + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
void push_back(char ch)
{
insert(_size,ch);
}
void append(const char* str)
{
insert(_size,str);
}
string& operator+=(char ch)
{
push_back(ch);
return *this;
}
string& operator+=(const char* str)
{
append(str);
return *this;
}
string& insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size == _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
reserve(newcapacity);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
end--;
}
_str[pos] = ch;
++_size;
return *this;
}
string& insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
end--;
}
strncpy(_str+pos, str, len);
_size += len;
return *this;
}
void resize(size_t n,char ch)
{
if (n > _size)
{
if (n > _capacity)
{
reserve(n);
}
for (size_t i = _size; i < n; ++i)
{
_str[i] = ch;
}
}
_str[n] = '\0';
_size = n;
}
void erase(size_t pos, size_t len = npos)
{
assert(pos < _size);
if (len + pos >= _size)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t end = pos;
while (end <= pos + len)
{
_str[end] = _str[end + len];
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 = npos)
{
char* ret = strstr(_str + pos, str);
if (ret)
{
return ret - _str;
}
else
{
return npos;
}
}
string& operator=(const string& s)
{
if (this != &s)
{
char* tmp = new char[strlen(s._str) + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
return *this;
}
}
bool operator<(const string& s1)
{
size_t ret = strcmp(_str, s1._str);
return ret < 0;
}
bool operator>(const string& s1)
{
return !(*this < s1);
}
bool operator==(const string& s1)
{
size_t ret = strcmp(_str, s1._str);
return ret == 0;
}
bool operator>=(const string& s1)
{
return *this == s1 || *this > s1;
}
bool operator<=(const string& s1)
{
return !(*this > s1);
}
bool operator!=(const string& s1)
{
return !(*this == s1);
}
istream& getline(istream& in, string& s)
{
while (1)
{
char ch;
ch = in.get();
if (ch == '\n')
{
break;
}
else
{
s += ch;
}
}
return in;
}
private:
char* _str;
size_t _size;
size_t _capacity;
static size_t npos;
};
size_t string::npos = -1;
istream& operator>>(istream& in, string& s)
{
while (1)
{
char ch;
ch = in.get();
if (ch==' '||ch == '\n')
{
break;
}
else
{
s += ch;
}
}
return in;
}
ostream& operator<<(ostream& out, const string& s)
{
for (size_t i = 0; i < s.size(); ++i)
{
cout << s[i] << " ";
}
cout << endl;
return out;
}
2.string中构造,拷贝构造,赋值重载的简洁写法
namespace 9TSe
{
class string
{
public:
string(const char* str = "\0")
:_str(new char[strlen(str)+1])
{
strcpy(_str,str);
}
string(const string& s)
:_str(nullptr)
{
string tmp(s._str);
swap(_str, tmp._str);
}
string& operator=(string s)
{
swap(_str, s._str);
private:
char* _str;
};
}
namespace 9TSe
{
class string
{
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);
swap(tmp);
}
void swap(string& s)
{
::swap(_str, s._str);
::swap(_size,s._size);
::swap(_capacity, s._capacity);
}
string& operator=(string s)
{
_str = nullptr;
swap(s);
return *this;
}
private:
char* _str;
size_t _size;
size_t _capacity;
};