Skip to content

C# <map> API

C# String —— 一个不可变的 UTF-16 码元序列,是 .NET 中的主要文本类型。

1 class · 6 methods

String

6 methods

将文本表示为 UTF-16 码元序列。String 是不可变的引用类型。

std::map::insert(const value_type& val) -> pair<iterator, bool>

获取当前 String 对象中的字符数量(UTF-16 码元)。

Parameters

NameTypeDescription
valconst 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 unchanged
std::map::at(const key_type& k) -> mapped_type&

从此实例中检索从 startIndex 开始、具有指定长度的子字符串。

Parameters

NameTypeDescription
startIndexconst 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_range
std::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();  // 2
std::map::erase(const key_type& k) -> size_type

返回一个新字符串,其中所有出现的 old 都被替换为 new。

Parameters

NameTypeDescription
oldconst 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"); // 0
std::map::find(const key_type& k) -> iterator

根据 separator 中的字符将字符串拆分为多个子字符串。

Parameters

NameTypeDescription
separatorconst 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";
}