string类(一)有哪些特点和用途?

2026-04-12 02:561阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

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

string类(一)有哪些特点和用途?

string类对象的常见构造 + string类实现了多个构造函数的重载,常用的构造函数如下:+ (constructor)函数名称:功能说明+ string():构造空的string类对象,即空字符串+ string(const char *s):用C-string来构造string类对象

string类对象的常见构造

string类实现了多个构造函数的重载,常用的构造函数如下:

(constructor)函数名称 功能说明 string() 构造空的string类对象,即空字符串 string(const char s)* 用 C-string 来构造 string 类对象 string(const char* s, size_t n) 复制s所指字符序列中的前n个字符 string(const string& s) 拷贝构造函数 string(size_t n, char c) 生成n个c字符的 string 类对象 string(const string& str, size_t pos, size_t len = npos) 复制str中从字符位置pos开始并跨越len个字符的部分

最后一个如果不给len,缺省值是npos,在定义中虽然npos = -1,但是npos是无符号数,所以是整数的最大值。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s1; string s2("hello world"); string s3("hello world", 5); string s4(s2);//拷贝构造s2 string s5(5, 'c');//生成5个c字符的string类对象 string s6(s2, 6, 5);//从下标为6的位置开始向后复制5个字符 //在string类中流插入和流提取已经重载过了,可以直接使用 cout << s1 << endl; //空串 cout << s2 << endl; //hello world cout << s3 << endl; //hello cout << s4 << endl; //hello world cout << s5 << endl; //ccccc cout << s6 << endl; //world return 0; }

我们还可以这样:

string s = "hello world"

因为会发生隐式类型转换,单参数的构造函数支持隐式类型转换,const char *转换成string,编译器优化为直接构造,如果不想进行隐式类型转换我们可以在构造函数前加关键字explicit

string类对象容量操作

函数名称 功能说明 size 返回字符串有效字符长度 length 返回字符串有效字符长度 capacity 返回空间总大小 empty 检测字符串是否为空串,是返回true,否则返回false clear 清空有效字符 reserve 为字符串预留空间 resize 将有效字符的个数改成n个,多出的空间用字符c填充 max_size 获取string对象的最大长度 shrink_to_fit(C++11) 缩容,将capacity缩小至和size一样大小

size/length函数

  • 使用size函数或者length函数获取当前有效字符的个数

size_t size() const; size_t length() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.length() << endl; // 11 }

size函数和length函数作用相同,因为string类产生的比较早,后来为了和其他的数据结构保持一致,所以增加了size函数。

max_size函数

  • 使用max_size函数获取string对象的最大长度

size_t max_size() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.max_size() << endl; // 2147483647 }

capacity函数

  • 使用capacity函数获取当前对象所分配的存储空间的大小

size_t capacity() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.capacity() << endl; // 15 }

VS下面的容量空间是不包含\0的。

string类(一)有哪些特点和用途?

empty函数

  • 使用empty函数判断字符串是否为空

bool empty() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.empty() << endl; // true }

clear函数

  • 使用clear函数清空有效字符,删除后对象变为空字符串

void clear();

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl;//15 s.clear(); cout << s.size() << endl; // 0 cout << s.capacity() << endl; //15 }

resize函数

  • 使用resize改变当前对象的有效字符的个数

void resize (size_t n); void resize (size_t n, char c);

resize规则:扩容并且初始化

1.当n大于对象当前的size时,将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’。 2.当n小于对象当前的size时,将size缩小到n。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl; //15 cout << s << endl; //hello world //n小于对象当前的size时,将size缩小到n s.resize(5); cout << s.size() << endl;//5 cout << s.capacity() << endl;//15 cout << s << endl; //hello //n大于对象当前的size时,将size扩大到n,扩大的字符为c s.resize(12, 'x'); cout << s.size() << endl;//12 cout << s.capacity() << endl;//15 cout << s << endl;//helloxxxxxxx //当n大于对象当前的size时,将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’ s.resize(20); cout << s.size() << endl;//20 cout << s.capacity() << endl;//31 cout << s << endl;//helloxxxxxxx }

注意:若给出的n大于对象当前的capacity,则capacity也会根据规则进行扩大。

reserve函数

  • 使用reserve改变当前对象的容量大小

void reserve (size_t n = 0);

reserve规则:  1、当n大于对象当前的capacity时,将capacity扩大到n或大于n。  2、当n小于对象当前的capacity时,capacity不变。

reserve函数对字符串的size没有影响,并且无法更改其内容。可以通过reserve函数提前开空间,减少扩容,提高效率。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl; //15 //n小于对象当前的capacity时,capacity不变 s.reserve(5); cout << s.size() << endl;//11 cout << s.capacity() << endl;//15 //n大于对象当前的capacity时,将capacity扩大到n或者大于n s.reserve(32); cout << s.size() << endl;//11 cout << s.capacity() << endl;//47 }

shrink_to_fit函数

  • shrink_to_fit是缩容,将capacity缩小至和size一样(C++11)

void shrink_to_fit();

注意:

  • size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()
  • clear()只是将string中有效字符清空,不改变底层空间大小
  • resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时用'\0'来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变容量大小,如果是将元素个数减少,容量大小不变
  • reserve(size_t n = 0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的容量大小时,reserver不会改变容量大小。
标签:常见构造

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

string类(一)有哪些特点和用途?

string类对象的常见构造 + string类实现了多个构造函数的重载,常用的构造函数如下:+ (constructor)函数名称:功能说明+ string():构造空的string类对象,即空字符串+ string(const char *s):用C-string来构造string类对象

string类对象的常见构造

string类实现了多个构造函数的重载,常用的构造函数如下:

(constructor)函数名称 功能说明 string() 构造空的string类对象,即空字符串 string(const char s)* 用 C-string 来构造 string 类对象 string(const char* s, size_t n) 复制s所指字符序列中的前n个字符 string(const string& s) 拷贝构造函数 string(size_t n, char c) 生成n个c字符的 string 类对象 string(const string& str, size_t pos, size_t len = npos) 复制str中从字符位置pos开始并跨越len个字符的部分

最后一个如果不给len,缺省值是npos,在定义中虽然npos = -1,但是npos是无符号数,所以是整数的最大值。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s1; string s2("hello world"); string s3("hello world", 5); string s4(s2);//拷贝构造s2 string s5(5, 'c');//生成5个c字符的string类对象 string s6(s2, 6, 5);//从下标为6的位置开始向后复制5个字符 //在string类中流插入和流提取已经重载过了,可以直接使用 cout << s1 << endl; //空串 cout << s2 << endl; //hello world cout << s3 << endl; //hello cout << s4 << endl; //hello world cout << s5 << endl; //ccccc cout << s6 << endl; //world return 0; }

我们还可以这样:

string s = "hello world"

因为会发生隐式类型转换,单参数的构造函数支持隐式类型转换,const char *转换成string,编译器优化为直接构造,如果不想进行隐式类型转换我们可以在构造函数前加关键字explicit

string类对象容量操作

函数名称 功能说明 size 返回字符串有效字符长度 length 返回字符串有效字符长度 capacity 返回空间总大小 empty 检测字符串是否为空串,是返回true,否则返回false clear 清空有效字符 reserve 为字符串预留空间 resize 将有效字符的个数改成n个,多出的空间用字符c填充 max_size 获取string对象的最大长度 shrink_to_fit(C++11) 缩容,将capacity缩小至和size一样大小

size/length函数

  • 使用size函数或者length函数获取当前有效字符的个数

size_t size() const; size_t length() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.length() << endl; // 11 }

size函数和length函数作用相同,因为string类产生的比较早,后来为了和其他的数据结构保持一致,所以增加了size函数。

max_size函数

  • 使用max_size函数获取string对象的最大长度

size_t max_size() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.max_size() << endl; // 2147483647 }

capacity函数

  • 使用capacity函数获取当前对象所分配的存储空间的大小

size_t capacity() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.capacity() << endl; // 15 }

VS下面的容量空间是不包含\0的。

string类(一)有哪些特点和用途?

empty函数

  • 使用empty函数判断字符串是否为空

bool empty() const;

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.empty() << endl; // true }

clear函数

  • 使用clear函数清空有效字符,删除后对象变为空字符串

void clear();

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl;//15 s.clear(); cout << s.size() << endl; // 0 cout << s.capacity() << endl; //15 }

resize函数

  • 使用resize改变当前对象的有效字符的个数

void resize (size_t n); void resize (size_t n, char c);

resize规则:扩容并且初始化

1.当n大于对象当前的size时,将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’。 2.当n小于对象当前的size时,将size缩小到n。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl; //15 cout << s << endl; //hello world //n小于对象当前的size时,将size缩小到n s.resize(5); cout << s.size() << endl;//5 cout << s.capacity() << endl;//15 cout << s << endl; //hello //n大于对象当前的size时,将size扩大到n,扩大的字符为c s.resize(12, 'x'); cout << s.size() << endl;//12 cout << s.capacity() << endl;//15 cout << s << endl;//helloxxxxxxx //当n大于对象当前的size时,将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’ s.resize(20); cout << s.size() << endl;//20 cout << s.capacity() << endl;//31 cout << s << endl;//helloxxxxxxx }

注意:若给出的n大于对象当前的capacity,则capacity也会根据规则进行扩大。

reserve函数

  • 使用reserve改变当前对象的容量大小

void reserve (size_t n = 0);

reserve规则:  1、当n大于对象当前的capacity时,将capacity扩大到n或大于n。  2、当n小于对象当前的capacity时,capacity不变。

reserve函数对字符串的size没有影响,并且无法更改其内容。可以通过reserve函数提前开空间,减少扩容,提高效率。

示例:

#include <string> #include <iostream> using namespace std; int main() { string s("hello world"); cout << s.size() << endl; // 11 cout << s.capacity() << endl; //15 //n小于对象当前的capacity时,capacity不变 s.reserve(5); cout << s.size() << endl;//11 cout << s.capacity() << endl;//15 //n大于对象当前的capacity时,将capacity扩大到n或者大于n s.reserve(32); cout << s.size() << endl;//11 cout << s.capacity() << endl;//47 }

shrink_to_fit函数

  • shrink_to_fit是缩容,将capacity缩小至和size一样(C++11)

void shrink_to_fit();

注意:

  • size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()
  • clear()只是将string中有效字符清空,不改变底层空间大小
  • resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时用'\0'来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变容量大小,如果是将元素个数减少,容量大小不变
  • reserve(size_t n = 0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的容量大小时,reserver不会改变容量大小。
标签:常见构造