目录
1、string的介绍
2、string的初始化
2.1、方法1
2.2、方法2
2.3、方法3
2.4、方法4
2.5、方法5
2.6、方法6
3、string的赋值运算符重载
4、 string的常用内置函数使用
5、string的遍历
4.1数组下标访问
4.2迭代器
4.3范围for
6、模拟实现MyString
6.1、头文件:MyString.h
6.2、源文件:MyString.cpp
1、string的介绍
string 是表示字符串的字符串类
2、string的初始化
2.1、方法1
string();
//空字符串构造函数(默认构造函数),构造一个长度为零个字符的空字符串。string s;
2.2、方法2
string(const string & str);
//拷贝构造函数,构造str的副本string s1(str);
2.3、方法3
string(const string & str, size_t pos, size_t len = npos);
//子字符串构造函数,复制str中从字符位置pos开始并跨越len个字符的部分
//(如果str太短或len为string::npos,则复制到str的末尾)string s2(s, 1, 2);
2.4、方法4
string (const char* s);
//复制s指向的以'\0'结尾的字符序列string s3("A character sequence");string (const char* s, size_t n);
//从s指向的字符数组中复制前n个字符string s4("Another character sequence", 12);
2.5、方法5
string(size_t n, char c);
//用字符c的n个连续副本填充字符串。string s5(10, 'x');string s6(10, 100); //100是ascll码值
2.6、方法6
//迭代器初始化string(InputIterator first, InputIterator last);string s7(s.begin(), s.begin() + 7);
3、string的赋值运算符重载
string str1, str2, str3;string & operator= (const char* s);str1 = "Test string: "; string& operator= (char c);str2 = 'x'; string& operator= (const string & str); str3 = str1 + str2; str3 = str1;
4、 string的常用内置函数使用
string v(10, 'x');string v1 = "www";//Iterators(迭代器)v.begin(); //获取第一个数的位置v.end(); //获取最后一个数的位置v.rbegin(); //获取最后一个数的位置v.rend(); //获取第一个数的位置//Capacity(容量)v.size(); //获取v数据的个数v.length(); //获取v数据的个数v.max_size(); //返回字符串可以达到的最大长度v.clear(); //擦除字符串的内容,该字符串将变为空字符串(长度为0个字符)v.capacity(); //获取v容量的大小v.empty(); //判断v是否为空v.resize(10); //改变vector的size,将v的现有元素个数调整至10个,多则删,少则补,其值随机v.resize(10, 3); //将v的现有元素个数调整至10个,多则删,少则补,并且赋值为3v.reserve(100); //改变vector的capacity,将v的容量扩充至100//Element access(元素访问)v[0]; //下标访问v.at(3); //返回对字符串中位置pos处的字符的引用//与下标访问的区别,函数会自动检查pos是否是字符串中字符的有效位置//(即pos是否小于字符串长度),如果不是,则抛出out_of_range异常 //Modifiers(修改器)v += " K. "; //通过在字符串的当前值末尾附加附加字符来扩展字符串v.append(v1); //通过在字符串的当前值末尾附加附加字符来扩展字符串v.append(v1, 6, 3); v.append("dots are cool", 5); v.append("here: "); v.append(v1.begin(), v1.end());v.assign("c-string"); //为字符串指定一个新值,替换其当前内容v.swap(v1); //交换v.pop_back(); //尾删v.push_back(5); //尾插一个数,值为5v.insert(v.begin(), 2); //在pos前插入元素,例如头插2v.erase(v.begin()); //删除pos位置的数据,例如头删//string operations(字符串操作)v.c_str(); //字符转整型getline(cin, v); //输入值到v中,直到回车
5、string的遍历
4.1数组下标访问
string a("www.6666.hh");
for(int i = 0;i <= a.size() - 1;++i)
{cout << a[i] << endl;
}
4.2迭代器
string::iterator it = a.begin();while (it != a.end()){cout << *it << " ";++it;}cout << endl;
4.3范围for
for (auto ch : a){cout << ch << " ";}cout << endl;
6、模拟实现MyString
6.1、头文件:MyString.h
#pragma once
#include<assert.h>
#include <type_traits>
#include <ostream>
#include <istream>class MyString{public: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;}//MyString();MyString(const char* str = "");MyString(const MyString& s);~MyString();void swap(MyString& s);MyString& operator=(MyString tmp);char& operator[](size_t pos);const char& operator[](size_t pos) const;size_t capacity() const;size_t size() const;void reserve(size_t n);void resize(size_t n, char ch = '\0');void clear();size_t find(char ch, size_t pos = 0);size_t find(const char* sub, size_t pos = 0);void push_back(char ch);void append(const char* str);void insert(size_t pos, char ch);void insert(size_t pos, const char* str);void erase(size_t pos, size_t len = npos);MyString& operator+=(char ch);MyString& operator+=(const char* str);bool operator<(const MyString& s) const;bool operator==(const MyString& s) const;bool operator<=(const MyString& s) const;bool operator>(const MyString& s) const;bool operator>=(const MyString& s) const;bool operator!=(const MyString& s) const;MyString substr(size_t pos, size_t len = npos);const char* c_str() const;private:char* _str;size_t _size;size_t _capacity;public:const static size_t npos;};std::ostream& operator<<(std::ostream& out, const MyString& s);std::istream& operator>>(std::istream& in, MyString& s);
6.2、源文件:MyString.cpp
#include"MyString.h"const size_t MyString::npos = -1;
//MyString::MyString()
// :_str(new char[1] {'\0'})
// , _size(0)
// , _capacity(0)
//{}MyString::MyString(const char* str):_size(strlen(str)), _capacity(_size)
{_str = new char[_capacity + 1];strcpy(_str, str);
}void MyString::swap(MyString& s)
{std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);
}// s2(s1)
MyString::MyString(const MyString& s):_str(nullptr), _size(0), _capacity(0)
{MyString tmp(s._str);swap(tmp);
}// s2 = s3
MyString& MyString::operator=(MyString tmp)
{swap(tmp);return *this;
}MyString::~MyString()
{delete[] _str;_str = nullptr;_size = _capacity = 0;
}char& MyString::operator[](size_t pos)
{assert(pos < _size);return _str[pos];
}const char& MyString::operator[](size_t pos) const
{assert(pos < _size);return _str[pos];
}size_t MyString::capacity() const
{return _capacity;
}size_t MyString::size() const
{return _size;
}const char* MyString::c_str() const
{return _str;
}void MyString::reserve(size_t n)
{if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}
}void MyString::resize(size_t n, char ch)
{if (n <= _size){_str[n] = '\0';_size = n;}else{reserve(n);while (_size < n){_str[_size] = ch;++_size;}_str[_size] = '\0';}
}size_t MyString::find(char ch, size_t pos)
{for (size_t i = pos; i < _size; i++){if (_str[i] == ch){return i;}}return npos;
}size_t MyString::find(const char* sub, size_t pos)
{const char* p = strstr(_str + pos, sub);if (p){return p - _str;}else{return npos;}
}MyString MyString::substr(size_t pos, size_t len)
{MyString s;size_t end = pos + len;if (len == npos || pos + len >= _size) // 有多少取多少{len = _size - len;end = _size;}s.reserve(len);for (size_t i = pos; i < end; i++){s += _str[i];}return s;
}void MyString::push_back(char ch)
{if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}_str[_size] = ch;++_size;_str[_size] = '\0';
}void MyString::append(const char* str)
{size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}strcpy(_str + _size, str);_size += len;
}MyString& MyString::operator+=(char ch)
{push_back(ch);return *this;
}MyString& MyString::operator+=(const char* str)
{append(str);return *this;
}// insert(0, 'x')
void MyString::insert(size_t pos, char ch)
{assert(pos <= _size);if (_size == _capacity){reserve(_capacity == 0 ? 4 : _capacity * 2);}size_t end = _size + 1;while (end > pos){_str[end] = _str[end - 1];--end;}_str[pos] = ch;_size++;
}void MyString::insert(size_t pos, const char* str)
{assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reserve(_size + len);}// 挪动数据int end = _size;while (end >= (int)pos){_str[end + len] = _str[end];--end;}strncpy(_str + pos, str, len);_size += len;
}void MyString::erase(size_t pos, size_t len)
{assert(pos < _size);if (len == npos || pos + len >= _size){_str[pos] = '\0';_size = pos;}else{size_t begin = pos + len;while (begin <= _size){_str[begin - len] = _str[begin];++begin;}_size -= len;}
}bool MyString::operator<(const MyString& s) const
{return strcmp(_str, s._str) < 0;
}bool MyString::operator==(const MyString& s) const
{return strcmp(_str, s._str) == 0;
}bool MyString::operator<=(const MyString& s) const
{return *this < s || *this == s;
}bool MyString::operator>(const MyString& s) const
{return !(*this <= s);
}bool MyString::operator>=(const MyString& s) const
{return !(*this < s);
}bool MyString::operator!=(const MyString& s) const
{return !(*this == s);
}void MyString::clear()
{_str[0] = '\0';_size = 0;
}std::ostream& operator<<(std::ostream& out, const MyString& s)
{for (auto ch : s)out << ch;return out;
}std::istream& operator>>(std::istream& in, MyString& s)
{s.clear();char buff[129];size_t i = 0;char ch;ch = in.get();while (ch != ' ' && ch != '\n'){buff[i++] = ch;if (i == 128){buff[i] = '\0';s += buff;i = 0;}//s += ch;ch = in.get();}if (i != 0){buff[i] = '\0';s += buff;}return in;
}