IT评测·应用市场-qidao123.com技术社区

标题: C++中一些常用头文件及其解析 [打印本页]

作者: 张国伟    时间: 2025-1-2 17:43
标题: C++中一些常用头文件及其解析
以下是C++中一些常用头文件及其解析:
<iostream>


  1. #include <iostream>
  2. int main() {
  3.     int num;
  4.     std::cout << "请输入一个整数: ";
  5.     std::cin >> num;
  6.     std::cout << "你输入的整数是: " << num << std::endl;
  7.     return 0;
  8. }
复制代码
<fstream>


  1. #include <fstream>
  2. #include <iostream>
  3. int main() {
  4.     std::ofstream outFile("test.txt");
  5.     if (outFile) {
  6.         outFile << "这是写入文件的一些文本内容" << std::endl;
  7.         outFile.close();
  8.     } else {
  9.         std::cerr << "无法打开文件进行写入" << std::endl;
  10.     }
  11.     std::ifstream inFile("test.txt");
  12.     if (inFile) {
  13.         std::string line;
  14.         while (std::getline(inFile, line)) {
  15.             std::cout << line << std::endl;
  16.         }
  17.         inFile.close();
  18.     } else {
  19.         std::cerr << "无法打开文件进行读取" << std::endl;
  20.     }
  21.     return 0;
  22. }
复制代码
<string>


  1. #include <string>
  2. #include <iostream>
  3. int main() {
  4.     std::string str = "Hello";
  5.     str += " World";
  6.     std::cout << str << std::endl;
  7.     return 0;
  8. }
复制代码
<vector>


  1. #include <vector>
  2. #include <iostream>
  3. int main() {
  4.     std::vector<int> numbers;
  5.     numbers.push_back(1);
  6.     numbers.push_back(2);
  7.     numbers.push_back(3);
  8.     for (int num : numbers) {
  9.         std::cout << num << " ";
  10.     }
  11.     std::cout << std::endl;
  12.     return 0;
  13. }
复制代码
<algorithm>


  1. #include <algorithm>
  2. #include <vector>
  3. #include <iostream>
  4. int main() {
  5.     std::vector<int> values = {3, 1, 4, 1, 5, 9, 2, 6};
  6.     std::sort(values.begin(), values.end());
  7.     for (int num : values) {
  8.         std::cout << num << " ";
  9.     }
  10.     std::cout << std::endl;
  11.     auto it = std::find(values.begin(), values.end(), 4);
  12.     if (it!= values.end()) {
  13.         std::cout << "找到了值为 4 的元素" << std::endl;
  14.     }
  15.     return 0;
  16. }
复制代码
<cmath>


  1. #include <cmath>
  2. #include <iostream>
  3. int main() {
  4.     double num = 9.0;
  5.     double squareRoot = std::sqrt(num);
  6.     std::cout << "9 的平方根是: " << squareRoot << std::endl;
  7.     return 0;
  8. }
复制代码
<cstdlib>


  1. #include <cstdlib>
  2. #include <iostream>
  3. int main() {
  4.     srand(static_cast<unsigned int>(time(nullptr)));
  5.     int randomNum = rand() % 100;  // 生成 0 到 99 之间的随机数
  6.     std::cout << "生成的随机数是: " << randomNum << std::endl;
  7.     return 0;
  8. }
复制代码
<ctime>


  1. #include <ctime>
  2. #include <iostream>
  3. int main() {
  4.     time_t now = time(nullptr);
  5.     struct tm* localTime = localtime(&now);
  6.     char buffer[80];
  7.     strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime);
  8.     std::cout << "当前时间是: " << buffer << std::endl;
  9.     return 0;
  10. }
复制代码
<cstring>


  1. #include <cstring>
  2. #include <iostream>
  3. int main() {
  4.     char str1[20] = "Hello";
  5.     char str2[20];
  6.     strcpy(str2, str1);
  7.     std::cout << "复制后的字符串: " << str2 << std::endl;
  8.     return 0;
  9. }
复制代码
<typeinfo>


  1. #include <iostream>
  2. #include <typeinfo>
  3. class Base {
  4.     virtual void foo() {}
  5. };
  6. class Derived : public Base {};
  7. int main() {
  8.     Base* ptr = new Derived();
  9.     const std::type_info& info = typeid(*ptr);
  10.     std::cout << "对象的实际类型是: " << info.name() << std::endl;
  11.     delete ptr;
  12.     return 0;
  13. }
复制代码
<limits>


  1. #include <iostream>
  2. #include <limits>
  3. int main() {
  4.     std::cout << "int 类型的最大值是: " << std::numeric_limits<int>::max() << std::endl;
  5.     std::cout << "double 类型的机器精度是: " << std::numeric_limits<double>::epsilon() << std::endl;
  6.     return 0;
  7. }
复制代码
<memory>


  1. #include <memory>
  2. #include <iostream>
  3. int main() {
  4.     std::unique_ptr<int> ptr = std::make_unique<int>(42);
  5.     std::cout << *ptr << std::endl;
  6.     return 0;
  7. }
复制代码
<thread>


  1. #include <iostream>
  2. #include <thread>
  3. void hello() {
  4.     std::cout << "Hello from thread " << std::this_thread::get_id() << std::endl;
  5. }
  6. int main() {
  7.     std::thread t(hello);
  8.     std::cout << "Hello from main thread " << std::this_thread::get_id() << std::endl;
  9.     t.join();
  10.     return 0;
  11. }
复制代码
这只是C++众多头文件中的一部门,随着C++标准的不断发展和新特性的引入,还有很多其他有效的头文件来满足各种差异的编程需求。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4