带你玩转std::string类

带你玩转std::string类,第1张

名称描述
size返回字符串长度
length返回字符串长度
max_size返回字符串的最大大小
resize调整字符串大小
capacity返回分配存储的大小
reserve请求更改容量
clear清除字符串
empty判断字符串是否为空
shrink_to_fit缩小以适应
size

公共成员函数

std::string::size

c++98

size_t size() const;

c++11

size_t size() const noexcept;

返回字符串长度
返回字符串的长度,以字节为单位。
这是符合字符串内容的实际字节数,不一定等于其存储容量。
注意细绳对象在不知道最终可能用于对其包含的字符进行编码的编码的情况下处理字节。因此,返回的值可能与多字节或可变长度字符(如 UTF-8)序列中的实际编码字符数不对应。string::size和string::length都是同义词并且返回相同的值。

参数

返回值

字符串中的字节数。
size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

例子
// string::size
#include 
#include 

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.size() << " bytes.\n";
  return 0;
}

输出:

The size of str is 11 bytes
迭代器有效性

没有变化。

数据竞赛

对象被访问。

异常安全

不抛出保证:这个成员函数从不抛出异常。

length

公共成员函数

std::string::length

c++98

size_t length() const;

c++11

size_t length() const noexcept;

返回字符串长度
返回字符串的长度,以字节为单位。
这是符合字符串内容的实际字节数,不一定等于其存储容量。
注意细绳对象在不知道最终可能用于对其包含的字符进行编码的编码的情况下处理字节。因此,返回的值可能与多字节或可变长度字符(如 UTF-8)序列中的实际编码字符数不对应。string::size和string::length都是同义词并且返回相同的值。

参数

返回值

字符串中的字节数。
size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

例子
// string::length
#include 
#include 

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}

输出:

The size of str is 11 bytes
迭代器有效性

没有变化。

数据竞赛

对象被访问。

异常安全

不抛出保证:这个成员函数从不抛出异常。

max_size

公共成员函数

std::string::max_size

c++98

size_t max_size() const;

c++11

size_t max_size() const noexcept;

返回字符串的最大大小
返回字符串可以达到 的最大长度。
由于已知的系统或库实现限制,这是字符串可以达到的最大潜在长度,但不能保证对象能够达到该长度:在达到该长度之前的任何时候,它仍然可能无法分配存储。

参数

返回值

字符串可以达到 的最大长度。
size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

例子
// comparing size, length, capacity and max_size
#include 
#include 

int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "length: " << str.length() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  std::cout << "max_size: " << str.max_size() << "\n";
  return 0;
}

输出:

size: 11
length: 11
capacity: 15
max_size: 4294967291
迭代器有效性

没有变化。

数据竞赛

对象被访问。

异常安全

不抛出保证:这个成员函数从不抛出异常。

resize

公共成员函数

std::string::resize
void resize (size_t n);
void resize (size_t n, char c);

调整字符串大小
将字符串大小调整为n 个字符的长度。 如果n小于当前字符串长度,则将当前值缩短为其第一个n字符,删除第n个之后的字符。 如果n大于当前字符串长度,则通过在末尾插入所需数量的字符来扩展当前内容,以达到n的大小。如果指定c,则新元素将初始化为c的副本,否则,它们是值初始化字符(空字符)。

参数

n
 新字符串长度,以字符数表示。
 size_t 是无符号整数类型(与成员类型 string::size_type 相同)。
c
 用于填充添加到字符串的新字符空间的字符(以防字符串被扩展)。

返回值

例子
// resizing string
#include 
#include 

int main ()
{
  std::string str ("I like to code in C");
  std::cout << str << '\n';

  unsigned sz = str.size();

  str.resize (sz+2,'+');
  std::cout << str << '\n';

  str.resize (14);
  std::cout << str << '\n';
  return 0;
}

输出:

I like to code in C
I like to code in C++
I like to code
迭代器有效性

没与此对象相关的任何迭代器、指针和引用都可能无效。

数据竞赛

对象被修改。

异常安全

不抛出保证:这个成员函数从不抛出异常。

capacity

公共成员函数

std::string::capacity

c++98

size_t capacity() const;

c++11

size_t capacity() const noexcept;

返回已分配存储的大小
返回当前为string分配的存储空间的大小,以字节表示。
这个容量不一定等于字符串长度。它可以等于或更大,额外的空间允许对象在将新字符添加到字符串时优化其 *** 作。
请注意,此容量不假设对字符串长度的限制。当这个容量用完并且需要更多时,它会由对象自动扩展(重新分配它的存储空间)。字符串长度的理论限制由成员max_size给出。字符串的容量可以在对象被修改的任何时候改变,即使这个修改意味着大小的减少或者容量没有被耗尽(这与向量容器中的容量保证相反)。字符串 的容量可以通过调用成员 reserve 来显式更改。

参数

返回值

当前为字符串分配的存储容量大小。
size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

例子
// comparing size, length, capacity and max_size
#include 
#include 

int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "length: " << str.length() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  std::cout << "max_size: " << str.max_size() << "\n";
  return 0;
}

输出:

size: 11
length: 11
capacity: 15
max_size: 429496729
迭代器有效性

没有变化。

数据竞赛

对象被访问。

异常安全

不抛出保证:这个成员函数从不抛出异常。

reserve

公共成员函数

std::string::reserve
void reserve (size_t n = 0);

请求更改容量
请求字符串容量适应计划的大小更改,最多为n个字符。 如果n大于当前字符串容量,则该函数使容器将其容量增加到n 个字符(或更大)。在所有其他情况下,缩小字符串容量被 视为非绑定请求:容器实现可以自由优化,否则字符串容量大于n。 此功能对字符串长度,并且不能更改其内容。

参数

n
字符串的计划长度。 请注意,生成的字符串容量可能等于或大于n。
size_t 是无符号整数类型(与成员类型 string::size_type 相同)。

返回值

例子
// string::reserve
#include 
#include 
#include 

int main ()
{
  std::string str;

  std::ifstream file ("test.txt",std::ios::in|std::ios::ate);
  if (file) {
    std::ifstream::streampos filesize = file.tellg();
    str.reserve(filesize);

    file.seekg(0);
    while (!file.eof())
    {
      str += file.get();
    }
    std::cout << str;
  }
  return 0;
}

此示例在字符串对象中保留了足够的容量来存储整个文件,然后逐个字符地读取该文件。通过为字符串保留至少整个文件大小的容量,我们试图避免每次插入新字符使其长度超过其容量时对象str可能遭受的所有自动重新分配。

迭代器有效性

此对象相关的任何迭代器、指针和引用都可能无效。

数据竞赛

对象被修改。

异常安全

强保证:如果抛出异常,字符串没有变化。
如果n大于max_size,则会引发length_error异常。
如果函数需要分配存储并且失败,则会引发bad_alloc异常。

clear

公共成员函数

std::string::clear

c++98

void clear();

c++11

void clear() noexcept;

清除字符串
擦除string的内容,变为空字符串(长度为0个字符)。

参数

返回值

例子
// string::clear
#include 
#include 

int main ()
{
  char c;
  std::string str;
  std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
  do {
    c = std::cin.get();
    str += c;
    if (c=='\n')
    {
       std::cout << str;
       str.clear();
    }
  } while (c!='.');
  return 0;
}

该程序重复用户引入的每一行,直到一行包含一个点(’.’)。每个换行符 ( ‘\n’ ) 都会触发行的重复和当前字符串内容的清除。

迭代器有效性

与此对象相关的任何迭代器、指针和引用都可能无效。

数据竞赛

对象被修改。

异常安全

不抛出保证:这个成员函数从不抛出异常。

empty

公共成员函数

std::string::empty

c++98

bool empty() const;

c++11

bool empty() const noexcept;

判断字符串是否为空
返回字符串是否为空(即其长度是否为0)。
此函数不会以任何方式修改字符串的值。要清除string的内容,请参阅string::clear。

参数

返回值

如果字符串长度为0则为true,否则为false。

例子
// string::empty
#include 
#include 

int main ()
{
  std::string content;
  std::string line;
  std::cout << "Please introduce a text. Enter an empty line to finish:\n";
  do {
    getline(std::cin,line);
    content += line + '\n';
  } while (!line.empty());
  std::cout << "The text you introduced was:\n" << content;
  return 0;
}

该程序逐行读取用户输入并将其存储到字符串内容中,直到引入一个空行。

迭代器有效性

没有变化。

数据竞赛

对象被访问。

异常安全

不抛出保证:这个成员函数从不抛出异常。

shrink_to_fit

公共成员函数

std::string::shrink_to_fit

c++11

void shrink_to_fit();

缩小以适合
请求字符串减小其容量以适应其大小。
该请求是非绑定的,容器实现可以自由优化,否则字符串的容量大于其size。
此函数对字符串长度没有影响,并且不能更改其内容。

参数

返回值

例子
// string::shrink_to_fit
#include 
#include 

int main ()
{
  std::string str (100,'x');
  std::cout << "1. capacity of str: " << str.capacity() << '\n';

  str.resize(10);
  std::cout << "2. capacity of str: " << str.capacity() << '\n';

  str.shrink_to_fit();
  std::cout << "3. capacity of str: " << str.capacity() << '\n';

  return 0;
}

输出:

1. capacity of str: 100
2. capacity of str: 100
3. capacity of str: 10
迭代器有效性

与此对象相关的任何迭代器、指针和引用都可能无效。

数据竞赛

对象被修改。

异常安全

强保证:如果抛出异常,字符串没有变化。
如果函数需要分配存储并且失败,则会引发bad_alloc异常。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/707123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-24
下一篇 2022-04-24

发表评论

登录后才能评论

评论列表(0条)

保存