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

  • iostream

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

#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(可以同时用于读写文件)类。

#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)类。

#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;
}
        while (getline(inputFile, line)) {
            stringstream ss(line);
            string token;
            vector<string> tokens;
            while (ss >> token) {
                tokens.push_back(token);
            }

  • string

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

#include <string>
#include <iostream>

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

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

#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

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

#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)的数据管理方式。

#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;

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值