STL | string

string

介绍

c++支持两种类型的字符串,一以NULL结尾的c风格字符串;二string类型的字符串

头文件

string是basic_string类模板使用char特化的类型

#include <string>

typedef basic_string<char, char_traits<char>, allocator<char>> string;

初始化

// 默认string对象,长度为0
string();
// 使用c风格的字符串初始化string
string(const char* s);
// 创建包含n个元素的string对象,并将每个元素赋值为c
string(size_type n, char c);
// 拷贝构造
string(cosnt string& str);
// 将string初始化为s的前n个字符
string(const char* s, size_type n);

template<class Iter>
string(Iter begin, Iter end);

// 将string初始化为str从pos开始到结尾的字符
string(const string& str, size_type pos = 0, size_type n = npos);

// 移动构造             
string(sting&& str) noexcept

// 初始化列表初始化
string(initializer_list<char> il)

函数

getline

将字符串从输入流中一行一行提取出来
方法一:使用默认分隔符(换行),ctrl+Z结束while循环,此时会将cin内部状态标志设置为eofbit,须调用cin.clear(),才能继续使用cin

string str;
vector<string> vec;
while(getline(cin, str)) {
    vec.push_back(str);
}

方法二:使用空格作为分隔符,ctrl+Z结束while循环,此时会将cin内部状态标志设置为eofbit,须调用cin.clear(),才能继续使用cin

string str;
vector<string> vec;
while(getline(cin, str, ' ')) {
    vec.push_back(str);  
}

与其他类型的转换

string转换为其他类型

// 将字符序列转换为double,idx表示第一个未转换字符的索引
double stod(const string& str, size_t* idx = 0)

// 转换为float
float stof(const string& str, size_t* idx = 0) 
// 转换为整型,base表示基,默认为十进制
int stoi(const string& str, size_t* idx = 0, int base 10)
long stol(const string& str, size_t* idx = 0, int base 10)
long double stold(const string& str, size_t* idx = 0)
long long stoll(const string& str, size_t* idx = 0, int base 10)
stoul
stoull

其他类型转换为string

// 有float double等参数的重载
string to_string(int value);
## 容量
// 返回字符串的长度size和length相同
size_type size() const;
size_type length() const;

// 返回最大可能的字符串长度
size_type max_size() const;
// 调整字符串的长度
void resize(size_type n);
// 新增部分填充c
void resize(size_type n, char c);
// 返回当前分配的存储空间
size_type capacity() const;
// 预留n的空间
void reserver(size_type n);
// 减少分配的存储空间以适应当前大小
void shrink_to_fit() noexcept;

访问

const_reference at(size_type pos) const;
reference at(size_type pos);

修饰器

void clear();
void push_back(char c);
void pop_back();
iterator insert(const_iterator pos, char c);
iterator erase(const_iterator pos);
iterator(const_iterator first, const_iterator last);

添加

string& append(const string& other);
string& append(const string& other, size_type pos, size_type count);
// 追加c字符串的count个字符
string& append(const char* s, size_type count);
string& append(const char* s);

插入

void insert(size_type pos, const string& other);
// 在pos位置插入c字符串
void insert(size_type pos, const string& other, size_type other_pos, size_type count);
void insert(size_type pos, const char* s, size_type count);
void insert(size_type pos, const char* s);

子串

// 返回从pos开始的子字符串, npos超过字符串末尾时会自动调整为字符串末尾
+ pos是开始位置
+ npos是字串的长度
string substr(size_type pos = 0, size_type count = npos) const; 

比较

返回负数表示当前字符串小于other,整数表示大于other,0表示相等

int compare(const string& other) const;
int compare(size_type pos, size_type count, const string& other) const;

int compare(size_type pos, size_type count, const string& other, 
            size_type other_pos, size_type other_count) const;

查找

返回查找到符合条件的索引位置

size_type find(const string& other) const;
size_type find(const string& other, size_type pos) const;
size_type find(const char* s) const;
size_type find(const char* s, size_type pos) const;
size_type find(const char c) const;
size_type find(char c, size_type pos);

// 反向查找
size_type rfind(const string& other) const;
size_tpye rfind(const char* s) const;
size_type rfind(char c) const;

// 查找第一次出现
size_type find_first_of(const string& other) const;
size_type find_first_of(const char* s) const;
size_type find_first_of(char c) const;

// 查找最后一次出现
size_type find_last_of(const string& other) const;
size_type find_last_of(const char* s) const;
size_type find_last_of(char c) const;

判空

bool empty();

交换

void swap(string& other);

其他

// 返回c字符串
const char* c_str() const;
// 返回字符串数组的指针
char* data() const;

运算符

  • operator+
  • operator+=
  • operator!=
  • operator=
  • operator==
  • operator<
  • operator<=
  • operator<<
  • operator>
  • operator>=
  • operator>>
  • operator[]
posted @   深蓝von  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示