C++STL——string类详解及其模拟实现

简介: C++STL——string类详解及其模拟实现

C++STL——string类

1. STL简介

STL全称standard template libaray,译为标准模板库

  • 需要注意,STL不是C++的标准库,而是C++标准库的重要组成部分
  • STL是一个包含众多数据结构和算法软件框架

下面展示STL的六大组件:


本章,我们将对STL中的容器——string部分常用的功能进行说明和使用,最后对string进行简单的模拟实现

本章思维导图:

注:本章思维导图已经同步导入至资源

2. string

头文件<string>

我们先来看看string类是如何被声明的:

typedef basic_string<char> string;

可以看到:

string类是被类模板basic_string用数据类型char实例化后得到的一个具体的类的别名

同时规定:

  • string类是一个用来表示字符串的类
  • 标准string类提供的接口和许多标准容器的接口类似,但也额外提供了处理单字节字符的接口(处理字符串的常规操作)
  • 不能用来操作多字节或者变长字节的字符序列

需要注意:

string类是包含在C++标准库中的,因此如果要使用string

  • 一种方式:使用域作用限定符,例如std::string
  • 另一种方式:使用using namespace std打开命名空间

2.1 成员函数

2.2.1 默认成员函数

2.1.1.1 constructor——构造函数

string类的构造函数string()被重载成了许多形式,但我们只需掌握以下三种即可:

string(); //方法一:构造一个空串
string (const string& str);   //方法二:拷贝构造
string (const char* s);   //方法三:用一个常量字符串构造

例如:

#include <string>
#include <iostream>
nt main()
{
  std::string s1; 
  std::string s2("hello world");
  std::string s3(s2);
    //string类里面重载了流插入<<和流提取>>运算符
  std::cout << "字符串s1为:" << s1 << std::endl;
  std::cout << "字符串s2为:" << s2 << std::endl;
  std::cout << "字符串s3为:" << s3 << std::endl;
  return 0;
}

output:

字符串s1为:
字符串s2为:hello world
字符串s3为:hello world
2.2.1.2 operator =——赋值运算符重载
string& operator= (const string& str);  //类类型之间的赋值
string& operator= (const char* s);  //利用隐式类型转换,先将字符串s实例化为一个string类型对象,再进行赋值
string& operator= (char c);   //和第二种方法类似

例如:

#include <string>
#include <iostream>
int main()
{
  std::string s1, s2, s3;
  s1 = "hello world";
  s2 = 'c';
  s3 = s1;
  std::cout << "字符串s1为:" << s1 << std::endl;
  std::cout << "字符串s2为:" << s2 << std::endl;
  std::cout << "字符串s3为:" << s3 << std::endl;
  return 0;
}

output:

字符串s1为:hello world
字符串s2为:c
字符串s3为:hello world

2.2.2 size()/length——获取有效长度

size_t size() const;
size_t length() const;
  • 这两个函数的效果是一模一样的
  • 但是更建议使用函数size()
  • 字符串的有效长度是不包括结束字符‘\0’,其结果就和C语言的strlen()函数一样

例如:

#include <string>
#include <iostream>
int main()
{
  std::string s1;
  std::string s2("hello world");
  std::cout << "sizs of s1: " << s1.size() << std::endl;
  std::cout << "sizs of s2: " << s2.size() << std::endl;
  return 0;
}

output:

sizs of s1: 0
sizs of s2: 11

2.2.3 capacity()——获取最大容量

size_t capacity() const;
  • 一般来说,这个最大容量同样指的是可以存放有效字符的最大容量,也不包括结束符‘\0’

需要注意,由于不同平台所用的库不同,因此当用同一个字符串构造string对象时,分配给其用来存储字符的初始空间也不一定相同(即capacity不一定相同),例如:

对于相同的代码:

#include <string>
#include <iostream>
int main()
{
  std::string s1;
  std::string s2("nice to meet you");
  std::cout << "capacity of s1 is: " << s1.capacity() << std::endl;
  std::cout << "capacity of s2 is: " << s2.capacity() << std::endl;
  return 0;
}

在VS下,output:

capacity of s1 is: 15
capacity of s2 is: 31

在Linux下,output:

capacity of s1 is: 0
capacity of s2 is: 16
  • 实际上,这两个平台的扩容机制也完全不同,之后我们会进行演示

2.2.4 operator []/at()——获取指定位置的字符

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
      char& at (size_t pos);
const char& at (size_t pos) const;

相同点:

  • 和普通的字符数组一样,string类类型的对象也可以通过类似[下标]的方式获得指定位置的字符
  • 这个函数被重载成了两份,分别给非cosnt对象和const对象使用

不同点:

  • 如果传入的pos大于size(),那么对于operator [],则会直接报错
  • 而对于at(),则会抛出异常

有了这两个成员函数,我们就可以对string对象存储的数据进行遍历访问了:

#include <string>
#include <iostream>
int main()
{
  std::string s1("hello world");
  for (int i = 0; i < s1.size(); i++)
    std::cout << s1[i] << ' ';
  std::cout << std::endl;
  for (int i = 0; i < s1.size(); i++)
    std::cout << s1.at(i) << ' ';
  std::cout << std::endl;
  return 0;
}

output:

h e l l o   w o r l d
h e l l o   w o r l d

2.2.5 iterator——迭代器

迭代器是一个用来访问容器数据的对象,其提供了统一的方式来遍历容器中的数据

  • 对于string类,我们可以将迭代器看成一个指针,其指向string对象存储的某个字符
  • 我们可以通过迭代器来访问或者修改容器中的数据
  • 尽管前面的[]运算符访问和修改string存储的数据十分方便,但必须说明,STL中,对于访问和遍历数据迭代器才是最常用的

以下是几种获取string类型迭代器的常见方式:

2.2.5.1 begin()/end()
iterator begin();
const_iterator begin() const;
/*****************************************/
      iterator end();
const_iterator end() const;
  • begin()即返回一个指向字符序列第一个字符的迭代器;end()即返回一个指向字符序列**最后一个字符(即‘\0’)**的迭代器
  • begin() constend() const则是针对const对象做出的函数重载

注意:

由于string类实际上就是存储字符序列的类,因此针对它的迭代器iterator,我们可以将其看成为一个指向char类型的指针char*;而const_iterator则对应的是const char*

  • 有些小伙伴可能会疑惑:为什么const_iterator不写成const iterator
  • 首先我们要清楚对于const对象,其返回的迭代器应该具有这样的功能:允许访问(遍历)数据,但不允许修改数据
  • const iterator本质上修饰的是迭代器iterator本身,我们可以看作是char* const,这样子的效果是不能改变迭代器指向,但是可以改变迭代器指向数据的内容,这显然是不符合要求的
  • 但**const_iterator本质上修饰的就是迭代器指向的数据**,我们可以看作是const char*,这样就可以符合要求

示例:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  string::iterator it = s1.begin();
    //对每个字符进行+1操作再进行打印
  while (it != s1.end())
  {
    (*it)++;
    cout << *it << ' ';
    it++;
  }
  cout << endl;
  return 0;
}

output:

i f m m p ! x p s m e
2.2.5.2 rbegin()/ rend()
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
/*****************************************/
      reverse_iterator rend();
const_reverse_iterator rend() const;
  • begin()/end()类似,只是返回的是反向迭代器
  • 所谓的反向迭代器即rbegin()返回一个指向字符序列最后一个有效字符的迭代器;rend()返回一个指向字符序列**第一个字符之前的字符(被认为是反向末端)**的迭代器
  • 如果反向迭代器的指向可以修改,那么例如对于rbegin()的返回结果进行+1操作,就会使迭代器指向倒数第二个字符
  • 反向迭代器和正向迭代器的返回类型原理类似,故不作赘述

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  string::reverse_iterator it = s1.rbegin();
  while (it != s1.rend())
  {
        cout << *it << ' ';
    it++;
  }
  cout << endl;
  return 0;
}

output:

d l r o w   o l l e h

2.2.6 容量管理

2.2.6.1 reserve
void reserve (size_t n = 0);
  • reserve成员函数会将string的最大容量capacity扩展为n
  • reserve()只会对capacity做出该变,而不会实际影响原来的数据(既不会删除也不会创建)
  • 需要注意,由于不同平台依赖的库不同,所以reserve最终的效果也会不同,但是无论如何,reserve()绝不会影响到存储的数据

下面就来看看在VS和Linux两个平台上reserve()函数的不同之处:

对于同样一份代码:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  string s2 = s1;
  cout << "the capacity of s1 is " << s1.capacity() << endl;
  s1.reserve(100);
  cout << "after reserve(100), the capacity of s1 is " << s1.capacity() << endl;
  s1.reserve(50);
  cout << "after reserve(50), the capacity of s1 is " << s1.capacity() << endl << endl;
  cout << "the capacity of s2 is " << s2.capacity() << endl;
  s2.reserve(1);
  cout << "after reserve(1), the capacity of s2 is " << s2.capacity() << endl;
  return 0;
}

VS:

output:

the capacity of s1 is 15
after reserve(100), the capacity of s1 is 111
after reserve(50), the capacity of s1 is 111
the capacity of s2 is 15
after reserve(1), the capacity of s2 is 15

Linux:

output:

the capacity of s1 is 11
after reserve(100), the capacity of s1 is 100
after reserve(50), the capacity of s1 is 50
the capacity of s2 is 11
after reserve(1), the capacity of s2 is 11

可以总结出二者的不同:

  • VS分配空间时,总会比给定值多几个空间;而Linux则是给多少开多少
  • VS的reserve()函数不能缩小空间,只能扩大空间;而Linuxreserve函数可以缩小空间

同时它们也有一个共同点:

  • 无论给定值再怎么小,reserve()都不会影响到原来的数据
2.2.6.2 resize()
void resize (size_t n);
void resize (size_t n, char c);
  • resize()函数也是对最大容量的管理,同时也能影响到原有数据
  • 如果n < capacityresize()不会影响capacity
  • 如果n < size,那么执行该函数后,字符串将保留原来的前n个字符
  • 同时,这个函数还有初始化的功能:
  • 如果传入的n大于size小于capacity,那么如果字符c被指明,那么剩余的空间就会被字符c填充;如果字符c没被指明,那么剩余的空间就会被空字符‘\0’填充
  • 如果传入的n大于capacity,那么就会在扩容之后进行跟上面一样的初始化(填充)操作

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello");
  string s2 = s1;
  cout << "the capacity of s1 is " << s1.capacity() << endl;
  s1.resize(s1.capacity(), 'c');
  cout << s1 << endl << endl;
  cout << "the capacity of s2 is " << s2.capacity() << endl;
  s2.resize(100);
  cout << s2 << endl;
  cout << "the capacity of s2 is " << s2.capacity() << endl;
  cout << "the size of s2 is " << s2.size() << endl;
  return 0;
}

output:

the capacity of s1 is 15
hellocccccccccc
the capacity of s2 is 15
hello
the capacity of s2 is 111
the size of s2 is 100

2.2.7 增 operator +=

注:本篇只讲最常用的string添加字符/字符串的方法。其他方法还有如

string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1;
  string s2;
  string s3;
  s1 += "hello world";
  s2 += s1;
  s3 += 'c';
  cout << s1 << endl;
  cout << s2 << endl;
  cout << s3 << endl;
  return 0;
}

output:

hello world
hello world
c

2.2.8 删 erase

string& erase (size_t pos = 0, size_t len = npos);
  • 即删除从pos位置开始的len个字符
  • 注意:nposstring里面定义的一个const静态全局变量
const static size_t npos = -1;
  • 无符号整形npos的值为-1,因此它的实际值为unsigned int的最大值
  • 如果npos用来表示一个长度,那么它通常用来说明直到字符串的尾
  • 如果npos用来表示一个返回值,那么它通常用来说明没有找到目标

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  s1.erase(2, 2);
  cout << s1 << endl;
  s1.erase(1);
  cout << s1 << endl;
  return 0;
}

output:

heo world
h

2.2.9 查 find/rfind

注:本篇只讲述最常用的find和rfind。其他方法还有如:

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;
  • 即从pos位置开始,寻找目标出现的下标
  • rfind()find()使用类似,故不作赘述

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world !!!");
  size_t pos1 = s1.find("ld");
  size_t pos2 = s1.find("l", 6);
  size_t pos3 = s1.find("nice");
  size_t pos4 = s1.rfind("!");
  cout << "pos1 =  " << pos1 << endl;
  cout << "pos2 =  " << pos2 << endl;
  cout << "pos3 =  " << pos3 << endl;
  cout << "pos4 =  " << pos4 << endl;
  return 0;
}

output:

pos1 =  9
pos2 =  9
pos3 =  4294967295
pos4 =  14

2.2.10 改

注:虽然string类有专门用于修改字符串的函数👉replace,但是由于效率原因并不常用。

实际使用中,一般都是用[]下标访问和迭代器访问来修改数据

2.2.11 c_str——获得C语言类型的字符串

const char* c_str() const;

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  const char* str = s1.c_str();
  cout << str << endl;
  return 0;
}

output:

hello world

2.2.12 substr——获得子串

string substr (size_t pos = 0, size_t len = npos) const;
  • 获得从pos位置开始,长度为len的子串
  • 同时将这个子串存储到string类中并进行返回

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1("hello world");
  string s2 = s1.substr(0, 5);  //hello
  string s3 = s1.substr(5);   //world
  cout << s2 << s3 << endl;
  return 0;
}

output:

hello world

2.2 非成员函数

2.2.1 operator <</operator >>——流插入/流提取运算符重载

  • 有了<<流插入运算符重载,我们就可以利用std::cout来向屏幕打印string存储的字符序列
  • 有了>>流提取运算符重载,我们就可以用std::cinstring类的数据
  • 注意:
  • cin类似于C语言的scanf,如果碰到空白字符就会停止读取。因此cin只能用于读取不带空格的字符序列
  • 原来的数据会被输入端新字符给覆盖
  • 如果输入的字符长度大于capacity,那么就会对这个string对象进行扩容,直到可以存储输入的字符序列

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1, s2;
  cin >> s1;
  cin >> s2;
  cout << "s1 = " << s1 << endl;
  cout << "s2 = " << s2 << endl;
  return 0;
}

input:

hello
nice to meet you

output:

s1 = hello
s2 = nice

2.2.2 getline——输入字符串

istream& getline (istream& is, string& str);
  • 同样是从标准输入流向string对象输入数据
  • 原来的数据会被输入端新字符给覆盖
  • getline()类似于C语言的gets()只有遇到换行才会停止读取。因此可以读取带空格的字符序列
  • 如果输入的字符长度大于capacity,那么就会对这个string对象进行扩容,直到可以存储输入的字符序列

例如:

#include <string>
#include <iostream>
using namespace std;
int main()
{
  string s1;
  getline(cin, s1);
  cout << s1 << endl;
  return 0;
}

output:

hello world

2.2.3 relational operators——关系运算符重载

(1) 
bool operator== (const string& lhs, const string& rhs);
bool operator== (const char*   lhs, const string& rhs);
bool operator== (const string& lhs, const char*   rhs);
(2) 
bool operator!= (const string& lhs, const string& rhs);
bool operator!= (const char*   lhs, const string& rhs);
bool operator!= (const string& lhs, const char*   rhs);
(3) 
bool operator<  (const string& lhs, const string& rhs);
bool operator<  (const char*   lhs, const string& rhs);
bool operator<  (const string& lhs, const char*   rhs);
(4) 
bool operator<= (const string& lhs, const string& rhs);
bool operator<= (const char*   lhs, const string& rhs);
bool operator<= (const string& lhs, const char*   rhs);
(5) 
bool operator>  (const string& lhs, const string& rhs);
bool operator>  (const char*   lhs, const string& rhs);
bool operator>  (const string& lhs, const char*   rhs);
(6) 
bool operator>= (const string& lhs, const string& rhs);
bool operator>= (const char*   lhs, const string& rhs);
bool operator>= (const string& lhs, const char*   rhs);
  • C++string类关系运算符的逻辑与C语言字符串比较函数strcmp的逻辑类似,故不再赘述

3. string类简单模拟实现

#include <iostream>
#include <assert.h>
namespace TESY
{
    class string
    {
    public:
        //构造函数
        string(const char* str = "")
      :_capacity(strlen(str))
      ,_size(strlen(str))
    {
            assert(str);  //不能传入空指针    
      _str = new char[_size + 1]; //特别注意,这里开空间要多开一个给结束符'\0'留空间
      strcpy(_str, str);
    }
        //拷贝构造(深拷贝)
        string(const string& str)
    {
      char* temp = new char[str._capacity + 1];
      strcpy(temp, str._str);
      _str = temp;
      _capacity = str._capacity;
      _size = str._size;  
    }
        //赋值运算符重载(深拷贝)
        string& operator=(const string& str)
    {
      if (this != &str)
      {
        char* temp = new char[str._capacity + 1];
        strcpy(temp, str._str);
        delete[] _str;
        _str = temp;
        _capacity = str._capacity;
        _size = str._size;
      }
      return *this;
    }
        //尾插一个字符串
        void append(const char* str)
    {
      int len = strlen(str);
            //检查容量
      if (_size + len > _capacity)
      {
        reserve(_size + len);
      }
      strcpy(_str + _size, str);
      _size += len;
    }
        //尾插一个字符
        void push_back(const char c)
    {
            //检查容量
      if (_size >= _capacity)
      {
        size_t newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
        reserve(newCapacity);
      }
      _str[_size] = c;
      _size++;
      _str[_size] = '\0';
    }
        //尾插
        string& operator+=(char c)
    {
      push_back(c);
      return *this;
    }
        string& operator+=(const char* str)
    {
      append(str);
      return *this;
    }
        string& operator+=(const string& str)
    {
      append(str._str);
      return *this;
    }
    //清空
        void clear()
    {
      delete[] _str;
      _size = _capacity = 0;
      _str = new char[1];
      _str[0] = '\0';
    }
        //交换两个string类的内容
        void swap(string& s)
    {
      std::swap(_str, s._str);
      std::swap(_capacity, s._capacity);
      std::swap(_size, s._size);
    }
        //返回C类型的字符串
        const char* c_str()const
    {
      return _str;
    }
        //返回长度
        size_t size()const
    {
      return _size;
    }
        //返回最大容量
        size_t capacity()const
    {
      return _capacity;
    }
        //判空
        bool empty()const
    {
      return _size == 0;
    }
        //修改容量
        void resize(size_t n, char c = '\0')
    {
      char* temp = new char[n + 1];
      int len = strlen(_str);
      if (n < len)
      {
        strncpy(temp, _str, n);
        temp[n] = '\0';
      }
      else
      {
        strncpy(temp, _str, len);
        for (int i = 0; i < n - len; i++)
          temp[len + i] = c;
        temp[n] = '\0';
      }
      delete[] _str;
      _str = temp;
      _size = n;
      _capacity = n;
    }
        //扩容
        void reserve(size_t n)
    {
      if (n > _capacity)
      {
        char* temp = new char[n + 1];
        strcpy(temp, _str);
        delete[] _str;
        _str = temp;
        _capacity = n;
      }
    }
        //下标访问
        char& operator[](size_t index)
    {
      assert(index <= _size);
      return _str[index];
    }
        const char& operator[](size_t index)const
    {
      assert(index <= _size);
      return _str[index];
    }
        //迭代器访问
        typedef char* iterator;
        typedef const char* const_iterator;
    iterator string::begin()
    {
      return _str;
    }
    iterator string::end()
    {
      return _str + _size;
    }
    const_iterator string::begin()const
    {
      return _str;
    }
    const_iterator string::end()const
    {
      return _str + _size;
    }
        //关系运算符重载
        friend bool operator<(const string& lhs, const string& rhs)
        {
            return strcmp(lhs._str, rhs._str) < 0;
        }
        friend bool operator<=(const string& lhs, const string& rhs)
        {
            return !(strcmp(lhs._str, rhs._str) > 0);
        }
        friend bool operator>(const string& lhs, const string& rhs)
        {
            return strcmp(lhs._str, rhs._str) > 0;
        }
        friend bool operator>=(const string& lhs, const string& rhs)
        {
            return (strcmp(lhs._str, rhs._str) < 0);
        }
        friend bool operator==(const string& lhs, const string& rhs)
        {
            return strcmp(lhs._str, rhs._str) == 0;
        }
        friend bool operator!=(const string& lhs, const string& rhs)
        {
            return strcmp(lhs._str, rhs._str) != 0;
        }
        // 返回c在string中第一次出现的位置
        size_t find(char c, size_t pos = 0) const
    {
      for (int i = pos; i < size(); i++)
      {
        if ((*this)[i] == c)
          return i;
      }
      return npos;
    }
        // 返回子串s在string中第一次出现的位置
        size_t find(const char* s, size_t pos = 0) const
    {
      char* ret = strstr(_str + pos, s);
      if (ret == nullptr)
        return npos;
      else
        return ret - _str;
    }
        // 在pos位置上插入字符c/字符串str
        string& insert(size_t pos, char c)
    {
      assert(pos <= _size);
            //检查容量
      if (_size >= _capacity)
      {
        size_t newCapacity = _capacity == 0 ? 4 : 2 * _capacity;
        reserve(newCapacity);
      }
            //挪动数据
      size_t end = _size + 1;
      while (end > pos)
      {
        _str[end] = _str[end - 1];
        end--;
      }
      _str[pos] = c;
      _size++;
      return *this;
    }
        string& insert(size_t pos, const char* str)
    {
      assert(pos <= _size);
            //检查容量
      int len = strlen(str);
      if (_size + len > _capacity)
      {
        reserve(_size + len);
      }
            //挪动数据
      size_t end = _size + len;
      while (end > pos)
      {
        _str[end] = _str[end - len];
        end--;
      }
      strncpy(_str + pos, str, len);
      _size += len;
      return *this;
    }
        //从pos位置开始删除len个字符
        string& erase(size_t pos = 0, size_t len = npos)
    {
      assert(pos < _size);
      if (len == npos || pos + len >= _size)
      {
        _str[pos] = '\0';
        _size = pos;
      }
      else
      {
        strcpy(_str + pos, _str + pos + len);
        _size -= len;
      }
      return *this;
    }
        //返回以从pos位置开始,长度为len的子串为内容的string对象
        string substr(size_t pos = 0, size_t len = npos) const
    {
      assert(pos < _size);
      string temp;
      size_t end = pos + len;
      if (len == npos || len + pos > _size)
        end = _size;
      temp.reserve(end - pos);
      temp._size = end - pos;
      strncpy(temp._str, _str + pos, end - pos);
      temp[end - pos] = '\0';
      return temp;
    }
        //析构
        ~string()
    {
      delete[] _str;
      _capacity = _size = 0;
    }
        //流插入
        friend std::ostream& operator<<(std::ostream& cout, const string& str)
        {
            cout << str._str;
        }
    private:
        char* _str;
        size_t _capacity;
        size_t _size;
        static const size_t npos = -1;
    };
    std::ostream& operator<<(std::ostream& cout, const string& str);
    bool operator<(const string& lhs, const string& rhs);
    bool operator<=(const string& lhs, const string& rhs);
    bool operator>(const string& lhs, const string& rhs);
    bool operator>=(const string& lhs, const string& rhs);
    bool operator==(const string& lhs, const string& rhs);
    bool operator!=(const string& lhs, const string& rhs);
};

本章完

如果本篇有任何错误或讲述不清的地方,欢迎各位在评论区讨论并指出

下一篇,我们将继续STL的学习——vector

相关文章
|
11天前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
46 12
|
1月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
53 16
|
1月前
|
缓存 安全 Java
《从头开始学java,一天一个知识点》之:字符串处理:String类的核心API
🌱 **《字符串处理:String类的核心API》一分钟速通!** 本文快速介绍Java中String类的3个高频API:`substring`、`indexOf`和`split`,并通过代码示例展示其用法。重点提示:`substring`的结束索引不包含该位置,`split`支持正则表达式。进一步探讨了String不可变性的高效设计原理及企业级编码规范,如避免使用`new String()`、拼接时使用`StringBuilder`等。最后通过互动解密游戏帮助读者巩固知识。 (上一篇:《多维数组与常见操作》 | 下一篇预告:《输入与输出:Scanner与System类》)
71 11
|
1月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
1月前
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
1月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
122 6
|
1月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
1月前
|
Java
课时14:Java数据类型划分(初见String类)
课时14介绍Java数据类型,重点初见String类。通过三个范例讲解:观察String型变量、&quot;+&quot;操作符的使用问题及转义字符的应用。String不是基本数据类型而是引用类型,但使用方式类似基本类型。课程涵盖字符串连接、数学运算与字符串混合使用时的注意事项以及常用转义字符的用法。
|
1月前
|
存储 JavaScript Java
课时44:String类对象两种实例化方式比较
本次课程的主要讨论了两种处理模式在Java程序中的应用,直接赋值和构造方法实例化。此外,还讨论了字符串池的概念,指出在Java程序的底层,DOM提供了专门的字符串池,用于存储和查找字符串。 1.直接赋值的对象化模式 2.字符串池的概念 3.构造方法实例化
|
5月前
|
存储 编译器 C语言
【c++丨STL】string类的使用
本文介绍了C++中`string`类的基本概念及其主要接口。`string`类在C++标准库中扮演着重要角色,它提供了比C语言中字符串处理函数更丰富、安全和便捷的功能。文章详细讲解了`string`类的构造函数、赋值运算符、容量管理接口、元素访问及遍历方法、字符串修改操作、字符串运算接口、常量成员和非成员函数等内容。通过实例演示了如何使用这些接口进行字符串的创建、修改、查找和比较等操作,帮助读者更好地理解和掌握`string`类的应用。
168 2

热门文章

最新文章

下一篇
oss创建bucket