1.auto
auto不再是一个存储类型 指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期 推导而得。
用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际 只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
auto不能直接用来声明数组
声明变量必须具有初始值
cpp
// 遍历并修改
for (auto& n : nums) {
n *= 2; // ✅ 修改原元素
}
cpp
// 编译报错:rror C3531: "e": 类型包含"auto"的符号必须具有初始值设定项
auto e;
/ 编译报错:error C3538: 在声明符列表中,"auto"必须始终推导为同一类型
auto cc = 3, dd = 4.0;
// 编译报错:error C3318: "auto []": 数组不能具有其中包含"auto"的元素类型
auto array[] = { 4, 5, 6 };
2.范围for
1.C++11中引入了基于范围的for循环。
2.for循环后的括号由冒号" :"分为两部分:第一部分是范围 内用于迭代的变量,第二部分则表示被迭代的范围,自动迭代,自动取数据,自动判断结束。
3.范围for可以作用到数组和容器对象上进行遍历
4.范围for的底层很简单,容器遍历实际就是替换为迭代器,这个从汇编层也可以看到。
cpp
string str("hello world");
for (auto ch : str)
{
cout << ch << " ";
}
3.string常用接口
3.1.构造string:

3.2.string类对象的容量操作

**reserve:**void reserve (size_t n = 0);
为string预留空间,不改变有效元素个数,当reserve的参 数小于string的底层空间总大小时,reserver不会改变容量大小。
**resize:**调整字符串大小 ,可以变大或变小。当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数 增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
void resize (size_t n); 调整到 n 个字符
void resize (size_t n, char c);调整到 n 个字符,新增的填充 c
clear: clear()只是将string中有效字符清空,不改变底层空间大小。
3.3. string类对象的访问及遍历操作

3.4. string类对象的修改操作

find:
size_t find(char ch, size_t pos = 0); // 找字符
size_t find(const char* str, size_t pos = 0); // 找子串
size_t find(const string& str, size_t pos = 0); // 找另一个 string
注意:
-
在string尾部追加字符时,s.push_back(c) / s.append(1, c) / s += 'c'三种的实现方式差 不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可 以连接字符串。
-
对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留 好。
3.5. string类非成员函数

**getline:**读取一整行输入 ,包括空格,遇到分隔符停止。
istream& getline (istream& is, string& str, char delim);//最后遇到delim停止
istream& getline (istream& is, string& str);//默认遇到\0停止
**注意:**insert ,replace,erase这三个函数谨慎使用,底层涉及数据挪动。效率低下
3.6. vs和g++下string结构的说明
注意:下述结构是在32位平台下进行验证,32位平台下指针占4个字节。
vs下string的结构
string总共占28个字节,内部结构稍微复杂一点,先是有一个联合体,联合体用来定义 string中字符串的存储空间:
当字符串长度小于16时,使用内部固定的字符数组来存放 当字符串长度大于等于16时,从堆上开辟空间
g++下string的结构
G++下,string是通过写时拷贝实现的,string对象总共占4个字节,内部只包含了一个 指针,该指针将来指向一块堆空间,内部包含了如下字段: 空间总大小 字符串有效长度 引用计数
4.string模拟实现
最主要是实现string类的构造、拷贝构造、赋值运算符重载以及析 构函数,这些很重要
.h
cpp
#pragma once
#include<iostream>
#include<string.h>
#include<assert.h>
#include <string>
using namespace std;
namespace dida
{
class string
{
public:
typedef char* iterator;
iterator begin()
{
return _str;
}
iterator end()
{
return _str + _size;
}
string();
string(const char* str);
const char* c_str() const;
~string();
string(const string& s);
//string& operator=(const string& s);
string& operator=(string s);
void swap(string& s);
size_t size() const;
const char& operator[](size_t i) const;
char& operator[](size_t i);
void reserve(size_t n);
void push_back(char str);
void append(const char* str);
void pop_back();
void clear();
string& operator+=(char ch);
string& operator+=(const char*str);
string& insert(size_t pos, char ch);
string& insert(size_t pos,const char* str);
string& erase(size_t pos, size_t len = npos);
size_t find(char ch, size_t pos = 0);
size_t find(const char*str, size_t pos = 0);
string substr(size_t pos, size_t len = npos);
bool operator<(const string& s)const;
bool operator<=(const string& s)const;
bool operator>(const string& s)const;
bool operator>=(const string& s)const;
bool operator==(const string& s)const;
bool operator!=(const string& s)const;
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
public:
static const size_t npos;
};
ostream& operator<<(ostream& out, const string& s);
istream& operator>>(istream& in, string& s);
istream& getline(istream& in, string& s, char delim = '\n');
}
.cpp:
cpp
#define _CRT_SECURE_NO_WARNINGS
#include"string.h"
namespace dida
{
const size_t string::npos = -1;
string::string()
:_str(new char[1] {'\0'})
, _size(0)
, _capacity(0)
{}
string::string(const char* str)
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_size + 1];
memcpy(_str, str, _size + 1);
}
const char* string::c_str() const
{
return _str;
}
string::~string()
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
/*string::string(const string& s)
{
_str = new char[s._capacity + 1];
memcpy(_str, s._str, s._size + 1);
_size = s._size;
_capacity = s._capacity;
}*/
void string::swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
string::string(const string& s)//现代写法:拷贝构造,更简洁,但效率不提升
{
string tmp(s._str);
swap(tmp);
}
//string& string::operator=(const string& s)
//{
// if (this != &s)
// {
// /*char* tmp = new char[s._capacity + 1];
// memcpy(tmp, s._str, s._size + 1);
// delete[] _str;
// _str = tmp;
// _size = s._size;
// _capacity = s._capacity;*/
// string tmp(s);
// swap(tmp);
// }
// return *this;
//}
string& string::operator=( string tmp)
{
swap(tmp);
return *this;
}
size_t string::size() const
{
return _size;
}
const char& string::operator[](size_t i) const
{
assert(i < _size);
return _str[i];
}
char& string::operator[](size_t i)
{
assert(i < _size);
return _str[i];
}
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* str = new char[n + 1];
//strcpy(str, _str);
memcpy(str, _str, _size + 1);
delete[] _str;
_str = str;
_capacity = n;
}
}
void string::push_back(char ch)
{
if (_size >= _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(newcapacity);
}
_str[_size] =ch;
++_size;
_str[_size] = '\0';
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
{
size_t newcapacity = _capacity * 2 > _size + len ? 2 * _capacity : _size + len;
reserve(newcapacity);
}
//strcpy(_str + _size, str);
memcpy(_str+_size, str, len + 1);
_size += len;
}
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)
{
//out << s.c_str();
for (size_t i = 0; i < s.size(); i++)
{
out << s[i];
}
return out;
}
istream& operator>>(istream& in, string& s)
{
s.clear();
char buff[128];
int i = 0;
char ch = in.get();
while (ch != ' ' && ch != '\n')
{
buff[i++] = ch;
if (i == 127)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
istream& getline(istream& in, string& s, char delim)
{
s.clear();
char buff[128];
int i = 0;
char ch = in.get();
while (ch !=delim)
{
buff[i++] = ch;
if (i == 127)
{
buff[i] = '\0';
s += buff;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
buff[i] = '\0';
s += buff;
}
return in;
}
string& string::insert(size_t pos, char ch)
{
assert(pos <= _size);
if (_size >= _capacity)
{
size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(newcapacity);
}
/* int end = _size;
while (end >=(int)pos)
{
_str[end + 1] = _str[end];
--end;
}*/
size_t end = _size + 1;
while (end > pos)
{
_str[end] = _str[end - 1];
--end;
}
_str[pos] = ch;
++_size;
return *this;
}
string& string::insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity)
{
size_t newcapacity = _capacity * 2 > _size + len ? 2 * _capacity : _size + len;
reserve(newcapacity);
}
/* int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}*/
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
--end;
}
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
_size += len;
return *this;
}
string& string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len == npos ||len >= (_size - pos))
{
_size = pos;
_str[_size] = '\0';
}
else
{
size_t i = pos + len;
memmove(_str + pos, _str + i, _size - i + 1);
_size -= len;
}
return *this;
}
void string::pop_back()
{
assert(_size > 0);
_size--;
_str[_size] = '\0';
}
void string::clear()
{
_str[0] = '\0';
_size = 0;
}
size_t string::find(char ch, size_t pos)
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == ch)
{
return i;
}
}
return npos;
}
size_t string::find(const char* str, size_t pos )
{
const char* p1 = strstr(_str+ pos, str);
if (p1 == nullptr)
{
return npos;
}
else
{
return p1 - _str;
}
}
string string::substr(size_t pos, size_t len)
{
if (len == npos || len >= _size - pos)
{
len = _size - pos;
}
string ret;
ret.reserve(len);
for (size_t i = 0; i < len; i++)
{
ret += _str[pos + i];
}
return ret;
}
bool string::operator<(const string& s)const
{
size_t i1 = 0, i2 = 0;
while (i1 < _size && i2 < s._size)
{
if (_str[i1] < s[i2])
{
return true;
}
else if (_str[i1] > s[i2])
{
return false;
}
else
{
i1++;
i2++;
}
}
return i2 < s._size;
}
bool string::operator<=(const string& s)const
{
return *this < s || *this == s;
}
bool string::operator>(const string& s)const
{
return !(*this <= s);
}
bool string::operator>=(const string& s)const
{
return !(*this < s);
}
bool string::operator==(const string& s)const
{
size_t i1 = 0, i2 = 0;
while (i1 < _size && i2 < s._size)
{
if (_str[i1] != s[i2])
{
return false;
}
else
{
i1++;
i2++;
}
}
return i2 ==s._size&&i1==_size;
}
bool string::operator!=(const string& s)const
{
return !(*this == s);
}
}