String
6 methods将文本表示为 UTF-16 码元序列。String 是不可变的引用类型。
std::map::insert(const value_type& val) -> pair<iterator, bool>获取当前 String 对象中的字符数量(UTF-16 码元)。
Parameters
| Name | Type | Description |
|---|---|---|
| val | const value_type& | Key-value pair to insert. |
Returns
pair<iterator, bool>
Example
cpp
#include <map>
std::map<std::string, int> m;
auto [it, ok] = m.insert({"a", 1});
// ok == true, it points to {"a", 1}
auto [it2, ok2] = m.insert({"a", 2});
// ok2 == false, existing value unchangedstd::map::at(const key_type& k) -> mapped_type&从此实例中检索从 startIndex 开始、具有指定长度的子字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| startIndex | const key_type& | 从零开始的起始字符位置。 |
Returns
mapped_type&
Example
cpp
std::map<std::string, int> m = {{"a", 1}};
int v = m.at("a"); // 1
// m.at("b"); // throws std::out_of_rangestd::map::size() const noexcept -> size_t报告 value 首次出现的从零开始的索引,未找到则返回 -1。
Returns
size_t
Example
cpp
std::map<std::string, int> m = {{"a", 1}, {"b", 2}};
size_t n = m.size(); // 2std::map::erase(const key_type& k) -> size_type返回一个新字符串,其中所有出现的 old 都被替换为 new。
Parameters
| Name | Type | Description |
|---|---|---|
| old | const key_type& | 要被替换的字符串。 |
Returns
size_type
Example
cpp
std::map<std::string, int> m = {{"a", 1}, {"b", 2}};
size_t removed = m.erase("a"); // 1
size_t removed2 = m.erase("z"); // 0std::map::find(const key_type& k) -> iterator根据 separator 中的字符将字符串拆分为多个子字符串。
Parameters
| Name | Type | Description |
|---|---|---|
| separator | const key_type& | 分隔子字符串的字符数组。 |
Returns
iterator
Example
cpp
std::map<std::string, int> m = {{"a", 1}};
auto it = m.find("a");
if (it != m.end()) {
std::cout << it->second; // 1
}std::map::empty() const noexcept -> bool返回此字符串的副本,使用当前区域性的大小写规则转换为大写。
Returns
bool
Example
cpp
std::map<std::string, int> m;
if (m.empty()) {
std::cout << "empty\n";
}