“cpp/string/basic string/clear”的版本间的差异

来自cppreference.com
< cpp‎ | string‎ | basic string
(1个修订: Translate from the English version)
 
(未显示3个用户的10个中间版本)
第1行: 第1行:
{{tr_note}}
+
{{cpp/string/basic_string/title|clear}}
{{cpp/string/basic_string/title | clear}}
+
 
{{cpp/string/basic_string/navbar}}
 
{{cpp/string/basic_string/navbar}}
{{ddcl |
+
{{|
 
void clear();
 
void clear();
 
}}
 
}}
  
Removes all characters from the string. The allocated memory will not be released, effectively leaving the {{rlpt|capacity}} of the string unchanged. The past-the-end iterators are not invalidated.
+
{{|}}
 +
 
 +
  
 
===参数===
 
===参数===
{{tr| (无)|(none)}}
+
(无)
  
 
===返回值===
 
===返回值===
{{tr| (无)|(none)}}
+
(无)
  
===复杂性===
+
linear in the size of the string.
+
 +
 
 +
===复杂
 +
 +
 
 +
===
 +
 +
 +
 +
 +
string
 +
 
 +
 +
 +
 +
.
 +
 
 +
 +
 +
 +
 +
 +
 +
 +
 +
  
=== 另请 参阅===
+
===参阅===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/string/basic_string/dcl list erase}}
+
{{|cpp/string/basic_string/erase}}
{{dcl list end}}
+
{{end}}
  
[[ja:cpp/string/basic string/clear]]
+
japlptru
[[pl:cpp/string/basic string/clear]]
+
[[pt:cpp/string/basic string/clear]]
+
[[ru:cpp/string/basic string/clear]]
+

2024年10月21日 (一) 04:00的最后版本

 
 
 
std::basic_string
 
void clear();
(C++11 起为 noexcept)
(C++20 起为 constexpr)

如同通过执行 erase(begin(), end()) 从字符串中移除所有字符。

使所有指针、引用及迭代器失效。

目录

[编辑] 参数

(无)

[编辑] 返回值

(无)

[编辑] 注解

不同于 std::vector::clear,C++ 标准未明确要求此函数不更改 capacity,但既存实现都不更改容量。这意味着它们不释放分配的内存(参阅 shrink_to_fit)。

[编辑] 复杂度

与字符串的大小成线性,尽管既存实现在常数时间内操作。

[编辑] 示例

#include <cassert>
#include <iostream>
#include <string>
 
int main()
{
    std::string s{ "Exemplar" };
    std::string::size_type const capacity = s.capacity();
 
    s.clear();
    assert(s.empty());
    assert(s.size() == 0);
    std::cout << std::boolalpha << (s.capacity() == capacity) << '\n';
}

可能的输出:

true

[编辑] 参阅

移除字符
(公开成员函数) [编辑]