解聘 人力资源网站上怎么做,seoul是啥意思,佛山企业网站建站,大连建设网站的公司#x1f496;#x1f496;#x1f496;欢迎来到我的博客#xff0c;我是anmory#x1f496;#x1f496;#x1f496; 又和大家见面了 欢迎来到C探索系列 作为一个程序员你不能不掌握的知识 先来自我推荐一波 个人网站欢迎访问以及捐款 推荐阅读 如何低成本搭建个人网站… 欢迎来到我的博客我是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 stringint 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.hnamespace 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 (endpos){_str[end1] _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(_strpos, str);if (ptr nullptr){return npos;}return ptr - _str;}
}
#define _CRT_SECURE_NO_WARNINGS 1#include string.hvoid 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()
{/*vectorint v1;vectorint v2(10, 1);vectorint::iterator it v2.begin();while (it ! v2.end()){cout *it ;it;}cout endl;*/return 0;
}结语 非常感谢各位的支持 我们共同进步 本系列持续更新关注我带你了解更多C知识 下期再见