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;

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 08:48:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 08:48:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 08:48:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 08:48:04       18 阅读

热门阅读

  1. Hadoop 集群

    2024-03-21 08:48:04       17 阅读
  2. python连接数据库

    2024-03-21 08:48:04       19 阅读
  3. 列表(list)篇(二)

    2024-03-21 08:48:04       20 阅读
  4. 能源新动力:移动电站行业洞察报告

    2024-03-21 08:48:04       15 阅读
  5. RabbitMQ

    2024-03-21 08:48:04       20 阅读
  6. js数组去重常见方法

    2024-03-21 08:48:04       16 阅读
  7. 数据分析-Pandas数据分类处理

    2024-03-21 08:48:04       17 阅读
  8. React.js快速入门教程

    2024-03-21 08:48:04       19 阅读