💖💖💖欢迎来到我的博客,我是anmory💖💖💖
又和大家见面了
欢迎来到C++探索系列
作为一个程序员你不能不掌握的知识
先来自我推荐一波
个人网站欢迎访问以及捐款
推荐阅读
如何低成本搭建个人网站
专栏:动画详解leetcode算法题
C语言知识
String常用接口解读
String类
string是代表了一串字符串的序列
标准string类通过类似于标准字节容器的接口为此类对象提供支持,但添加了专门设计用于处理单字节字符字符串的功能
字符串类是一个实例使用 char(即字节)作为其字符类型的basic_string类模板,其默认char_traits和分配器类型
此类处理字节与所使用的编码无关:如果用于处理多字节或可变长度字符(如 UTF-8)的序列,则此类的所有成员(如长度或大小)及其迭代器仍将根据字节(而不是实际编码字符)进行操作
成员函数
(Constructor)
:构造函数
string()
:构造一个空的字符串,长度为0
string(const char* str)
:拷贝构造一个字符串,参数由函数传递进来
string(const char* str,size_t pos,size_t len = npos)
:拷贝一个字符串的字串,从pos位置开始拷贝len个,如果len过长那么就从pos位置拷贝全部字符串
string(const char* s)
:复制以\0结尾的C字符串
string(const char* s,size_t n)
:拷贝字符串中的前n个字符
string(size_t n,char c)
:用n个连续的c填充string
更多请参考string构造函数
案例
// string constructor
#include <iostream>
#include <string>
int main ()
{
std::string s0 ("Initial string");
// constructors used in the same order as described above:
std::string s1;
std::string s2 (s0);
std::string s3 (s0, 8, 3);
std::string s4 ("A character sequence");
std::string s5 ("Another character sequence", 12);
std::string s6a (10, 'x');
std::string s6b (10, 42); // 42 is the ASCII code for '*'
std::string s7 (s0.begin(), s0.begin()+7);
std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a;
std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n';
return 0;
}
迭代器
begin
:返回迭代器到开始位置
end
:返回迭代器到结束位置
rbegin
:将迭代器指向字符串的末尾
rend
:将迭代器指向字符串的开头
cbegin
:返回const迭代器到开始位置
cend
:返回const迭代器到结束位置
crbegin
:将迭代器指向字符串的末尾
crend
:将const迭代器指向字符串的开头
更多内容请访问迭代器
容量
size
:返回字符串的长度
length
:返回字符串的长度
max_size
:返回字符串的最大尺寸
capacity
:返回字符串的容量
resize
:重新为字符串分配大小
reserve
:为字符串预留一定空间
clear
:清除字符串内容
更多请访问字符串
元素访问
operator[]
:获取字符串的字符
更多请访问元素访问
修改操作
operator+=
:在字符串末尾添加字符或字符串
append
:在字符串末尾添加字符或字符串
push_back
:在字符串末尾添加字符
insert
:在字符串中插入字符或字符串
erase
:在字符串中清除字符或字符串
swap
:交换字符串的值
更多请访问修改操作
string操作
c_str
:等效的C字符串
find
:找到字符串中的内容
find_first_of
:找到字符串中的字符
substr
:找到字符串中的子串
非成员函数重载
operator+
:连接字符串
relational operator
:字符串的比较
swap
:交换两个字符串的内容
operator>>
:流插入,用于输入
operator<<
:流提取,用于输出
getline
:将流中的行转换为字符串
String常用接口的模拟实现
#pragma once
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
namespace anmory
{
class string
{
friend ostream& operator<<(ostream& out, const string& s);
//friend istream& operator>>(istream& in, const string& s);
public:
const size_t npos = -1;
// iterator的定义和实现
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;
}
//你现在是在string这个类中,我重载的operator++也是stirng的,而不是迭代器的
void clear()
{
_str[0] = '\0';
_size = 0;
}
string(const char* str = "")// 这里不希望能够修改str的值,因此需要使用const
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];// 预留\0的位置
strcpy(_str, str);
}
// 构造函数
string(const string& s)
{
// 防止自我赋值导致错误
if (this != &s)
{
// 行赋值操作
_size = s._size;
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, s._str);
}
}
// 析构函数
~string()
{
delete[] _str;
_size = 0;
_capacity = 0;
}
const char* c_str() const
{
return _str;
}
size_t size()
{
return _size;
}
size_t capacity()
{
return _capacity;
}
void swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
// 重载=,+=
string& operator=(const string& s);
string& operator=(string s);
string& operator+=(const char* str);
string& operator+=(char ch);
void push_back(char ch);
void append(const char* str);
void reserve(size_t n);
size_t resize(size_t n);
size_t resize(size_t n, char c);
void insert(size_t pos, char c);
void insert(size_t pos, const char* str);
void erase(size_t pos, size_t len);
size_t find(char ch, size_t pos);
size_t find(const char* str, size_t pos,size_t len);
private:
char* _str = 0;
size_t _size = 0;
size_t _capacity = 0;
};
bool operator>(const string& s1, const string& s2);
bool operator>=(const string& s1, const string& s2);
bool operator<(const string& s1, const string& s2);
bool operator<=(const string& s1, const string& s2);
bool operator==(const string& s1, const string& s2);
bool operator!=(const string& s1, const string& s2);
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"
namespace anmory
{
const size_t npos = -1;
/*istream& operator>>(istream& in, string& s)
{
char ch;
in >> ch;
while (ch != ' ' && ch != '\n')
{
s += ch;
in >> ch;
}
return in;
}*/
string& string::operator=(const string& s)
{
{
// 防止自我赋值
if (this != &s)
{
delete[] _str;// 先删去原有的字符串内容
// 再进行赋值操作
_size = strlen(s._str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, s._str);
}
return *this;
}
}
string& string::operator=(string s)
{
swap(s);
return *this;
}
string& string::operator+=(char ch)
{
push_back(ch);
return *this;
}
string& string::operator+=(const char* str)
{
append(str);
return *this;
}
ostream& operator<<(ostream& out,const string& s)
{
//for (size_t i = 0; i < s._size; i++)
//{
// out << s._str[s._size];
//}
for (auto ch : s)
{
out << ch;
}
//for (string::iterator iter = s.begin(); iter != s.end(); ++iter)
//{
// out << *iter;
//}
return out;
}
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
void string::push_back(char ch)
{
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';// 为字符串最后手动加上\0
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
size_t newcapacity = _size + len > _capacity * 2 ? _size + len : _capacity * 2;
char* tmp = new char[newcapacity + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
strcat(_str, str);
_size += len;
}
size_t string::resize(size_t n)
{
if (n > _capacity)
{
size_t newcapacity = n > _capacity * 2 ? n : _capacity * 2;
_size = n;
char* tmp = new char[_capacity + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
if (n <= _size)
{
_size = n;
}
return _size;
}
bool operator>(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str())>0;
}
bool operator>=(const string& s1, const string& s2)
{
return s1 == s2 || s1 > s2;
}
bool operator<(const string& s1, const string& s2)
{
return !(s1 >= s2);
}
bool operator<=(const string& s1, const string& s2)
{
return !(s1 > s2);
}
bool operator==(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) == 0;
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
size_t string::resize(size_t n, char c)
{
if (n <= _size)
{
_size = n;
_str[_size] = '\0';
}
if(n>_capacity)
{
size_t newcapacity = n > _capacity * 2 ? n : _capacity * 2;
char* tmp = new char[newcapacity + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = newcapacity;
}
if (n > _size)
{
for (size_t i = _size; i < n; i++)
{
_str[i] = c;
}
_str[_size] = '\0';
_size = n;
}
return _size;
}
void string::insert(size_t pos, char c)
{
assert(pos < _size);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;
while (end>=pos)
{
_str[end+1] = _str[end];
end--;
}
_str[pos] = c;
_size++;
_str[_size + 1] = '\0';
}
void string::insert(size_t pos, const char* str)
{
assert(pos < _size);
size_t len = strlen(str);
if (_size == _capacity)
{
reserve(_capacity == 0 ? 4 : _capacity * 2);
}
size_t end = _size + 1;
size_t end2 = end + len;
while (end >= pos)
{
_str[end + len] = _str[end];
end--;
}
_size += len;
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
}
void string::erase(size_t pos, size_t len)
{
assert(pos < _size);
size_t end = pos + len;
while (_str[end] != '\0')
{
_str[pos++] = _str[end++];
}
_size -= len;
_str[_size] = '\0';
}
size_t string::find(char ch, size_t pos)
{
while (_str[pos] != '\0')
{
if (_str[pos] == ch)
{
return ch;
}
}
return string::npos;
}
size_t string::find(const char* str, size_t pos,size_t len)
{
char* ptr = strstr(_str+pos, str);
if (ptr == nullptr)
{
return npos;
}
return ptr - _str;
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"
void test_c_str()
{
anmory::string s("hello world");
cout << "test_c_str:> " << s.c_str() << endl;
}
void test_operator()
{
anmory::string s("hello world");
char ch = '*';
s += ch;
cout << "test+= :> " << s << endl;
string tmp;
//tmp = s;
cout << "test= :> " << tmp << endl;
}
void test_push_back()
{
anmory::string s("hello world");
s.push_back('x');
cout << "test_push_back:> " << s << endl;
}
void test_append()
{
anmory::string s1("hello world");
s1.append("xxxxx");
cout << "test_append:> " << s1 << endl;
}
void test_clear()
{
anmory::string s1("hello world");
s1.clear();
cout << "test_clear:> " << s1 << endl;
}
void test_reserve()
{
anmory::string s("hello world");
s.reserve(100);
cout << "test_reserve:> " << s.size() << " " << s.capacity() << endl;
}
void test_resize()
{
anmory::string s("hello world");
s.resize(19,'c');
cout << "test_resize:> " << s << endl;
}
void test_size()
{
anmory::string s("hello");
cout << "test_size:> " << s.size() << endl;
}
void test_insert()
{
anmory::string s("helloworld");
s.insert(4, 'x');
cout << "test_insert:> " << s << endl;
anmory::string s1("hello world");
s1.insert(5, "xxx");
cout << "test_insert:> " << s1 << endl;
}
void test_erase()
{
anmory::string s("hello world");
s.erase(5, 3);
cout << "test_erase:> " << s << endl;
}
int main()
{
test_c_str();
test_operator();
test_push_back();
test_append();
test_clear();
test_reserve();
test_resize();
test_size();
test_insert();
test_erase();
return 0;
}
模拟实现中的深拷贝和浅拷贝的问题
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class string
{
public:
void swap(string s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
string(const char* str = "")
{
_size = strlen(str);
_capacity = _size;
}
string(const string& s)
{
string tmp(s);
swap(tmp);
}
/*string& operator=(const string& s)
{
string tmp(s);
swap(tmp);
return *this;
}*/
string& operator=(string tmp)
{
swap(tmp);
return *this;
}
~string()
{
delete[] _str;
_size = 0;
}
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
};
int main()
{
/*vector<int> v1;
vector<int> v2(10, 1);
vector<int>::iterator it = v2.begin();
while (it != v2.end())
{
cout << *it << " ";
it++;
}
cout << endl;*/
return 0;
}
结语
💖💖💖非常感谢各位的支持💖💖💖
我们共同进步
本系列持续更新,关注我,带你了解更多C++知识
下期再见