1、string的基本概念及使用
1.1 基本概念
本质:
- string是C++风格的字符串,而string本质上是一个类
string和char * 区别:
- char * 是一个指针
- string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
特点:
string 类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete 替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
1.2 string构造函数
构造函数原型:
string();
//创建一个空的字符串 例如: string str;
string(const char* s);
//使用字符串s初始化string(const string& str);
//使用一个string对象初始化另一个string对象string(int n, char c);
//使用n个字符c初始化
示例:
#include <string>
//string构造
void test01()
{
string s1; //创建空字符串,调用无参构造函数
cout << "str1 = " << s1 << endl;
const char* str = "hello world";
string s2(str); //把c_string转换成了string
cout << "str2 = " << s2 << endl;
string s3(s2); //调用拷贝构造函数
cout << "str3 = " << s3 << endl;
string s4(10, 'a');
cout << "str3 = " << s3 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:string的多种构造方式没有可比性,灵活使用即可
1.3 string赋值操作
功能描述:
- 给string字符串进行赋值
赋值的函数原型:
string& operator=(const char* s);
//char*类型字符串 赋值给当前的字符串string& operator=(const string &s);
//把字符串s赋给当前的字符串string& operator=(char c);
//字符赋值给当前的字符串string& assign(const char *s);
//把字符串s赋给当前的字符串string& assign(const char *s, int n);
//把字符串s的前n个字符赋给当前的字符串string& assign(const string &s);
//把字符串s赋给当前字符串string& assign(int n, char c);
//用n个字符c赋给当前字符串
示例:
//赋值
void test01()
{
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("hello c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++",5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(5, 'x');
cout << "str7 = " << str7 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:
string的赋值方式很多,operator=
这种方式是比较实用的
1.4 string字符串拼接
功能描述:
- 实现在字符串末尾拼接字符串
函数原型:
string& operator+=(const char* str);
//重载+=操作符string& operator+=(const char c);
//重载+=操作符string& operator+=(const string& str);
//重载+=操作符string& append(const char *s);
//把字符串s连接到当前字符串结尾string& append(const char *s, int n);
//把字符串s的前n个字符连接到当前字符串结尾string& append(const string &s);
//同operator+=(const string& str)string& append(const string &s, int pos, int n);
//字符串s中从pos开始的n个字符连接到字符串结尾
示例:
//字符串拼接
void test01()
{
string str1 = "我";
str1 += "爱玩游戏";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "LOL DNF";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" love ");
str3.append("game abcde", 4);
//str3.append(str2);
str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
cout << "str3 = " << str3 << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:字符串拼接的重载版本很多,初学阶段记住几种即可
1.5 string查找和替换
功能描述:
- 查找:查找指定字符串是否存在
- 替换:在指定的位置替换字符串
函数原型:
int find(const string& str, int pos = 0) const;
//查找str第一次出现位置,从pos开始查找int find(const char* s, int pos = 0) const;
//查找s第一次出现位置,从pos开始查找int find(const char* s, int pos, int n) const;
//从pos位置查找s的前n个字符第一次位置int find(const char c, int pos = 0) const;
//查找字符c第一次出现位置int rfind(const string& str, int pos = npos) const;
//查找str最后一次位置,从pos开始查找int rfind(const char* s, int pos = npos) const;
//查找s最后一次出现位置,从pos开始查找int rfind(const char* s, int pos, int n) const;
//从pos查找s的前n个字符最后一次位置int rfind(const char c, int pos = 0) const;
//查找字符c最后一次出现位置string& replace(int pos, int n, const string& str);
//替换从pos开始n个字符为字符串strstring& replace(int pos, int n,const char* s);
//替换从pos开始的n个字符为字符串s
示例:
//查找和替换
void test01()
{
//查找
string str1 = "abcdefgde";
int pos = str1.find("de");
if (pos == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos = " << pos << endl;
}
pos = str1.rfind("de");
cout << "pos = " << pos << endl;
}
void test02()
{
//替换
string str1 = "abcdefgde";
str1.replace(1, 3, "1111");
cout << "str1 = " << str1 << endl;
}
int main() {
//test01();
//test02();
system("pause");
return 0;
}
总结:
- find查找是从左往后,rfind从右往左
- find找到字符串后返回查找的第一个字符位置,找不到返回-1
- replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
1.6 string字符串比较
功能描述:
- 字符串之间的比较
比较方式:
- 字符串比较是按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
函数原型:
int compare(const string &s) const;
//与字符串s比较int compare(const char *s) const;
//与字符串s比较
示例:
//字符串比较
void test01()
{
string s1 = "hello";
string s2 = "aello";
int ret = s1.compare(s2);
if (ret == 0) {
cout << "s1 等于 s2" << endl;
}
else if (ret > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大
1.7 string字符存取
string中单个字符存取方式有两种
char& operator[](int n);
//通过[]方式取字符char& at(int n);
//通过at方法获取字符
示例:
void test01()
{
string str = "hello world";
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
//字符修改
str[0] = 'x';
str.at(1) = 'x';
cout << str << endl;
}
int main() {
test01();
system("pause");
return 0;
}
总结:string字符串中单个字符存取有两种方式,利用 [ ] 或 at
1.8 string插入和删除
功能描述:
- 对string字符串进行插入和删除字符操作
函数原型:
string& insert(int pos, const char* s);
//插入字符串string& insert(int pos, const string& str);
//插入字符串string& insert(int pos, int n, char c);
//在指定位置插入n个字符cstring& erase(int pos, int n = npos);
//删除从Pos开始的n个字符
示例:
//字符串插入和删除
void test01()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl;
str.erase(1, 3); //从1号位置开始3个字符
cout << str << endl;
}
int main() {
test01();
system("pause");
return 0;
}
**总结:**插入和删除的起始下标都是从0开始
1.9 string子串
功能描述:
- 从字符串中获取想要的子串
函数原型:
string substr(int pos = 0, int n = npos) const;
//返回由pos开始的n个字符组成的字符串
示例:
//子串
void test01()
{
string str = "abcdefg";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
string email = "hello@sina.com";
int pos = email.find("@");
string username = email.substr(0, pos);
cout << "username: " << username << endl;
}
int main() {
test01();
system("pause");
return 0;
}
**总结:**灵活的运用求子串功能,可以在实际开发中获取有效的信息
2、string 的实现
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "iostream"
#include "assert.h"
#include "string.h"
using namespace std;
namespace mySTL
{
class string
{
public:
//迭代器
typedef char* iterator;
iterator begin(){
return _str;
}
iterator end(){
return _str + _size;
}
//构造函数
string(const char* s = ""){
_size = strlen(s);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, s);
}
//拷贝构造
string(const string& s) :_str(nullptr), _size(0), _capacity(0) {
string tmp(s._str);
swap(tmp);
}
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 tmp(s);
swap(tmp);
return *this;
}
//传值 交换
string& operator=(string s){
swap(s);
return *this;
}
//析构函数
~string(){
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
//c_str()
const char* c_str() const{
return _str;
}
//size()
size_t size() const{
return _size;
}
size_t capacity() const{
return _capacity;
}
//普通对象,可读可写
char& operator[](size_t pos){
//判断位置释放合法
assert(pos <= _size);
return _str[pos];
}
//const对象,只读
const char& operator[](size_t pos) const
{
assert(pos <= _size);
return _str[pos];
}
//reserve() 预先扩好空间
void reserve(size_t n){
if (n > _capacity){
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
//resize()
void resize(size_t n, char ch = '\0'){
if (n > _size){
//需要扩容则扩容
reserve(n);
//填入字符
for (size_t i = _size; i < n; ++i){
_str[i] = ch;
}
_size = n;
_str[_size] = '\0';
}
else{
_str[n] = '\0';
_size = n;
}
}
//push_back
void push_back(char ch){
if (_size == _capacity){
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
_str[_size++] = ch;
_str[_size] = '\0';
}
//append
void append(const char* s){
size_t len = strlen(s);
if (_size + len > _capacity){
reserve(_size + len);
}
strcpy(_str + _size, s);
_size += len;
}
//+=
string& operator+=(const char* s){
append(s);
return *this;
}
string& operator+=(char ch){
push_back(ch);
return *this;
}
//insert
string& insert(size_t pos, char ch){
assert(pos <= _size);
if (_size == _capacity){
size_t new_capacity = _capacity == 0 ? 4 : 2 * _capacity;
reserve(new_capacity);
}
for (size_t i = _size; i >= pos; --i){
_str[i + 1] = _str[i];
}
_str[pos] = ch;
_size++;
_str[_size] = '\0';
}
string& insert(size_t pos, const char* s){
assert(pos <= _size);
int len = strlen(s);
if (_size + len > _capacity){
reserve(_size + len);
}
int end = _size;
while (end >= pos){
_str[end + len] = _str[end];
end--;
}
strncpy(_str + pos, s, len);
_size += len;
return *this;
}
//erase
string& erase(size_t pos, size_t num = npos){
assert(pos <= _size);
if (num == npos || pos + num >= _size){
_str[pos] = '\0';
_size = pos;
}
else{
strcpy(_str + pos, _str + pos + num);
_size -= num;
}
return *this;
}
//find
size_t find(char ch, size_t pos = 0) const{
assert(pos < _size);
for (; pos < _size; ++pos){
if (_str[pos] == ch){
return pos;
}
}
return npos;
}
size_t find(const char* s, size_t pos = 0) const{
assert(pos < _size);
const char* ptr = strstr(_str + pos, s);
if (ptr == nullptr){
return npos;
}
return ptr - _str;
}
//clear()
void clear(){
_str[0] = '\0';
_size = 0;
}
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos = -1;
};
// 重载<< >>
/*ostream& operator<<(ostream& out, string& s){
for (size_t i = 0; i < s.size(); ++i)
{
out << s[i];
}
return out;
}*/
ostream& operator<<(ostream& out, string& s) {
printf("%s", s.c_str());
return out;
}
istream& operator>>(istream& in, string& s){
s.clear();
char tmp[128] = { '\0' };
size_t i = 0;
char ch = in.get();
while (ch != ' ' && ch != '\n'){
tmp[i] = ch;
if (i == 127)
{
s += tmp;
i = 0;
}
else {
i++;
}
ch = in.get();
}
if (i > 0){
tmp[i] = '\0';
s += tmp;
}
return in;
}
/*istream& operator>>(istream& in, string& s) {
s.clear();
char tmp[128] = {};
scanf("%s", tmp);
s += tmp;
return in;
}*/
}