C++ STL组件string如何改写为长尾?

2026-04-12 00:431阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计3095个文字,预计阅读时间需要13分钟。

C++ STL组件string如何改写为长尾?

一、string基本概念

1.本质:string是C++风格的字符串类,本质上是一个类。

2.特点:封装了char*,管理字符串。

二、string与char*的区别

1.char*:是一个指针,指向一个字符数组。

2.string:是一个类,内部封装了char*,提供了一系列操作字符串的方法。

一、string基本概念

1、本质

string是C++风格的字符串,而string本质上是一个类

2、string和char * 区别:

1、char * 是一个指针

2、string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。

3、特点:

string 类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

二、string构造函数

1、构造函数原型

1、string();

//创建一个空的字符串 例如: string str;

2、string(const char* s);

//使用字符串s初始化

3、string(const string& str);

//使用一个string对象初始化另一个string对象

4、string(int n, char c);

//使用n个字符c初始化

2、示例

void test01() { string s; const char* str = "halouwa"; string s1(str); cout << "s1="<<s1 << endl; string s2(s1); cout << "s2=" << s2 << endl; string s3(10, 'a'); cout << "s3=" << s3<< endl; } int main() { test01(); system("pause"); return 0; }

三、string赋值操作

功能描述:给string字符串进行赋值


1、赋值的函数原型

1、string& operator=(const char* s);

//char*类型字符串 赋值给当前的字符串

2、string& operator=(const string &s);

//把字符串s赋给当前的字符串

3、string& operator=(char c);

//字符赋值给当前的字符串

4、string& assign(const char *s);

//把字符串s赋给当前的字符串

5、string& assign(const char *s, int n);

//把字符串s的前n个字符赋给当前的字符串

6、string& assign(const string &s);

//把字符串s赋给当前字符串

7、string& assign(int n, char c);

//用n个字符c赋给当前字符串

void test01() { string s1; const char* s = "hello s1"; s1 = s; cout << "s1 = " << s1 << endl; string s2; s2 = s1; cout << "s2 = " << s2 << endl; string s3; char c = 'c'; s3 = c; cout << "s3 = " << s3 << endl; string s4; s4.assign("hello s4"); cout << "s4 = " << s4 << endl; string s5; s5.assign("hello s4",5); cout << "s5 = " << s5 << endl; string s6; s6.assign(s5); cout << "s6 = " << s6 << endl; string s7; s7.assign(10,'w'); cout << "s7 = " << s7 << endl; } int main() { test01(); system("pause"); return 0; }


总结:

string的赋值方式有很多,operator= 这种方式最常用

四、string字符串拼接

功能描述:

实现在字符串末尾拼接字符串


1、函数原型

1、string& operator+=(const char* str);

//重载+=操作符

2、string& operator+=(const char c);

//重载+=操作符

3、string& operator+=(const string& str);

//重载+=操作符

4、string& append(const char *s);

//把字符串s连接到当前字符串结尾

5、string& append(const char *s, int n);

//把字符串s的前n个字符连接到当前字符串结尾

6、string& append(const string &s);

//同operator+=(const string& str)

7、string& append(const string &s, int pos, int n);

//字符串s中从pos开始的n个字符连接到字符串结尾

//字符串拼接 void test01() { string s1 = "我"; s1 += "爱学习"; cout << "s1 = " << s1 << endl; s1 += ':'; cout << "s1 = " << s1 << endl; string s = "c++"; s1 += s; cout << "s1 = " << s1 << endl; string s2; s2.append("I "); cout << "s2 = " << s2 << endl; s2.append("like you",5); cout << "s2 = " << s2 << endl; string s3 = "study "; s2.append(s3); cout << "s2 = " << s2 << endl; string s4 = "python c++"; s2.append(s4,7,3); cout << "s2 = " << s2 << endl; } int main() { test01(); system("pause"); }

五、string查找和替换

1、功能描述

查找:查找指定字符串是否存在

替换:在指定的位置替换字符串


2、函数原型

1、int find(const string& str, int pos = 0) const;

//查找str第一次出现位置,从pos开始查找

2、int find(const char* s, int pos = 0) const;

//查找s第一次出现位置,从pos开始查找

C++ STL组件string如何改写为长尾?

3、int find(const char* s, int pos, int n) const;

//从pos位置查找s的前n个字符第一次位置

4、int find(const char c, int pos = 0) const;

//查找字符c第一次出现位置

5、int rfind(const string& str, int pos = npos) const;

//查找str最后一次位置,从pos开始查找

6、int rfind(const char* s, int pos = npos) const;

//查找s最后一次出现位置,从pos开始查找

7、int rfind(const char* s, int pos, int n) const;

//从pos查找s的前n个字符最后一次位置

8、int rfind(const char c, int pos = 0) const;

//查找字符c最后一次出现位置

9、string& replace(int pos, int n, const string& str);

//替换从pos开始n个字符为字符串str

10、string& replace(int pos, int n,const char* s);

//替换从pos开始的n个字符为字符串s

3、示例

//字符串的查找和替换 //1、查找 void test01() { string str = "asdfggghhhjj"; //找到,返回索引,未找到,返回-1 int pos = str.find("gg"); cout << pos << endl; //find,从左往右找,rfind,从右往左找 pos = str.rfind("gg"); cout << pos << endl; } //2、替换 void test02() { string str1 = "qwertyuiop"; //将第2个字符开始的后0个字符,替换成”1111“ str1.replace(2,0,"1111"); cout << str1 << endl; } int main() { test01(); test02(); }

总结:

1、find查找是从左往右,rfind从右往左

2、find找到,返回查找的第一个字符位置,未找到返回-1

3、replace在替换时,需要指定从那个位置起,多少个字符,替换成什么样的字符串

六、string字符串比较

字符串之间的比较


2、比较方式

字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1


3、函数原型

1、int compare(const string &s) const;

//与字符串s比较

2、int compare(const char *s) const;

//与字符串s比较

4、示例

//字符串比较 void test01() { string str1 = "hello"; string str2 = "hello"; if (str1.compare(str2) == 0) { cout << "相等" << endl; } else if (str1.compare(str2) == 1) { cout << "str1 > str2" << endl; } else if (str1.compare(str2) == -1) { cout << "str1 > str2" << endl; } else cout << "错误" << endl; } int main() { test01(); }

总结:

字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

七、string字符存取

1、存取方式

string中单个字符存取方式有两种

1、char& operator[](int n); //通过[]方式取字符

2、char& at(int n); //通过at方法获取字符


//字符串单个字符的存取 void test1() { string str = "hello"; cout << "str = " << str << endl; //访问 for (int i = 0; str[i] != '\0';i++) { cout<<str[i] << " "; } cout << endl; for (int i = 0; i<str.size();i++) { cout << str.at(i) << " "; } cout << endl; //修改 str[0] = 'x'; cout << "str = " << str << endl; str.at(1) = 'x'; cout << "str = " << str << endl; } int main() { test1(); system("pause"); return 0; }

八、string插入和删除

功能描述:

对string字符串进行插入和删除字符操作


1、string& insert(int pos, const char* s);

//插入字符串

2、string& insert(int pos, const string& str);

//插入字符串

3、string& insert(int pos, int n, char c);

//在指定位置插入n个字符c

4、string& erase(int pos, int n = npos);

//删除从Pos开始的n个字符


//字符串的插入和删除 void test1() { string str = "algorithm"; cout << "str = " << str << endl; //插入 str.insert(1,"xxx"); cout << "str = " << str << endl; //删除 str.erase(1,3); cout << "str = " << str << endl; } int main() { test1(); system("pause"); }

九、string子串

功能描述:

从字符串中获取想要的子串


1、string substr(int pos = 0, int n = npos) const;

//返回由pos开始的n个字符组成的字符串

void test1() { string str = "lisi@qq.com"; cout << "str =" << str << endl; string sub = str.substr(0,4); cout << "substr =" << sub << endl; } int main() { test1(); system("pause"); }

本文共计3095个文字,预计阅读时间需要13分钟。

C++ STL组件string如何改写为长尾?

一、string基本概念

1.本质:string是C++风格的字符串类,本质上是一个类。

2.特点:封装了char*,管理字符串。

二、string与char*的区别

1.char*:是一个指针,指向一个字符数组。

2.string:是一个类,内部封装了char*,提供了一系列操作字符串的方法。

一、string基本概念

1、本质

string是C++风格的字符串,而string本质上是一个类

2、string和char * 区别:

1、char * 是一个指针

2、string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。

3、特点:

string 类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

二、string构造函数

1、构造函数原型

1、string();

//创建一个空的字符串 例如: string str;

2、string(const char* s);

//使用字符串s初始化

3、string(const string& str);

//使用一个string对象初始化另一个string对象

4、string(int n, char c);

//使用n个字符c初始化

2、示例

void test01() { string s; const char* str = "halouwa"; string s1(str); cout << "s1="<<s1 << endl; string s2(s1); cout << "s2=" << s2 << endl; string s3(10, 'a'); cout << "s3=" << s3<< endl; } int main() { test01(); system("pause"); return 0; }

三、string赋值操作

功能描述:给string字符串进行赋值


1、赋值的函数原型

1、string& operator=(const char* s);

//char*类型字符串 赋值给当前的字符串

2、string& operator=(const string &s);

//把字符串s赋给当前的字符串

3、string& operator=(char c);

//字符赋值给当前的字符串

4、string& assign(const char *s);

//把字符串s赋给当前的字符串

5、string& assign(const char *s, int n);

//把字符串s的前n个字符赋给当前的字符串

6、string& assign(const string &s);

//把字符串s赋给当前字符串

7、string& assign(int n, char c);

//用n个字符c赋给当前字符串

void test01() { string s1; const char* s = "hello s1"; s1 = s; cout << "s1 = " << s1 << endl; string s2; s2 = s1; cout << "s2 = " << s2 << endl; string s3; char c = 'c'; s3 = c; cout << "s3 = " << s3 << endl; string s4; s4.assign("hello s4"); cout << "s4 = " << s4 << endl; string s5; s5.assign("hello s4",5); cout << "s5 = " << s5 << endl; string s6; s6.assign(s5); cout << "s6 = " << s6 << endl; string s7; s7.assign(10,'w'); cout << "s7 = " << s7 << endl; } int main() { test01(); system("pause"); return 0; }


总结:

string的赋值方式有很多,operator= 这种方式最常用

四、string字符串拼接

功能描述:

实现在字符串末尾拼接字符串


1、函数原型

1、string& operator+=(const char* str);

//重载+=操作符

2、string& operator+=(const char c);

//重载+=操作符

3、string& operator+=(const string& str);

//重载+=操作符

4、string& append(const char *s);

//把字符串s连接到当前字符串结尾

5、string& append(const char *s, int n);

//把字符串s的前n个字符连接到当前字符串结尾

6、string& append(const string &s);

//同operator+=(const string& str)

7、string& append(const string &s, int pos, int n);

//字符串s中从pos开始的n个字符连接到字符串结尾

//字符串拼接 void test01() { string s1 = "我"; s1 += "爱学习"; cout << "s1 = " << s1 << endl; s1 += ':'; cout << "s1 = " << s1 << endl; string s = "c++"; s1 += s; cout << "s1 = " << s1 << endl; string s2; s2.append("I "); cout << "s2 = " << s2 << endl; s2.append("like you",5); cout << "s2 = " << s2 << endl; string s3 = "study "; s2.append(s3); cout << "s2 = " << s2 << endl; string s4 = "python c++"; s2.append(s4,7,3); cout << "s2 = " << s2 << endl; } int main() { test01(); system("pause"); }

五、string查找和替换

1、功能描述

查找:查找指定字符串是否存在

替换:在指定的位置替换字符串


2、函数原型

1、int find(const string& str, int pos = 0) const;

//查找str第一次出现位置,从pos开始查找

2、int find(const char* s, int pos = 0) const;

//查找s第一次出现位置,从pos开始查找

C++ STL组件string如何改写为长尾?

3、int find(const char* s, int pos, int n) const;

//从pos位置查找s的前n个字符第一次位置

4、int find(const char c, int pos = 0) const;

//查找字符c第一次出现位置

5、int rfind(const string& str, int pos = npos) const;

//查找str最后一次位置,从pos开始查找

6、int rfind(const char* s, int pos = npos) const;

//查找s最后一次出现位置,从pos开始查找

7、int rfind(const char* s, int pos, int n) const;

//从pos查找s的前n个字符最后一次位置

8、int rfind(const char c, int pos = 0) const;

//查找字符c最后一次出现位置

9、string& replace(int pos, int n, const string& str);

//替换从pos开始n个字符为字符串str

10、string& replace(int pos, int n,const char* s);

//替换从pos开始的n个字符为字符串s

3、示例

//字符串的查找和替换 //1、查找 void test01() { string str = "asdfggghhhjj"; //找到,返回索引,未找到,返回-1 int pos = str.find("gg"); cout << pos << endl; //find,从左往右找,rfind,从右往左找 pos = str.rfind("gg"); cout << pos << endl; } //2、替换 void test02() { string str1 = "qwertyuiop"; //将第2个字符开始的后0个字符,替换成”1111“ str1.replace(2,0,"1111"); cout << str1 << endl; } int main() { test01(); test02(); }

总结:

1、find查找是从左往右,rfind从右往左

2、find找到,返回查找的第一个字符位置,未找到返回-1

3、replace在替换时,需要指定从那个位置起,多少个字符,替换成什么样的字符串

六、string字符串比较

字符串之间的比较


2、比较方式

字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1


3、函数原型

1、int compare(const string &s) const;

//与字符串s比较

2、int compare(const char *s) const;

//与字符串s比较

4、示例

//字符串比较 void test01() { string str1 = "hello"; string str2 = "hello"; if (str1.compare(str2) == 0) { cout << "相等" << endl; } else if (str1.compare(str2) == 1) { cout << "str1 > str2" << endl; } else if (str1.compare(str2) == -1) { cout << "str1 > str2" << endl; } else cout << "错误" << endl; } int main() { test01(); }

总结:

字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

七、string字符存取

1、存取方式

string中单个字符存取方式有两种

1、char& operator[](int n); //通过[]方式取字符

2、char& at(int n); //通过at方法获取字符


//字符串单个字符的存取 void test1() { string str = "hello"; cout << "str = " << str << endl; //访问 for (int i = 0; str[i] != '\0';i++) { cout<<str[i] << " "; } cout << endl; for (int i = 0; i<str.size();i++) { cout << str.at(i) << " "; } cout << endl; //修改 str[0] = 'x'; cout << "str = " << str << endl; str.at(1) = 'x'; cout << "str = " << str << endl; } int main() { test1(); system("pause"); return 0; }

八、string插入和删除

功能描述:

对string字符串进行插入和删除字符操作


1、string& insert(int pos, const char* s);

//插入字符串

2、string& insert(int pos, const string& str);

//插入字符串

3、string& insert(int pos, int n, char c);

//在指定位置插入n个字符c

4、string& erase(int pos, int n = npos);

//删除从Pos开始的n个字符


//字符串的插入和删除 void test1() { string str = "algorithm"; cout << "str = " << str << endl; //插入 str.insert(1,"xxx"); cout << "str = " << str << endl; //删除 str.erase(1,3); cout << "str = " << str << endl; } int main() { test1(); system("pause"); }

九、string子串

功能描述:

从字符串中获取想要的子串


1、string substr(int pos = 0, int n = npos) const;

//返回由pos开始的n个字符组成的字符串

void test1() { string str = "lisi@qq.com"; cout << "str =" << str << endl; string sub = str.substr(0,4); cout << "substr =" << sub << endl; } int main() { test1(); system("pause"); }