Python容器轉(zhuǎn)換與共有函數(shù)舉例詳解
Python容器轉(zhuǎn)換與共有函數(shù)詳解
一、容器類型概覽
Python主要的內(nèi)置容器類型:
| 容器類型 | 描述 | 可變性 | 是否有序 | 示例 |
|---|---|---|---|---|
| 列表(list) | 有序的元素集合 | 可變 | 有序 | [1, 2, 3] |
| 元組(tuple) | 不可變的元素集合 | 不可變 | 有序 | (1, 2, 3) |
| 集合(set) | 無序的唯一元素集合 | 可變 | 無序 | {1, 2, 3} |
| 凍結(jié)集合(frozenset) | 不可變的集合 | 不可變 | 無序 | frozenset([1, 2, 3]) |
| 字典(dict) | 鍵值對映射 | 可變 | Python 3.7+有序 | {'a': 1, 'b': 2} |
| 字符串(str) | 字符序列 | 不可變 | 有序 | "hello" |
| 字節(jié)(bytes) | 字節(jié)序列 | 不可變 | 有序 | b"hello" |
| 字節(jié)數(shù)組(bytearray) | 可變的字節(jié)序列 | 可變 | 有序 | bytearray(b"hello") |
二、容器類型轉(zhuǎn)換
1. 基本容器轉(zhuǎn)換
# ==================== 基本容器轉(zhuǎn)換 ====================
# 1. 列表與其他容器的轉(zhuǎn)換
original_list = [1, 2, 3, 2, 1] # 包含重復(fù)元素
# 列表 -> 元組
list_to_tuple = tuple(original_list)
print(f"列表轉(zhuǎn)元組: {original_list} -> {list_to_tuple}")
print(f"類型: {type(list_to_tuple)}")
# 列表 -> 集合(自動去重)
list_to_set = set(original_list)
print(f"列表轉(zhuǎn)集合: {original_list} -> {list_to_set}")
print(f"類型: {type(list_to_set)}")
# 列表 -> 凍結(jié)集合
list_to_frozenset = frozenset(original_list)
print(f"列表轉(zhuǎn)凍結(jié)集合: {original_list} -> {list_to_frozenset}")
print(f"類型: {type(list_to_frozenset)}")
print("\n" + "="*50 + "\n")
# 2. 元組與其他容器的轉(zhuǎn)換
original_tuple = (1, 2, 3, 2, 1)
# 元組 -> 列表
tuple_to_list = list(original_tuple)
print(f"元組轉(zhuǎn)列表: {original_tuple} -> {tuple_to_list}")
# 元組 -> 集合
tuple_to_set = set(original_tuple)
print(f"元組轉(zhuǎn)集合: {original_tuple} -> {tuple_to_set}")
# 元組 -> 凍結(jié)集合
tuple_to_frozenset = frozenset(original_tuple)
print(f"元組轉(zhuǎn)凍結(jié)集合: {original_tuple} -> {tuple_to_frozenset}")
print("\n" + "="*50 + "\n")
# 3. 集合與其他容器的轉(zhuǎn)換
original_set = {1, 2, 3, 4, 5}
# 集合 -> 列表(無序,可能順序不同)
set_to_list = list(original_set)
print(f"集合轉(zhuǎn)列表: {original_set} -> {set_to_list}")
# 集合 -> 元組
set_to_tuple = tuple(original_set)
print(f"集合轉(zhuǎn)元組: {original_set} -> {set_to_tuple}")
# 集合 -> 凍結(jié)集合
set_to_frozenset = frozenset(original_set)
print(f"集合轉(zhuǎn)凍結(jié)集合: {original_set} -> {set_to_frozenset}")
print("\n" + "="*50 + "\n")
# 4. 字符串與容器的轉(zhuǎn)換
text = "hello world"
# 字符串 -> 列表(字符列表)
str_to_list = list(text)
print(f"字符串轉(zhuǎn)列表: '{text}' -> {str_to_list}")
# 字符串 -> 元組
str_to_tuple = tuple(text)
print(f"字符串轉(zhuǎn)元組: '{text}' -> {str_to_tuple}")
# 字符串 -> 集合(唯一字符)
str_to_set = set(text)
print(f"字符串轉(zhuǎn)集合: '{text}' -> {str_to_set}")
# 列表 -> 字符串(join方法)
list_to_str = ''.join(['h', 'e', 'l', 'l', 'o'])
print(f"字符列表轉(zhuǎn)字符串: {['h', 'e', 'l', 'l', 'o']} -> '{list_to_str}'")
# 單詞列表轉(zhuǎn)字符串
words = ['hello', 'world', 'python']
words_to_str = ' '.join(words)
print(f"單詞列表轉(zhuǎn)字符串: {words} -> '{words_to_str}'")
print("\n" + "="*50 + "\n")
# 5. 字典與其他容器的轉(zhuǎn)換
original_dict = {'a': 1, 'b': 2, 'c': 3}
# 字典 -> 列表(只保留鍵)
dict_keys_to_list = list(original_dict)
print(f"字典轉(zhuǎn)列表(鍵): {original_dict} -> {dict_keys_to_list}")
# 字典 -> 列表(鍵列表)
dict_keys_list = list(original_dict.keys())
print(f"字典鍵轉(zhuǎn)列表: {original_dict} -> {dict_keys_list}")
# 字典 -> 列表(值列表)
dict_values_list = list(original_dict.values())
print(f"字典值轉(zhuǎn)列表: {original_dict} -> {dict_values_list}")
# 字典 -> 列表(鍵值對列表)
dict_items_list = list(original_dict.items())
print(f"字典項轉(zhuǎn)列表: {original_dict} -> {dict_items_list}")
# 字典 -> 元組(鍵的元組)
dict_keys_tuple = tuple(original_dict)
print(f"字典鍵轉(zhuǎn)元組: {original_dict} -> {dict_keys_tuple}")
# 字典 -> 集合(鍵的集合)
dict_keys_set = set(original_dict)
print(f"字典鍵轉(zhuǎn)集合: {original_dict} -> {dict_keys_set}")
print("\n" + "="*50 + "\n")
# 6. 列表/元組 -> 字典(特殊轉(zhuǎn)換)
# 使用zip將兩個列表轉(zhuǎn)換為字典
keys = ['a', 'b', 'c']
values = [1, 2, 3]
list_to_dict = dict(zip(keys, values))
print(f"兩個列表轉(zhuǎn)字典: keys={keys}, values={values} -> {list_to_dict}")
# 列表/元組的元組/列表 -> 字典
pairs = [('a', 1), ('b', 2), ('c', 3)]
pairs_to_dict = dict(pairs)
print(f"對列表轉(zhuǎn)字典: {pairs} -> {pairs_to_dict}")
# 嵌套列表轉(zhuǎn)字典
nested_list = [['x', 10], ['y', 20], ['z', 30]]
nested_to_dict = dict(nested_list)
print(f"嵌套列表轉(zhuǎn)字典: {nested_list} -> {nested_to_dict}")
2. 高級轉(zhuǎn)換示例
# ==================== 高級轉(zhuǎn)換技巧 ====================
def advanced_conversions():
print("高級轉(zhuǎn)換技巧示例:")
# 1. 使用推導(dǎo)式進(jìn)行轉(zhuǎn)換
numbers = [1, 2, 3, 4, 5]
# 列表推導(dǎo)式轉(zhuǎn)換
squares = [x**2 for x in numbers]
print(f"數(shù)字平方列表: {numbers} -> {squares}")
# 字典推導(dǎo)式轉(zhuǎn)換
square_dict = {x: x**2 for x in numbers}
print(f"數(shù)字平方字典: {numbers} -> {square_dict}")
# 集合推導(dǎo)式轉(zhuǎn)換
square_set = {x**2 for x in numbers}
print(f"數(shù)字平方集合: {numbers} -> {square_set}")
# 2. 使用map函數(shù)轉(zhuǎn)換
numbers_str = list(map(str, numbers))
print(f"數(shù)字轉(zhuǎn)字符串列表: {numbers} -> {numbers_str}")
# 3. 使用filter過濾轉(zhuǎn)換
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"過濾偶數(shù): {numbers} -> {even_numbers}")
# 4. 嵌套容器轉(zhuǎn)換
nested_list = [[1, 2], [3, 4], [5, 6]]
# 展平嵌套列表
flat_list = [item for sublist in nested_list for item in sublist]
print(f"展平嵌套列表: {nested_list} -> {flat_list}")
# 5. 多重轉(zhuǎn)換
data = [(1, 'a'), (2, 'b'), (3, 'c')]
# 轉(zhuǎn)換為字典
tuple_list_to_dict = dict(data)
print(f"元組列表轉(zhuǎn)字典: {data} -> {tuple_list_to_dict}")
# 轉(zhuǎn)換為兩個列表
nums, letters = zip(*data) if data else ([], [])
print(f"元組列表拆分為兩個列表: {data} -> nums={nums}, letters={letters}")
# 6. 自定義對象轉(zhuǎn)換
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person(name={self.name}, age={self.age})"
people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 35)]
# 對象列表轉(zhuǎn)換為名字列表
names = [person.name for person in people]
print(f"對象列表轉(zhuǎn)名字列表: {people} -> {names}")
# 對象列表轉(zhuǎn)換為字典
people_dict = {person.name: person.age for person in people}
print(f"對象列表轉(zhuǎn)字典: {people} -> {people_dict}")
advanced_conversions()
3. 實際應(yīng)用場景
# ==================== 實際應(yīng)用場景 ====================
def practical_applications():
print("\n實際應(yīng)用場景:")
# 場景1: 數(shù)據(jù)清洗和去重
print("場景1: 數(shù)據(jù)清洗和去重")
raw_data = [5, 2, 8, 2, 5, 9, 1, 5, 8]
# 使用集合去重,然后排序
cleaned_data = sorted(set(raw_data))
print(f"原始數(shù)據(jù): {raw_data}")
print(f"清洗后數(shù)據(jù): {cleaned_data}")
# 場景2: 統(tǒng)計詞頻
print("\n場景2: 統(tǒng)計詞頻")
text = "apple banana apple orange banana apple"
words = text.split()
# 多種方法統(tǒng)計詞頻
# 方法1: 使用字典
word_count = {}
for word in words:
word_count[word] = word_count.get(word, 0) + 1
print(f"方法1 - 字典統(tǒng)計: {word_count}")
# 方法2: 使用collections.Counter
from collections import Counter
word_counter = Counter(words)
print(f"方法2 - Counter統(tǒng)計: {dict(word_counter)}")
# 場景3: 矩陣轉(zhuǎn)置
print("\n場景3: 矩陣轉(zhuǎn)置")
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# 使用zip轉(zhuǎn)置
transposed = list(zip(*matrix))
print(f"原始矩陣: {matrix}")
print(f"轉(zhuǎn)置矩陣: {transposed}")
# 場景4: 分組數(shù)據(jù)
print("\n場景4: 分組數(shù)據(jù)")
students = [
("Alice", "Math", 85),
("Bob", "Math", 90),
("Alice", "Science", 92),
("Bob", "Science", 88),
("Charlie", "Math", 78)
]
# 按學(xué)生分組成績
from collections import defaultdict
student_grades = defaultdict(list)
for name, subject, grade in students:
student_grades[name].append((subject, grade))
print("按學(xué)生分組成績:")
for student, grades in student_grades.items():
print(f" {student}: {grades}")
# 場景5: 配置文件解析
print("\n場景5: 配置文件解析")
config_lines = [
"DATABASE_HOST=localhost",
"DATABASE_PORT=5432",
"DATABASE_NAME=mydb",
"DEBUG=true"
]
# 轉(zhuǎn)換為配置字典
config_dict = {}
for line in config_lines:
if '=' in line:
key, value = line.split('=', 1)
config_dict[key] = value
print(f"配置字典: {config_dict}")
# 場景6: 處理CSV數(shù)據(jù)
print("\n場景6: 處理CSV數(shù)據(jù)")
csv_data = "name,age,city\nAlice,25,New York\nBob,30,London\nCharlie,35,Paris"
# 解析CSV為列表字典
lines = csv_data.split('\n')
headers = lines[0].split(',')
records = []
for line in lines[1:]:
values = line.split(',')
record = dict(zip(headers, values))
records.append(record)
print("解析后的數(shù)據(jù):")
for record in records:
print(f" {record}")
practical_applications()
三、容器共有函數(shù)
1. 通用操作函數(shù)
# ==================== 容器共有函數(shù) ====================
def common_container_functions():
print("容器共有函數(shù)示例:")
# 示例數(shù)據(jù)
sample_list = [1, 2, 3, 4, 5]
sample_tuple = (1, 2, 3, 4, 5)
sample_set = {1, 2, 3, 4, 5}
sample_dict = {'a': 1, 'b': 2, 'c': 3}
sample_string = "hello"
# 1. len() - 獲取容器長度
print("\n1. len() - 獲取容器長度:")
print(f"列表長度: len({sample_list}) = {len(sample_list)}")
print(f"元組長度: len({sample_tuple}) = {len(sample_tuple)}")
print(f"集合長度: len({sample_set}) = {len(sample_set)}")
print(f"字典長度: len({sample_dict}) = {len(sample_dict)}")
print(f"字符串長度: len('{sample_string}') = {len(sample_string)}")
# 2. max() - 獲取最大值
print("\n2. max() - 獲取最大值:")
print(f"列表最大值: max({sample_list}) = {max(sample_list)}")
print(f"元組最大值: max({sample_tuple}) = {max(sample_tuple)}")
print(f"集合最大值: max({sample_set}) = {max(sample_set)}")
# 字典最大值(默認(rèn)比較鍵)
print(f"字典鍵最大值: max({sample_dict}) = {max(sample_dict)}")
print(f"字典值最大值: max({sample_dict}.values()) = {max(sample_dict.values())}")
# 3. min() - 獲取最小值
print("\n3. min() - 獲取最小值:")
print(f"列表最小值: min({sample_list}) = {min(sample_list)}")
print(f"元組最小值: min({sample_tuple}) = {min(sample_tuple)}")
print(f"集合最小值: min({sample_set}) = {min(sample_set)}")
# 4. sum() - 求和(僅限數(shù)值容器)
print("\n4. sum() - 求和:")
print(f"列表求和: sum({sample_list}) = {sum(sample_list)}")
print(f"元組求和: sum({sample_tuple}) = {sum(sample_tuple)}")
print(f"集合求和: sum({sample_set}) = {sum(sample_set)}")
# 字典值求和
print(f"字典值求和: sum({sample_dict}.values()) = {sum(sample_dict.values())}")
# 5. any() - 任意元素為真則返回True
print("\n5. any() - 任意元素為真:")
test_list = [0, False, '', None]
print(f"any({test_list}) = {any(test_list)}")
test_list2 = [0, False, 1, '']
print(f"any({test_list2}) = {any(test_list2)}")
# 6. all() - 所有元素為真則返回True
print("\n6. all() - 所有元素為真:")
print(f"all({test_list}) = {all(test_list)}")
print(f"all({sample_list}) = {all(sample_list)}")
# 7. sorted() - 排序(返回新列表)
print("\n7. sorted() - 排序:")
unsorted_list = [3, 1, 4, 1, 5, 9, 2]
print(f"排序前: {unsorted_list}")
print(f"升序排序: {sorted(unsorted_list)}")
print(f"降序排序: {sorted(unsorted_list, reverse=True)}")
# 字典排序
print(f"字典按鍵排序: {sorted(sample_dict)}")
print(f"字典按值排序: {sorted(sample_dict.items(), key=lambda x: x[1])}")
# 8. reversed() - 反轉(zhuǎn)(返回迭代器)
print("\n8. reversed() - 反轉(zhuǎn):")
print(f"列表反轉(zhuǎn): {list(reversed(sample_list))}")
print(f"元組反轉(zhuǎn): {tuple(reversed(sample_tuple))}")
print(f"字符串反轉(zhuǎn): {''.join(reversed(sample_string))}")
# 9. enumerate() - 同時獲取索引和值
print("\n9. enumerate() - 枚舉:")
print("列表枚舉:")
for index, value in enumerate(sample_list):
print(f" [{index}] = {value}")
print("字典枚舉(鍵值對):")
for index, (key, value) in enumerate(sample_dict.items()):
print(f" [{index}] {key} = {value}")
# 10. zip() - 并行迭代多個容器
print("\n10. zip() - 并行迭代:")
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
cities = ["New York", "London", "Paris"]
print("并行迭代多個列表:")
for name, age, city in zip(names, ages, cities):
print(f" {name} ({age}歲) 來自 {city}")
# 轉(zhuǎn)換為字典
people_dict = dict(zip(names, ages))
print(f"通過zip創(chuàng)建字典: {people_dict}")
common_container_functions()
2. 成員測試和迭代
# ==================== 成員測試和迭代 ====================
def membership_and_iteration():
print("\n成員測試和迭代:")
# 示例數(shù)據(jù)
fruits = ["apple", "banana", "orange", "grape"]
fruit_set = {"apple", "banana", "orange", "grape"}
fruit_dict = {"apple": 1, "banana": 2, "orange": 3, "grape": 4}
# 1. in 操作符 - 成員測試
print("1. in 操作符 - 成員測試:")
# 列表成員測試
print(f"'apple' 在列表 {fruits} 中: {'apple' in fruits}")
print(f"'pear' 在列表 {fruits} 中: {'pear' in fruits}")
# 集合成員測試(更快)
print(f"'apple' 在集合 {fruit_set} 中: {'apple' in fruit_set}")
# 字典成員測試(默認(rèn)測試鍵)
print(f"'apple' 在字典 {fruit_dict} 的鍵中: {'apple' in fruit_dict}")
print(f"1 在字典 {fruit_dict} 的值中: {1 in fruit_dict.values()}")
print(f"('apple', 1) 在字典 {fruit_dict} 的項中: {('apple', 1) in fruit_dict.items()}")
# 2. not in 操作符
print("\n2. not in 操作符:")
print(f"'pear' 不在列表 {fruits} 中: {'pear' not in fruits}")
# 3. 迭代容器
print("\n3. 迭代容器:")
# 列表迭代
print("迭代列表:")
for fruit in fruits:
print(f" - {fruit}")
# 集合迭代(無序)
print("\n迭代集合:")
for fruit in fruit_set:
print(f" - {fruit}")
# 字典迭代
print("\n迭代字典(鍵):")
for key in fruit_dict:
print(f" - {key}")
print("\n迭代字典(鍵值對):")
for key, value in fruit_dict.items():
print(f" - {key}: {value}")
# 4. 帶索引的迭代
print("\n4. 帶索引的迭代:")
print("使用enumerate:")
for i, fruit in enumerate(fruits):
print(f" [{i}] {fruit}")
# 5. 同時迭代多個容器
print("\n5. 同時迭代多個容器:")
quantities = [10, 20, 15, 25]
prices = [1.5, 0.8, 2.0, 3.5]
print("水果庫存:")
for fruit, qty, price in zip(fruits, quantities, prices):
total = qty * price
print(f" {fruit}: {qty}個 × ${price} = ${total:.2f}")
# 6. 使用iter()和next()手動迭代
print("\n6. 手動迭代:")
fruit_iter = iter(fruits)
print(f"第一次next: {next(fruit_iter)}")
print(f"第二次next: {next(fruit_iter)}")
print(f"第三次next: {next(fruit_iter)}")
# 7. 迭代時修改容器(注意事項)
print("\n7. 迭代時修改容器(注意事項):")
# 錯誤示例:迭代時刪除元素
numbers = [1, 2, 3, 4, 5]
print(f"原始列表: {numbers}")
# 安全的方式:創(chuàng)建副本或使用列表推導(dǎo)式
numbers_copy = numbers.copy()
to_remove = []
for num in numbers_copy:
if num % 2 == 0:
to_remove.append(num)
for num in to_remove:
numbers.remove(num)
print(f"刪除偶數(shù)后: {numbers}")
# 使用列表推導(dǎo)式(更簡潔)
numbers = [1, 2, 3, 4, 5]
numbers = [num for num in numbers if num % 2 != 0]
print(f"使用推導(dǎo)式刪除偶數(shù)后: {numbers}")
membership_and_iteration()
3. 切片操作(適用于序列類型)
# ==================== 切片操作 ====================
def slicing_operations():
print("\n切片操作(適用于序列類型):")
# 示例數(shù)據(jù)
data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
text = "Hello, World!"
# 基本切片語法: sequence[start:stop:step]
# 1. 基本切片
print("1. 基本切片:")
print(f"完整序列: {data}")
print(f"data[2:6]: {data[2:6]}") # 從索引2到5(不包含6)
print(f"data[:5]: {data[:5]}") # 從開始到索引4
print(f"data[5:]: {data[5:]}") # 從索引5到結(jié)束
print(f"data[:]: {data[:]}") # 完整副本
# 2. 帶步長的切片
print("\n2. 帶步長的切片:")
print(f"data[::2]: {data[::2]}") # 每隔一個元素
print(f"data[1::2]: {data[1::2]}") # 從索引1開始,每隔一個
print(f"data[2:8:2]: {data[2:8:2]}") # 從2到7,每隔一個
# 3. 負(fù)索引切片
print("\n3. 負(fù)索引切片:")
print(f"data[-3:]: {data[-3:]}") # 最后3個元素
print(f"data[:-3]: {data[:-3]}") # 除了最后3個
print(f"data[-5:-2]: {data[-5:-2]}") # 從倒數(shù)第5到倒數(shù)第3
# 4. 負(fù)步長(反轉(zhuǎn))
print("\n4. 負(fù)步長(反轉(zhuǎn)):")
print(f"data[::-1]: {data[::-1]}") # 反轉(zhuǎn)列表
print(f"data[5:1:-1]: {data[5:1:-1]}") # 從索引5到索引2(反向)
print(f"data[-2:-6:-1]: {data[-2:-6:-1]}") # 從倒數(shù)第2到倒數(shù)第5(反向)
# 5. 字符串切片
print("\n5. 字符串切片:")
print(f"原始字符串: '{text}'")
print(f"text[0:5]: '{text[0:5]}'") # Hello
print(f"text[7:12]: '{text[7:12]}'") # World
print(f"text[::2]: '{text[::2]}'") # 每隔一個字符
print(f"text[::-1]: '{text[::-1]}'") # 反轉(zhuǎn)字符串
# 6. 元組切片
print("\n6. 元組切片:")
data_tuple = tuple(data)
print(f"元組切片 data_tuple[2:6]: {data_tuple[2:6]}")
# 7. 切片賦值(僅可變序列)
print("\n7. 切片賦值(僅可變序列):")
numbers = [0, 1, 2, 3, 4, 5]
print(f"原始列表: {numbers}")
# 替換切片部分
numbers[2:4] = [20, 30, 40] # 可以改變元素數(shù)量
print(f"替換后: {numbers}")
# 刪除切片部分
numbers[1:4] = []
print(f"刪除后: {numbers}")
# 插入元素
numbers[2:2] = [100, 200] # 在索引2處插入
print(f"插入后: {numbers}")
# 8. 高級切片應(yīng)用
print("\n8. 高級切片應(yīng)用:")
# 提取每行數(shù)據(jù)的特定列
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
# 提取第二列
col2 = [row[1] for row in matrix]
print(f"矩陣第二列: {col2}")
# 使用切片獲取子矩陣
submatrix = [row[1:3] for row in matrix[1:3]]
print(f"子矩陣: {submatrix}")
# 9. 自定義可切片對象
print("\n9. 自定義可切片對象:")
class SliceableList:
def __init__(self, data):
self.data = list(data)
def __getitem__(self, key):
if isinstance(key, slice):
# 處理切片
return self.data[key]
else:
# 處理單個索引
return self.data[key]
def __setitem__(self, key, value):
if isinstance(key, slice):
self.data[key] = value
else:
self.data[key] = value
def __repr__(self):
return f"SliceableList({self.data})"
custom_list = SliceableList(range(10))
print(f"自定義列表: {custom_list}")
print(f"自定義列表切片[2:7:2]: {custom_list[2:7:2]}")
slicing_operations()
4. 容器比較和復(fù)制
# ==================== 容器比較和復(fù)制 ====================
def comparison_and_copy():
print("\n容器比較和復(fù)制:")
# 1. 容器比較
print("1. 容器比較:")
# 列表比較(按元素逐個比較)
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [1, 2, 4]
print(f"{list1} == {list2}: {list1 == list2}")
print(f"{list1} == {list3}: {list1 == list3}")
print(f"{list1} != {list3}: {list1 != list3}")
# 字典比較(比較鍵值對)
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 2, 'a': 1} # 順序不同
print(f"{dict1} == {dict2}: {dict1 == dict2}")
# 集合比較
set1 = {1, 2, 3}
set2 = {3, 2, 1} # 順序不同
print(f"{set1} == {set2}: {set1 == set2}")
# 包含關(guān)系比較
print(f"{set1} 是 {set2} 的超集: {set1 >= set2}")
print(f"{set1} 是 {set2} 的子集: {set1 <= set2}")
# 2. 淺拷貝(shallow copy)
print("\n2. 淺拷貝:")
original = [[1, 2], [3, 4]]
# 列表的淺拷貝方法
copy1 = original.copy() # copy()方法
copy2 = list(original) # 構(gòu)造函數(shù)
copy3 = original[:] # 切片
print(f"原始列表: {original}")
print(f"淺拷貝1: {copy1}")
print(f"淺拷貝2: {copy2}")
print(f"淺拷貝3: {copy3}")
# 修改原始列表
original[0][0] = 100
print(f"修改原始列表后:")
print(f"原始列表: {original}")
print(f"淺拷貝1: {copy1}") # 也被修改了!
print("注意:淺拷貝只復(fù)制第一層,嵌套對象是共享的")
# 3. 深拷貝(deep copy)
print("\n3. 深拷貝:")
import copy
original = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(original)
print(f"原始列表: {original}")
print(f"深拷貝: {deep_copy}")
# 修改原始列表
original[0][0] = 100
print(f"修改原始列表后:")
print(f"原始列表: {original}")
print(f"深拷貝: {deep_copy}") # 未被修改
print("注意:深拷貝復(fù)制所有嵌套對象")
# 4. 復(fù)制字典
print("\n4. 字典復(fù)制:")
original_dict = {'a': [1, 2], 'b': [3, 4]}
# 淺拷貝
shallow_dict = original_dict.copy()
# 深拷貝
deep_dict = copy.deepcopy(original_dict)
# 修改原始字典
original_dict['a'][0] = 100
print(f"原始字典: {original_dict}")
print(f"淺拷貝字典: {shallow_dict}") # 嵌套列表被修改
print(f"深拷貝字典: {deep_dict}") # 未被修改
# 5. 復(fù)制集合
print("\n5. 集合復(fù)制:")
original_set = {1, 2, 3}
# 集合復(fù)制
set_copy = original_set.copy()
print(f"原始集合: {original_set}")
print(f"復(fù)制集合: {set_copy}")
# 6. 不可變?nèi)萜鞯?復(fù)制"
print("\n6. 不可變?nèi)萜鞯?復(fù)制':")
# 元組是不可變的,所以賦值只是創(chuàng)建引用
original_tuple = (1, 2, [3, 4])
tuple_reference = original_tuple
# 要創(chuàng)建包含可變元素的元組的真正副本
import copy
tuple_copy = copy.deepcopy(original_tuple)
# 修改原始元組中的列表
original_tuple[2][0] = 100
print(f"原始元組: {original_tuple}")
print(f"元組引用: {tuple_reference}") # 也被修改了
print(f"元組深拷貝: {tuple_copy}") # 未被修改
# 7. 性能考慮
print("\n7. 性能考慮:")
# 測試不同復(fù)制方法的性能
import time
large_list = list(range(1000000))
# 測試淺拷貝性能
start = time.time()
shallow_copy = large_list.copy()
shallow_time = time.time() - start
# 測試切片復(fù)制性能
start = time.time()
slice_copy = large_list[:]
slice_time = time.time() - start
# 測試構(gòu)造函數(shù)復(fù)制性能
start = time.time()
constructor_copy = list(large_list)
constructor_time = time.time() - start
print(f"淺拷貝時間: {shallow_time:.6f}秒")
print(f"切片復(fù)制時間: {slice_time:.6f}秒")
print(f"構(gòu)造函數(shù)復(fù)制時間: {constructor_time:.6f}秒")
print("三種方法性能相近,選擇最清晰的方式即可")
comparison_and_copy()
5. 內(nèi)置函數(shù)與容器操作
# ==================== 內(nèi)置函數(shù)與容器操作 ====================
def builtin_functions_with_containers():
print("\n內(nèi)置函數(shù)與容器操作:")
# 1. map() - 對容器中每個元素應(yīng)用函數(shù)
print("1. map() - 映射:")
numbers = [1, 2, 3, 4, 5]
# 轉(zhuǎn)換為字符串
str_numbers = list(map(str, numbers))
print(f"數(shù)字轉(zhuǎn)字符串: {numbers} -> {str_numbers}")
# 計算平方
squares = list(map(lambda x: x**2, numbers))
print(f"計算平方: {numbers} -> {squares}")
# 2. filter() - 過濾容器元素
print("\n2. filter() - 過濾:")
# 過濾偶數(shù)
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"過濾偶數(shù): {numbers} -> {even_numbers}")
# 過濾非空字符串
strings = ["hello", "", "world", "", "python"]
non_empty = list(filter(None, strings)) # None會過濾掉假值
print(f"過濾空字符串: {strings} -> {non_empty}")
# 3. reduce() - 累積計算(需要functools)
print("\n3. reduce() - 累積計算:")
from functools import reduce
# 計算乘積
product = reduce(lambda x, y: x * y, numbers)
print(f"計算乘積: {numbers} -> {product}")
# 連接字符串
words = ["Hello", "World", "Python"]
concatenated = reduce(lambda x, y: x + " " + y, words)
print(f"連接字符串: {words} -> '{concatenated}'")
# 4. zip() - 合并多個容器
print("\n4. zip() - 合并容器:")
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# 合并為元組列表
people = list(zip(names, ages))
print(f"合并列表: names={names}, ages={ages} -> {people}")
# 處理不等長列表
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c', 'd']
combined = list(zip(list1, list2)) # 以最短的為準(zhǔn)
print(f"不等長合并: {list1}, {list2} -> {combined}")
# 5. enumerate() - 帶索引的迭代
print("\n5. enumerate() - 帶索引迭代:")
fruits = ["apple", "banana", "orange"]
print("帶索引迭代:")
for index, fruit in enumerate(fruits):
print(f" [{index}] {fruit}")
# 指定起始索引
print("指定起始索引為1:")
for index, fruit in enumerate(fruits, start=1):
print(f" [{index}] {fruit}")
# 6. sorted() 和 reversed() 的更多用法
print("\n6. sorted() 和 reversed():")
# 復(fù)雜對象排序
students = [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30},
{"name": "Charlie", "age": 22}
]
# 按年齡排序
sorted_by_age = sorted(students, key=lambda x: x["age"])
print(f"按年齡排序: {sorted_by_age}")
# 按姓名排序
sorted_by_name = sorted(students, key=lambda x: x["name"])
print(f"按姓名排序: {sorted_by_name}")
# 多級排序
from operator import itemgetter
data = [
("apple", 3, 1.5),
("banana", 2, 0.8),
("apple", 1, 1.5),
("banana", 3, 0.8)
]
# 先按名稱,再按數(shù)量排序
multi_sorted = sorted(data, key=itemgetter(0, 1))
print(f"多級排序: {multi_sorted}")
# 7. all() 和 any() 的高級用法
print("\n7. all() 和 any() 的高級用法:")
# 檢查所有字符串長度大于2
strings = ["hello", "world", "python", "go"]
all_long = all(len(s) > 2 for s in strings)
print(f"所有字符串長度>2: {strings} -> {all_long}")
# 檢查是否有字符串包含特定字符
any_contains_o = any('o' in s for s in strings)
print(f"有字符串包含'o': {strings} -> {any_contains_o}")
# 8. sum() 的進(jìn)階用法
print("\n8. sum() 的進(jìn)階用法:")
# 計算嵌套列表總和
nested = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
total = sum(sum(sublist) for sublist in nested)
print(f"嵌套列表總和: {nested} -> {total}")
# 計算字典值總和
inventory = {"apples": 10, "bananas": 5, "oranges": 8}
total_items = sum(inventory.values())
print(f"庫存總數(shù): {inventory} -> {total_items}")
# 9. 使用iter()和next()的高級模式
print("\n9. iter()和next()的高級模式:")
# 分批處理
def batch_process(data, batch_size=3):
"""將數(shù)據(jù)分批處理"""
it = iter(data)
while True:
batch = []
try:
for _ in range(batch_size):
batch.append(next(it))
yield batch
except StopIteration:
if batch:
yield batch
break
numbers = list(range(10))
print(f"原始數(shù)據(jù): {numbers}")
print("分批處理:")
for i, batch in enumerate(batch_process(numbers, 3), 1):
print(f" 批次{i}: {batch}")
builtin_functions_with_containers()
四、綜合應(yīng)用示例
# ==================== 綜合應(yīng)用示例 ====================
def comprehensive_example():
print("\n綜合應(yīng)用示例:")
# 場景:學(xué)生成績管理系統(tǒng)
students_data = [
{"id": 1, "name": "Alice", "scores": {"math": 85, "english": 90, "science": 88}},
{"id": 2, "name": "Bob", "scores": {"math": 78, "english": 85, "science": 92}},
{"id": 3, "name": "Charlie", "scores": {"math": 92, "english": 88, "science": 95}},
{"id": 4, "name": "Diana", "scores": {"math": 65, "english": 70, "science": 68}},
]
# 1. 提取所有學(xué)生姓名
student_names = [student["name"] for student in students_data]
print(f"1. 所有學(xué)生姓名: {student_names}")
# 2. 計算每個學(xué)生的平均分
student_averages = []
for student in students_data:
name = student["name"]
scores = student["scores"].values()
average = sum(scores) / len(scores) if scores else 0
student_averages.append((name, average))
print(f"\n2. 學(xué)生平均分: {student_averages}")
# 3. 按平均分排序
sorted_students = sorted(student_averages, key=lambda x: x[1], reverse=True)
print(f"\n3. 按平均分排序: {sorted_students}")
# 4. 找出數(shù)學(xué)成績最高的學(xué)生
math_scores = [(student["name"], student["scores"]["math"]) for student in students_data]
top_math_student = max(math_scores, key=lambda x: x[1])
print(f"\n4. 數(shù)學(xué)最高分: {top_math_student}")
# 5. 統(tǒng)計各科目平均分
subjects = ["math", "english", "science"]
subject_averages = {}
for subject in subjects:
scores = [student["scores"][subject] for student in students_data]
subject_averages[subject] = sum(scores) / len(scores)
print(f"\n5. 各科目平均分:")
for subject, avg in subject_averages.items():
print(f" {subject}: {avg:.2f}")
# 6. 找出需要幫助的學(xué)生(任一科目低于70分)
struggling_students = []
for student in students_data:
name = student["name"]
low_scores = [subject for subject, score in student["scores"].items() if score < 70]
if low_scores:
struggling_students.append((name, low_scores))
print(f"\n6. 需要幫助的學(xué)生:")
for student, low_subjects in struggling_students:
print(f" {student}: {low_subjects}")
# 7. 創(chuàng)建成績報告
print("\n7. 成績報告:")
report = []
for student in students_data:
name = student["name"]
scores = student["scores"]
total = sum(scores.values())
average = total / len(scores)
# 成績等級
if average >= 90:
grade = "A"
elif average >= 80:
grade = "B"
elif average >= 70:
grade = "C"
elif average >= 60:
grade = "D"
else:
grade = "F"
report.append({
"name": name,
"scores": scores,
"average": round(average, 2),
"grade": grade
})
for student_report in report:
print(f" {student_report['name']}: 平均分={student_report['average']}, 等級={student_report['grade']}")
print(f" 各科成績: {student_report['scores']}")
# 8. 數(shù)據(jù)轉(zhuǎn)換:將學(xué)生數(shù)據(jù)轉(zhuǎn)換為不同的格式
print("\n8. 數(shù)據(jù)格式轉(zhuǎn)換:")
# 轉(zhuǎn)換為CSV格式
csv_lines = ["name,math,english,science,average,grade"]
for student_report in report:
csv_line = f"{student_report['name']},{student_report['scores']['math']},"
csv_line += f"{student_report['scores']['english']},{student_report['scores']['science']},"
csv_line += f"{student_report['average']},{student_report['grade']}"
csv_lines.append(csv_line)
print("CSV格式:")
for line in csv_lines:
print(f" {line}")
# 轉(zhuǎn)換為字典格式(按學(xué)科分組)
subject_groups = {}
for subject in subjects:
subject_groups[subject] = {}
for student in students_data:
subject_groups[subject][student["name"]] = student["scores"][subject]
print("\n按學(xué)科分組:")
for subject, scores in subject_groups.items():
print(f" {subject}: {scores}")
comprehensive_example()
五、性能考慮與最佳實踐
# ==================== 性能考慮與最佳實踐 ====================
def performance_and_best_practices():
print("\n性能考慮與最佳實踐:")
import time
import sys
# 1. 選擇合適的容器類型
print("1. 選擇合適的容器類型:")
# 測試列表和集合的成員測試性能
print("測試成員測試性能:")
# 創(chuàng)建大數(shù)據(jù)集
large_list = list(range(1000000))
large_set = set(large_list)
# 測試列表成員測試
start = time.time()
result = 999999 in large_list
list_time = time.time() - start
# 測試集合成員測試
start = time.time()
result = 999999 in large_set
set_time = time.time() - start
print(f" 列表成員測試時間: {list_time:.6f}秒")
print(f" 集合成員測試時間: {set_time:.6f}秒")
print(f" 集合比列表快 {list_time/set_time:.1f}倍")
# 2. 避免不必要的轉(zhuǎn)換
print("\n2. 避免不必要的轉(zhuǎn)換:")
data = list(range(10000))
# 不必要轉(zhuǎn)換
start = time.time()
for _ in range(1000):
temp = list(data) # 創(chuàng)建不必要的副本
unnecessary_time = time.time() - start
# 直接使用
start = time.time()
for _ in range(1000):
temp = data # 直接引用
direct_time = time.time() - start
print(f" 不必要轉(zhuǎn)換時間: {unnecessary_time:.6f}秒")
print(f" 直接使用時間: {direct_time:.6f}秒")
# 3. 使用生成器表達(dá)式代替列表推導(dǎo)式(節(jié)省內(nèi)存)
print("\n3. 使用生成器表達(dá)式節(jié)省內(nèi)存:")
# 大數(shù)據(jù)集
huge_range = range(10000000)
# 列表推導(dǎo)式(消耗大量內(nèi)存)
print(" 列表推導(dǎo)式內(nèi)存測試:")
start_memory = sys.getsizeof([])
# 注意:這里不實際創(chuàng)建,只是演示
# huge_list = [x**2 for x in huge_range] # 這會消耗大量內(nèi)存
# 生成器表達(dá)式(節(jié)省內(nèi)存)
print(" 生成器表達(dá)式內(nèi)存測試:")
huge_gen = (x**2 for x in huge_range)
gen_memory = sys.getsizeof(huge_gen)
print(f" 生成器對象大小: {gen_memory}字節(jié)")
print(" 生成器只在迭代時生成值,不預(yù)先生成所有值")
# 4. 使用內(nèi)置函數(shù)和庫函數(shù)
print("\n4. 使用內(nèi)置函數(shù)和庫函數(shù):")
numbers = list(range(1000000))
# 手動求最大值
start = time.time()
max_value = numbers[0]
for num in numbers:
if num > max_value:
max_value = num
manual_time = time.time() - start
# 使用內(nèi)置max函數(shù)
start = time.time()
max_value = max(numbers)
builtin_time = time.time() - start
print(f" 手動求最大值時間: {manual_time:.6f}秒")
print(f" 內(nèi)置max函數(shù)時間: {builtin_time:.6f}秒")
# 5. 批量操作 vs 逐個操作
print("\n5. 批量操作 vs 逐個操作:")
# 創(chuàng)建測試數(shù)據(jù)
strings = ["hello"] * 10000
# 逐個連接(低效)
start = time.time()
result = ""
for s in strings:
result += s
concat_time = time.time() - start
# 批量連接(高效)
start = time.time()
result = "".join(strings)
join_time = time.time() - start
print(f" 逐個連接時間: {concat_time:.6f}秒")
print(f" 批量連接時間: {join_time:.6f}秒")
print(f" join()比逐個連接快 {concat_time/join_time:.1f}倍")
# 6. 適當(dāng)使用切片
print("\n6. 適當(dāng)使用切片:")
data = list(range(1000000))
# 復(fù)制列表的不同方法
start = time.time()
for _ in range(100):
copy1 = data[:]
slice_time = time.time() - start
start = time.time()
for _ in range(100):
copy2 = list(data)
list_time = time.time() - start
start = time.time()
for _ in range(100):
copy3 = data.copy()
copy_time = time.time() - start
print(f" 切片復(fù)制時間: {slice_time:.6f}秒")
print(f" 構(gòu)造函數(shù)復(fù)制時間: {list_time:.6f}秒")
print(f" copy()方法時間: {copy_time:.6f}秒")
print(" 三種方法性能相似,選擇最清晰的方式")
# 7. 總結(jié)建議
print("\n7. 最佳實踐總結(jié):")
print(" - 頻繁成員測試時使用集合(set)而不是列表(list)")
print(" - 使用生成器表達(dá)式處理大數(shù)據(jù)節(jié)省內(nèi)存")
print(" - 優(yōu)先使用內(nèi)置函數(shù)和庫函數(shù)")
print(" - 字符串操作使用join()而不是循環(huán)拼接")
print(" - 避免不必要的容器轉(zhuǎn)換和復(fù)制")
print(" - 使用切片進(jìn)行高效復(fù)制和操作")
print(" - 根據(jù)需求選擇合適的容器類型")
performance_and_best_practices()
總結(jié)
容器轉(zhuǎn)換要點:
- 類型間轉(zhuǎn)換:使用目標(biāo)類型的構(gòu)造函數(shù)(如
list(),tuple(),set(),dict()) - 字典轉(zhuǎn)換:使用
dict()和zip()組合,或直接從鍵值對列表轉(zhuǎn)換 - 字符串轉(zhuǎn)換:使用
str()或join()方法 - 去重轉(zhuǎn)換:使用
set()可以快速去重
容器共有函數(shù)要點:
- 通用函數(shù):
len(),max(),min(),sum(),sorted(),reversed() - 成員測試:使用
in和not in操作符 - 迭代操作:
for循環(huán),enumerate(),zip() - 切片操作:適用于所有序列類型(列表、元組、字符串)
- 比較操作:
==,!=,>,<等(不同類型容器比較規(guī)則不同)
性能建議:
- 選擇合適容器:根據(jù)操作類型選擇最有效的容器
- 使用內(nèi)置函數(shù):它們通常經(jīng)過優(yōu)化,比手動實現(xiàn)更快
- 避免不必要轉(zhuǎn)換:特別是大數(shù)據(jù)集
- 批量操作優(yōu)先:如使用
join()而非循環(huán)拼接字符串 - 注意深淺拷貝:根據(jù)需求選擇正確的復(fù)制方式
掌握容器轉(zhuǎn)換和共有函數(shù)是Python編程的基礎(chǔ),能夠大大提高代碼的簡潔性和效率。
現(xiàn)代C++容器轉(zhuǎn)換與共有函數(shù)詳解
一、C++ STL容器概覽
C++標(biāo)準(zhǔn)模板庫(STL)提供了多種容器,分為序列容器、關(guān)聯(lián)容器和無序關(guān)聯(lián)容器。
| 容器類型 | 描述 | 頭文件 | 示例 |
|---|---|---|---|
vector | 動態(tài)數(shù)組 | <vector> | vector<int> v = {1,2,3}; |
deque | 雙端隊列 | <deque> | deque<int> d = {1,2,3}; |
list | 雙向鏈表 | <list> | list<int> l = {1,2,3}; |
forward_list | 單向鏈表 | <forward_list> | forward_list<int> fl = {1,2,3}; |
array | 固定大小數(shù)組 | <array> | array<int,3> a = {1,2,3}; |
set | 有序唯一鍵集合 | <set> | set<int> s = {1,2,3}; |
multiset | 有序集合(允許多個相同鍵) | <set> | multiset<int> ms = {1,2,2,3}; |
map | 有序鍵值對映射 | <map> | map<string,int> m = {{"a",1},{"b",2}}; |
multimap | 有序映射(允許多個相同鍵) | <map> | multimap<string,int> mm = {{"a",1},{"a",2}}; |
unordered_set | 無序唯一鍵集合 | <unordered_set> | unordered_set<int> us = {1,2,3}; |
unordered_multiset | 無序集合(允許多個相同鍵) | <unordered_set> | unordered_multiset<int> ums = {1,2,2,3}; |
unordered_map | 無序鍵值對映射 | <unordered_map> | unordered_map<string,int> um = {{"a",1},{"b",2}}; |
unordered_multimap | 無序映射(允許多個相同鍵) | <unordered_map> | unordered_multimap<string,int> umm = {{"a",1},{"a",2}}; |
stack | 棧(適配器) | <stack> | stack<int> st; |
queue | 隊列(適配器) | <queue> | queue<int> q; |
priority_queue | 優(yōu)先隊列(適配器) | <queue> | priority_queue<int> pq; |
二、容器轉(zhuǎn)換
在C++中,容器之間的轉(zhuǎn)換通常通過構(gòu)造函數(shù)、賦值操作或標(biāo)準(zhǔn)庫算法來實現(xiàn)。由于C++是強(qiáng)類型語言,轉(zhuǎn)換時需要考慮類型匹配和性能。
1. 使用構(gòu)造函數(shù)進(jìn)行轉(zhuǎn)換
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <algorithm>
int main() {
// 1. 從數(shù)組或初始化列表構(gòu)造
int arr[] = {1, 2, 3, 2, 1};
std::vector<int> vec(std::begin(arr), std::end(arr));
std::cout << "vector from array: ";
for (int x : vec) std::cout << x << ' ';
std::cout << '\n';
// 2. 從vector構(gòu)造list
std::list<int> lst(vec.begin(), vec.end());
std::cout << "list from vector: ";
for (int x : lst) std::cout << x << ' ';
std::cout << '\n';
// 3. 從vector構(gòu)造set(自動去重和排序)
std::set<int> s(vec.begin(), vec.end());
std::cout << "set from vector (sorted and unique): ";
for (int x : s) std::cout << x << ' ';
std::cout << '\n';
// 4. 從vector構(gòu)造unordered_set(去重,無序)
std::unordered_set<int> us(vec.begin(), vec.end());
std::cout << "unordered_set from vector (unique, unordered): ";
for (int x : us) std::cout << x << ' ';
std::cout << '\n';
// 5. 將set轉(zhuǎn)換為vector
std::vector<int> vec2(s.begin(), s.end());
std::cout << "vector from set: ";
for (int x : vec2) std::cout << x << ' ';
std::cout << '\n';
// 6. 將兩個vector組合成map
std::vector<std::string> keys = {"one", "two", "three"};
std::vector<int> values = {1, 2, 3};
std::map<std::string, int> m;
for (size_t i = 0; i < keys.size() && i < values.size(); ++i) {
m[keys[i]] = values[i];
}
std::cout << "map from two vectors: ";
for (const auto& p : m) std::cout << p.first << ":" << p.second << ' ';
std::cout << '\n';
// 使用std::transform和std::inserter
std::map<std::string, int> m2;
std::transform(keys.begin(), keys.end(), values.begin(),
std::inserter(m2, m2.end()),
[](const std::string& k, int v) { return std::make_pair(k, v); });
std::cout << "map using transform: ";
for (const auto& p : m2) std::cout << p.first << ":" << p.second << ' ';
std::cout << '\n';
return 0;
}
2. 使用賦值和交換
#include <iostream>
#include <vector>
#include <list>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::list<int> lst;
// 使用assign方法(會覆蓋原有內(nèi)容)
lst.assign(vec.begin(), vec.end());
std::cout << "list after assign from vector: ";
for (int x : lst) std::cout << x << ' ';
std::cout << '\n';
// 使用swap交換兩個相同類型的容器(常數(shù)時間)
std::vector<int> vec2 = {10, 20, 30};
std::cout << "before swap:\n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << '\n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << '\n';
vec.swap(vec2);
std::cout << "after swap:\n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << '\n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << '\n';
// 使用std::swap也可以
std::swap(vec, vec2);
std::cout << "after std::swap:\n";
std::cout << "vec: "; for (int x : vec) std::cout << x << ' '; std::cout << '\n';
std::cout << "vec2: "; for (int x : vec2) std::cout << x << ' '; std::cout << '\n';
return 0;
}
3. 使用算法進(jìn)行轉(zhuǎn)換
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
int main() {
std::vector<int> vec = {5, 3, 1, 4, 2, 3, 1};
// 復(fù)制到另一個vector
std::vector<int> vec2;
std::copy(vec.begin(), vec.end(), std::back_inserter(vec2));
std::cout << "copy to vec2: ";
for (int x : vec2) std::cout << x << ' ';
std::cout << '\n';
// 復(fù)制到set(去重和排序)
std::set<int> s;
std::copy(vec.begin(), vec.end(), std::inserter(s, s.begin()));
std::cout << "copy to set: ";
for (int x : s) std::cout << x << ' ';
std::cout << '\n';
// 使用std::transform進(jìn)行轉(zhuǎn)換
std::vector<int> vec3;
std::transform(vec.begin(), vec.end(), std::back_inserter(vec3),
[](int x) { return x * 2; });
std::cout << "transform (double): ";
for (int x : vec3) std::cout << x << ' ';
std::cout << '\n';
// 使用std::remove_copy_if過濾
std::vector<int> vec4;
std::remove_copy_if(vec.begin(), vec.end(), std::back_inserter(vec4),
[](int x) { return x % 2 == 0; });
std::cout << "remove_copy_if (remove even): ";
for (int x : vec4) std::cout << x << ' ';
std::cout << '\n';
// 使用std::unique_copy去重(需要先排序)
std::vector<int> vec5;
std::sort(vec.begin(), vec.end());
std::unique_copy(vec.begin(), vec.end(), std::back_inserter(vec5));
std::cout << "unique_copy after sorting: ";
for (int x : vec5) std::cout << x << ' ';
std::cout << '\n';
return 0;
}
三、容器共有函數(shù)
1. 通用成員函數(shù)
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>
void container_common_functions() {
std::cout << "=== 容器共有函數(shù) ===\n";
// 1. 構(gòu)造函數(shù)和析構(gòu)函數(shù)
std::vector<int> v1 = {1, 2, 3}; // 初始化列表構(gòu)造函數(shù)
std::vector<int> v2(v1.begin(), v1.end()); // 范圍構(gòu)造函數(shù)
std::vector<int> v3(5, 10); // 數(shù)量+值構(gòu)造函數(shù)
// 2. 賦值操作
std::vector<int> v4;
v4 = v1; // 拷貝賦值
v4 = {4, 5, 6}; // 初始化列表賦值
v4.assign(3, 7); // assign方法
v4.assign(v1.begin(), v1.end()); // 范圍assign
// 3. 迭代器
std::cout << "迭代器: ";
for (auto it = v1.begin(); it != v1.end(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n";
std::cout << "反向迭代器: ";
for (auto it = v1.rbegin(); it != v1.rend(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n";
// 4. 容量相關(guān)
std::cout << "size: " << v1.size() << "\n";
std::cout << "max_size: " << v1.max_size() << "\n";
std::cout << "empty: " << std::boolalpha << v1.empty() << "\n";
// vector特有的容量操作
v1.reserve(10);
std::cout << "capacity: " << v1.capacity() << "\n";
v1.shrink_to_fit();
// 5. 元素訪問
if (!v1.empty()) {
std::cout << "front: " << v1.front() << "\n";
std::cout << "back: " << v1.back() << "\n";
}
// 6. 修改器
v1.push_back(4);
v1.pop_back();
v1.insert(v1.begin() + 1, 99);
v1.erase(v1.begin() + 1);
v1.clear();
// 7. 交換
std::vector<int> v5 = {10, 20, 30};
v1.swap(v5);
// 8. 比較操作
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 2, 3};
std::cout << "a == b: " << (a == b) << "\n";
std::cout << "a < b: " << (a < b) << "\n";
}
2. 序列容器特有操作
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <array>
void sequence_container_specific() {
std::cout << "\n=== 序列容器特有操作 ===\n";
// 1. vector - 動態(tài)數(shù)組
std::vector<int> vec = {1, 2, 3};
// 隨機(jī)訪問
std::cout << "vector[1]: " << vec[1] << "\n";
std::cout << "vector.at(1): " << vec.at(1) << "\n";
// 調(diào)整大小
vec.resize(5, 0); // 擴(kuò)展,新增元素初始化為0
std::cout << "after resize(5): ";
for (int x : vec) std::cout << x << " ";
std::cout << "\n";
vec.resize(2); // 縮小
std::cout << "after resize(2): ";
for (int x : vec) std::cout << x << " ";
std::cout << "\n";
// 2. deque - 雙端隊列
std::deque<int> dq = {1, 2, 3};
dq.push_front(0); // 前端插入
dq.push_back(4); // 后端插入
dq.pop_front(); // 前端刪除
dq.pop_back(); // 后端刪除
// 3. list - 雙向鏈表
std::list<int> lst = {1, 2, 3};
// 鏈表特有操作
lst.push_front(0);
lst.pop_front();
// 合并、拼接
std::list<int> lst2 = {4, 5, 6};
lst.merge(lst2);
lst.sort(); // 鏈表排序(成員函數(shù))
lst.unique(); // 刪除連續(xù)重復(fù)元素
// 4. forward_list - 單向鏈表
std::forward_list<int> flist = {1, 2, 3};
// 只有push_front,沒有push_back
flist.push_front(0);
flist.pop_front();
// 5. array - 固定大小數(shù)組
std::array<int, 3> arr = {1, 2, 3};
// array特有:fill方法
arr.fill(7);
std::cout << "array after fill(7): ";
for (int x : arr) std::cout << x << " ";
std::cout << "\n";
}
3. 關(guān)聯(lián)容器特有操作
#include <iostream>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
void associative_container_specific() {
std::cout << "\n=== 關(guān)聯(lián)容器特有操作 ===\n";
// 1. set/multiset
std::set<int> s = {3, 1, 4, 1, 5, 9};
std::multiset<int> ms = {3, 1, 4, 1, 5, 9};
// 插入
auto result = s.insert(2);
std::cout << "插入2: " << (result.second ? "成功" : "失敗") << "\n";
// 查找
auto it = s.find(3);
if (it != s.end()) {
std::cout << "找到3\n";
}
// 計數(shù)
std::cout << "set中1的個數(shù): " << s.count(1) << "\n";
std::cout << "multiset中1的個數(shù): " << ms.count(1) << "\n";
// 范圍查找
auto lower = s.lower_bound(2); // 第一個>=2的元素
auto upper = s.upper_bound(5); // 第一個>5的元素
auto range = s.equal_range(4); // 等于4的范圍
// 2. map/multimap
std::map<std::string, int> m = {{"apple", 1}, {"banana", 2}};
std::multimap<std::string, int> mm = {{"apple", 1}, {"apple", 2}};
// 插入
m.insert({"orange", 3});
m.emplace("pear", 4);
// 訪問(map特有)
std::cout << "apple: " << m["apple"] << "\n";
std::cout << "orange: " << m.at("orange") << "\n";
// 3. 無序容器特有操作
std::unordered_set<int> us = {1, 2, 3, 4, 5};
std::unordered_map<std::string, int> um = {{"a", 1}, {"b", 2}};
// 桶接口
std::cout << "桶數(shù)量: " << us.bucket_count() << "\n";
std::cout << "負(fù)載因子: " << us.load_factor() << "\n";
// 重新哈希
us.rehash(20);
us.reserve(100);
// 遍歷桶
for (size_t i = 0; i < us.bucket_count(); ++i) {
std::cout << "桶" << i << "有" << us.bucket_size(i) << "個元素\n";
}
}
4. 容器適配器特有操作
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <deque>
void container_adapter_specific() {
std::cout << "\n=== 容器適配器特有操作 ===\n";
// 1. stack - 棧
std::stack<int> st;
st.push(1);
st.push(2);
st.push(3);
std::cout << "棧頂: " << st.top() << "\n";
st.pop();
std::cout << "彈出后棧頂: " << st.top() << "\n";
// 2. queue - 隊列
std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << "隊首: " << q.front() << "\n";
std::cout << "隊尾: " << q.back() << "\n";
q.pop();
std::cout << "彈出后隊首: " << q.front() << "\n";
// 3. priority_queue - 優(yōu)先隊列
std::priority_queue<int> pq;
pq.push(3);
pq.push(1);
pq.push(4);
pq.push(2);
std::cout << "優(yōu)先隊列: ";
while (!pq.empty()) {
std::cout << pq.top() << " ";
pq.pop();
}
std::cout << "\n";
// 自定義比較器的優(yōu)先隊列(最小堆)
std::priority_queue<int, std::vector<int>, std::greater<int>> min_pq;
min_pq.push(3);
min_pq.push(1);
min_pq.push(4);
std::cout << "最小堆優(yōu)先隊列: ";
while (!min_pq.empty()) {
std::cout << min_pq.top() << " ";
min_pq.pop();
}
std::cout << "\n";
}
四、實用工具函數(shù)
1. 迭代器工具
#include <iostream>
#include <vector>
#include <list>
#include <iterator>
#include <algorithm>
void iterator_utilities() {
std::cout << "\n=== 迭代器工具 ===\n";
std::vector<int> vec = {1, 2, 3, 4, 5};
// 1. 迭代器適配器
// back_inserter
std::vector<int> dest;
std::copy(vec.begin(), vec.end(), std::back_inserter(dest));
// front_inserter(需要支持push_front的容器)
std::list<int> lst;
std::copy(vec.begin(), vec.end(), std::front_inserter(lst));
// inserter(在指定位置插入)
std::vector<int> vec2;
std::copy(vec.begin(), vec.end(), std::inserter(vec2, vec2.begin()));
// 2. 流迭代器
std::cout << "使用ostream_iterator輸出: ";
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
// 3. 反向迭代器
std::cout << "反向輸出: ";
std::copy(vec.rbegin(), vec.rend(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
// 4. 移動迭代器(C++11)
std::vector<std::string> strings = {"hello", "world"};
std::vector<std::string> strings2;
// 使用移動迭代器轉(zhuǎn)移資源
strings2.insert(strings2.begin(),
std::make_move_iterator(strings.begin()),
std::make_move_iterator(strings.end()));
std::cout << "移動后原vector大小: " << strings.size() << "\n";
std::cout << "目標(biāo)vector大小: " << strings2.size() << "\n";
}
2. 算法庫中的容器操作
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <execution> // C++17 并行算法
void algorithm_utilities() {
std::cout << "\n=== 算法庫工具 ===\n";
std::vector<int> vec = {5, 3, 1, 4, 2, 6, 8, 7};
// 1. 排序和查找
std::sort(vec.begin(), vec.end());
std::cout << "排序后: ";
for (int x : vec) std::cout << x << " ";
std::cout << "\n";
bool found = std::binary_search(vec.begin(), vec.end(), 4);
std::cout << "二分查找4: " << (found ? "找到" : "未找到") << "\n";
// 2. 數(shù)值算法
int sum = std::accumulate(vec.begin(), vec.end(), 0);
int product = std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int>());
std::cout << "和: " << sum << "\n";
std::cout << "積: " << product << "\n";
// 3. 變換
std::vector<int> transformed;
std::transform(vec.begin(), vec.end(), std::back_inserter(transformed),
[](int x) { return x * 2; });
// 4. 過濾
std::vector<int> filtered;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(filtered),
[](int x) { return x % 2 == 0; });
// 5. 并行算法(C++17)
std::vector<int> large_vec(1000000);
std::iota(large_vec.begin(), large_vec.end(), 0);
// 并行排序
std::sort(std::execution::par, large_vec.begin(), large_vec.end());
// 6. 范圍算法(C++20概念)
#if __cplusplus >= 202002L
std::ranges::sort(vec);
#endif
}
3. 視圖和范圍(C++20)
#if __cplusplus >= 202002L
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
void ranges_and_views() {
std::cout << "\n=== C++20 范圍和視圖 ===\n";
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 1. 范圍算法
std::ranges::sort(vec);
auto found = std::ranges::find(vec, 5);
// 2. 視圖(惰性求值)
auto even_squares = vec
| std::views::filter([](int x) { return x % 2 == 0; })
| std::views::transform([](int x) { return x * x; });
std::cout << "偶數(shù)的平方: ";
for (int x : even_squares) {
std::cout << x << " ";
}
std::cout << "\n";
// 3. 更多視圖操作
// 取前5個元素
auto first5 = vec | std::views::take(5);
// 跳過前3個元素
auto skip3 = vec | std::views::drop(3);
// 反轉(zhuǎn)
auto reversed = vec | std::views::reverse;
// 4. 管道操作符組合
auto result = vec
| std::views::filter([](int x) { return x > 5; })
| std::views::transform([](int x) { return x * 2; })
| std::views::take(3);
std::cout << "管道操作結(jié)果: ";
for (int x : result) {
std::cout << x << " ";
}
std::cout << "\n";
}
#endif
五、類型轉(zhuǎn)換和類型特性
1. 類型轉(zhuǎn)換工具
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <type_traits>
void type_conversion_utilities() {
std::cout << "\n=== 類型轉(zhuǎn)換工具 ===\n";
// 1. 使用std::common_type獲取公共類型
using type1 = std::common_type_t<int, double>;
using type2 = std::common_type_t<int, float, double>;
std::cout << "int和double的公共類型: " << typeid(type1).name() << "\n";
std::cout << "int, float, double的公共類型: " << typeid(type2).name() << "\n";
// 2. 使用decay去掉引用和const
using decayed = std::decay_t<const int&>;
std::cout << "const int& decay后: " << typeid(decayed).name() << "\n";
// 3. 判斷類型特性
std::cout << "vector<int>是容器: "
<< std::boolalpha
<< std::is_same_v<decltype(std::begin(std::vector<int>{})),
typename std::vector<int>::iterator> << "\n";
// 4. 條件類型選擇
constexpr bool use_vector = true;
using Container = std::conditional_t<use_vector,
std::vector<int>,
std::list<int>>;
Container c = {1, 2, 3};
std::cout << "使用的容器類型: " << typeid(c).name() << "\n";
}
2. 智能指針與容器
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>
void smart_pointers_with_containers() {
std::cout << "\n=== 智能指針與容器 ===\n";
// 1. 容器存儲unique_ptr
std::vector<std::unique_ptr<int>> vec;
vec.push_back(std::make_unique<int>(10));
vec.push_back(std::make_unique<int>(20));
vec.push_back(std::make_unique<int>(30));
// 遍歷unique_ptr容器
for (const auto& ptr : vec) {
std::cout << *ptr << " ";
}
std::cout << "\n";
// 2. 容器存儲shared_ptr
std::vector<std::shared_ptr<int>> shared_vec;
auto p1 = std::make_shared<int>(100);
auto p2 = p1; // 共享所有權(quán)
shared_vec.push_back(p1);
shared_vec.push_back(p2);
std::cout << "引用計數(shù): " << p1.use_count() << "\n";
// 3. 使用weak_ptr避免循環(huán)引用
struct Node {
std::weak_ptr<Node> next;
int value;
Node(int v) : value(v) {}
};
auto node1 = std::make_shared<Node>(1);
auto node2 = std::make_shared<Node>(2);
node1->next = node2;
node2->next = node1; // weak_ptr不會增加引用計數(shù)
}
六、性能優(yōu)化技巧
1. 高效容器操作
#include <iostream>
#include <vector>
#include <list>
#include <chrono>
#include <algorithm>
void performance_optimization() {
std::cout << "\n=== 性能優(yōu)化技巧 ===\n";
const size_t N = 1000000;
// 1. 預(yù)分配內(nèi)存
{
std::vector<int> v1;
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; ++i) {
v1.push_back(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "無預(yù)分配插入" << N << "個元素: " << duration.count() << " ms\n";
}
{
std::vector<int> v2;
v2.reserve(N);
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < N; ++i) {
v2.push_back(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "預(yù)分配后插入" << N << "個元素: " << duration.count() << " ms\n";
}
// 2. 使用emplace代替insert
std::vector<std::pair<int, std::string>> v;
// 低效:創(chuàng)建臨時對象然后復(fù)制
v.push_back(std::make_pair(1, "one"));
// 高效:直接在容器中構(gòu)造
v.emplace_back(2, "two");
// 3. 使用移動語義
std::string large_string = "這是一個很長的字符串...";
std::vector<std::string> strings;
strings.push_back(std::move(large_string)); // 移動而非復(fù)制
// 4. 選擇合適的算法
std::vector<int> data(N);
std::iota(data.begin(), data.end(), 0);
// 線性查找
auto start = std::chrono::high_resolution_clock::now();
auto it1 = std::find(data.begin(), data.end(), N/2);
auto end = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// 二分查找(需要先排序)
std::sort(data.begin(), data.end());
start = std::chrono::high_resolution_clock::now();
bool found = std::binary_search(data.begin(), data.end(), N/2);
end = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
std::cout << "線性查找時間: " << duration1.count() << " μs\n";
std::cout << "二分查找時間: " << duration2.count() << " μs\n";
}
2. 內(nèi)存池和自定義分配器
#include <iostream>
#include <vector>
#include <memory>
#include <list>
// 簡單的內(nèi)存池分配器
template<typename T>
class SimplePoolAllocator {
public:
using value_type = T;
SimplePoolAllocator() = default;
template<typename U>
SimplePoolAllocator(const SimplePoolAllocator<U>&) {}
T* allocate(std::size_t n) {
std::cout << "分配 " << n << " 個 " << typeid(T).name() << " 對象\n";
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t n) {
std::cout << "釋放 " << n << " 個 " << typeid(T).name() << " 對象\n";
::operator delete(p);
}
};
template<typename T, typename U>
bool operator==(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) {
return true;
}
template<typename T, typename U>
bool operator!=(const SimplePoolAllocator<T>&, const SimplePoolAllocator<U>&) {
return false;
}
void custom_allocator_example() {
std::cout << "\n=== 自定義分配器示例 ===\n";
// 使用自定義分配器的vector
std::vector<int, SimplePoolAllocator<int>> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
std::cout << "vector元素: ";
for (int x : vec) std::cout << x << " ";
std::cout << "\n";
// 使用自定義分配器的list
std::list<int, SimplePoolAllocator<int>> lst;
lst.push_back(10);
lst.push_back(20);
lst.push_back(30);
}
七、綜合示例:學(xué)生成績管理系統(tǒng)
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <string>
#include <iomanip>
struct Student {
int id;
std::string name;
std::map<std::string, double> scores; // 科目 -> 分?jǐn)?shù)
double average_score() const {
if (scores.empty()) return 0.0;
double sum = std::accumulate(scores.begin(), scores.end(), 0.0,
[](double acc, const auto& pair) { return acc + pair.second; });
return sum / scores.size();
}
void add_score(const std::string& subject, double score) {
scores[subject] = score;
}
};
class GradeBook {
private:
std::vector<Student> students;
std::set<std::string> subjects;
public:
// 添加學(xué)生
void add_student(int id, const std::string& name) {
students.push_back({id, name, {}});
}
// 添加成績
void add_score(int student_id, const std::string& subject, double score) {
auto it = std::find_if(students.begin(), students.end(),
[student_id](const Student& s) {
return s.id == student_id;
});
if (it != students.end()) {
it->add_score(subject, score);
subjects.insert(subject);
}
}
// 計算每個學(xué)生的平均分
std::map<int, double> calculate_averages() const {
std::map<int, double> result;
for (const auto& student : students) {
result[student.id] = student.average_score();
}
return result;
}
// 按平均分排序?qū)W生
std::vector<std::pair<int, double>> sort_by_average() const {
auto averages = calculate_averages();
std::vector<std::pair<int, double>> sorted(averages.begin(), averages.end());
std::sort(sorted.begin(), sorted.end(),
[](const auto& a, const auto& b) {
return a.second > b.second; // 降序
});
return sorted;
}
// 計算每科平均分
std::map<std::string, double> calculate_subject_averages() const {
std::map<std::string, double> result;
for (const auto& subject : subjects) {
double sum = 0.0;
int count = 0;
for (const auto& student : students) {
auto it = student.scores.find(subject);
if (it != student.scores.end()) {
sum += it->second;
++count;
}
}
if (count > 0) {
result[subject] = sum / count;
}
}
return result;
}
// 生成報告
void generate_report() const {
std::cout << "\n=== 學(xué)生成績報告 ===\n";
// 學(xué)生成績表
std::cout << "\n學(xué)生成績詳情:\n";
std::cout << std::setw(5) << "ID"
<< std::setw(10) << "姓名";
for (const auto& subject : subjects) {
std::cout << std::setw(10) << subject;
}
std::cout << std::setw(10) << "平均分\n";
for (const auto& student : students) {
std::cout << std::setw(5) << student.id
<< std::setw(10) << student.name;
for (const auto& subject : subjects) {
auto it = student.scores.find(subject);
if (it != student.scores.end()) {
std::cout << std::setw(10) << std::fixed
<< std::setprecision(2) << it->second;
} else {
std::cout << std::setw(10) << "N/A";
}
}
std::cout << std::setw(10) << std::fixed
<< std::setprecision(2) << student.average_score() << "\n";
}
// 按平均分排序
std::cout << "\n按平均分排名:\n";
auto sorted = sort_by_average();
for (size_t i = 0; i < sorted.size(); ++i) {
std::cout << i + 1 << ". 學(xué)生ID: " << sorted[i].first
<< ", 平均分: " << sorted[i].second << "\n";
}
// 科目平均分
std::cout << "\n科目平均分:\n";
auto subject_averages = calculate_subject_averages();
for (const auto& [subject, avg] : subject_averages) {
std::cout << subject << ": " << avg << "\n";
}
}
};
int main() {
GradeBook gradebook;
// 添加學(xué)生
gradebook.add_student(1, "張三");
gradebook.add_student(2, "李四");
gradebook.add_student(3, "王五");
// 添加成績
gradebook.add_score(1, "數(shù)學(xué)", 85.5);
gradebook.add_score(1, "英語", 90.0);
gradebook.add_score(1, "物理", 88.0);
gradebook.add_score(2, "數(shù)學(xué)", 78.0);
gradebook.add_score(2, "英語", 85.0);
gradebook.add_score(2, "物理", 92.5);
gradebook.add_score(3, "數(shù)學(xué)", 92.0);
gradebook.add_score(3, "英語", 88.5);
gradebook.add_score(3, "物理", 95.0);
// 生成報告
gradebook.generate_report();
return 0;
}
總結(jié)對比表
| 特性 | Python容器 | C++ STL容器 |
|---|---|---|
| 類型系統(tǒng) | 動態(tài)類型,容器可包含任意類型 | 靜態(tài)類型,容器元素類型固定 |
| 內(nèi)存管理 | 自動垃圾回收 | 手動或RAII管理 |
| 性能控制 | 有限,依賴解釋器優(yōu)化 | 細(xì)粒度控制,可優(yōu)化到硬件級 |
| 轉(zhuǎn)換靈活性 | 靈活,自動類型轉(zhuǎn)換 | 嚴(yán)格,需要顯式轉(zhuǎn)換 |
| 迭代方式 | 簡單for循環(huán) | 迭代器模式 |
| 算法集成 | 內(nèi)置方法較少,依賴標(biāo)準(zhǔn)庫函數(shù) | 豐富的算法庫,與容器深度集成 |
| 并發(fā)安全 | GIL限制,部分操作原子 | 默認(rèn)非線程安全,需要外部同步 |
| 內(nèi)存布局 | 隱藏細(xì)節(jié) | 明確控制(連續(xù)/鏈?zhǔn)剑?/td> |
最佳實踐
選擇合適容器:
vector:默認(rèn)選擇,緩存友好list/forward_list:頻繁插入刪除deque:兩端操作頻繁set/map:需要排序或快速查找unordered_set/unordered_map:只需要快速查找,不關(guān)心順序
性能優(yōu)化:
- 預(yù)分配vector內(nèi)存
- 使用
emplace代替insert - 優(yōu)先使用算法庫而非手寫循環(huán)
- 考慮使用移動語義
現(xiàn)代C++特性:
- 使用范圍for循環(huán)(C++11)
- 使用結(jié)構(gòu)化綁定(C++17)
- 使用概念和范圍(C++20)
- 使用智能指針管理資源
錯誤處理:
- 使用
at()進(jìn)行邊界檢查 - 檢查迭代器有效性
- 使用異常處理資源錯誤
- 使用
C++容器系統(tǒng)提供了強(qiáng)大的功能和性能控制,但需要更多的類型安全和內(nèi)存管理考慮。掌握這些工具可以編寫出高效、可靠的C++代碼。
總結(jié)
到此這篇關(guān)于Python容器轉(zhuǎn)換與共有函數(shù)的文章就介紹到這了,更多相關(guān)Python容器轉(zhuǎn)換與共有函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows系統(tǒng)下Python如何進(jìn)行版本升級與管理
用戶經(jīng)常在使用python時候,發(fā)現(xiàn)版本升級過快,不同的項目可能用到不同的python版本,本文就windows下,關(guān)于Python 3.11 升級到 Python 3.12做一個簡單的記錄和介紹,希望對大家有所幫助2025-05-05
Python實現(xiàn)基于KNN算法的筆跡識別功能詳解
這篇文章主要介紹了Python實現(xiàn)基于KNN算法的筆跡識別功能,結(jié)合實例形式詳細(xì)分析了使用KNN算法進(jìn)行筆跡識別的相關(guān)庫引入、操作步驟與相關(guān)注意事項,需要的朋友可以參考下2018-07-07
python并發(fā)編程多進(jìn)程 模擬搶票實現(xiàn)過程
這篇文章主要介紹了python并發(fā)編程多進(jìn)程 模擬搶票實現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08

