iostream、fstream、sstream、string、vector、unordered_map、stack

iostream

用于输入输出操作,包含了处理标准输入输出流的功能(例如,cin, cout, cerr等)。

cpp 复制代码
#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;
    std::cout << "You entered: " << number << std::endl;
    return 0;
}

fstream

用于文件的读写操作。它提供了ifstream(用于读取文件),ofstream(用于写入文件),和fstream(可以同时用于读写文件)类。

cpp 复制代码
#include <fstream>
#include <iostream>

int main() {
    std::ofstream outfile("example.txt");
    outfile << "Hello, world!" << std::endl;
    outfile.close();
    
    std::ifstream infile("example.txt");
    std::string line;
    while (getline(infile, line)) {
        std::cout << line << std::endl;
    }
    infile.close();
    return 0;
}

sstream

用于字符串的流操作。主要提供了istringstream(从string读取数据),ostringstream(向string写入数据),和stringstream(可用于读写string)类。

cpp 复制代码
#include <sstream>
#include <iostream>

int main() {
    std::stringstream ss;
    ss << 100 << ' ' << 200;
    int foo, bar;
    ss >> foo >> bar;
    std::cout << foo << ", " << bar << std::endl;
    return 0;
}
cpp 复制代码
        while (getline(inputFile, line)) {
            stringstream ss(line);
            string token;
            vector<string> tokens;
            while (ss >> token) {
                tokens.push_back(token);
            }

string

提供了字符串处理功能,包括定义和操作std::string类型的对象。

cpp 复制代码
#include <string>
#include <iostream>

int main() {
    std::string str = "Hello, world!";
    std::cout << str << std::endl;
    return 0;
}

vector

实现了动态数组的功能,可以存储任意类型的对象,并且可以动态增长和缩小。

cpp 复制代码
#include <vector>
#include <iostream>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    for(int i : vec) {
        std::cout << i << ' ';
    }
    std::cout << std::endl;
    return 0;
}

unordered_map

提供了一种通过键来快速访问元素的方式,基于哈希表实现。它允许快速插入、删除和查找操作。

cpp 复制代码
#include <unordered_map>
#include <iostream>

int main() {
    std::unordered_map<std::string, int> map;
    map["one"] = 1;
    map["two"] = 2;
    std::cout << map["one"] << std::endl;
    return 0;
}

stack

实现了栈的数据结构,提供了后进先出(LIFO)的数据管理方式。

cpp 复制代码
#include <stack>
#include <iostream>

int main() {
    std::stack<int> stack;
    stack.push(1);
    stack.push(2);
    std::cout << stack.top() << std::endl;  // 查看栈顶元素
    stack.pop();                            // 移除栈顶元素
    std::cout << stack.top() << std::endl;
    return 0;
相关推荐
vibecoding日记4 小时前
双非如何快速入职字节等大厂大模型?真实案例分析:推理优化和投机解码
算法·求职·大模型工程师
yszaygr21386 小时前
Verilog参数化游程编码RLE模块
算法
望易6 小时前
刚设计的大模型架构-双域耦合认知框架
算法·架构
复杂网络10 小时前
多个 Claude Code 与多个 Codex 协同工作:设计与实现方案
算法
HjhIron1 天前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法