std::string
7 methodsC++ 标准库字符串类,是 std::basic_string 针对 char 的特化版本。
HashMap::new() -> HashMap<K, V>返回字符串中的字符数量,不包括空终止符。
Returns
HashMap<K, V>
Example
rust
use std::collections::HashMap;
let mut map: HashMap<&str, i32> = HashMap::new();
map.insert("a", 1);
assert_eq!(map.get("a"), Some(&1));map.insert(k: K, v: V) -> Option<V>将给定的字符串追加到此字符串的末尾。
Parameters
| Name | Type | Description |
|---|---|---|
| k | K | 要追加的字符串。 |
| v | V | Value to associate with the key. |
Returns
Option<V>
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
assert_eq!(map.insert("a", 1), None);
assert_eq!(map.insert("a", 2), Some(1));map.get<Q: ?Sized>(&self, k: &Q) -> Option<&V>返回从 pos 开始、长度为 len 的子字符串(如果为 npos 则到末尾)。
Parameters
| Name | Type | Description |
|---|---|---|
| pos | &Q | 起始位置(默认为 0)。 |
Returns
Option<&V>
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
assert_eq!(map.get("a"), Some(&1));
assert_eq!(map.get("b"), None);map.remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>从 pos 开始搜索 s 的首次出现。未找到则返回 npos。
Parameters
| Name | Type | Description |
|---|---|---|
| k | &Q | 要搜索的子字符串。 |
Returns
Option<V>
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
assert_eq!(map.remove("a"), Some(1));
assert_eq!(map.remove("a"), None);map.len() -> usize返回一个指向以 null 结尾的 C 字符串表示的指针,该字符串包含此字符串的数据。
Returns
usize
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
assert_eq!(map.len(), 2);map.contains_key<Q: ?Sized>(&self, k: &Q) -> bool用字符串 s 替换从 pos 开始、长度为 len 的字符串部分。
Parameters
| Name | Type | Description |
|---|---|---|
| pos | &Q | 要替换部分的起始位置。 |
Returns
bool
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
assert!(map.contains_key("a"));
assert!(!map.contains_key("b"));map.iter() -> Iter<K, V>Returns an iterator visiting all key-value pairs in arbitrary order.
Returns
Iter<K, V>
Example
rust
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
for (k, v) in map.iter() {
println!("{}: {}", k, v);
}