C++中常用的算法头文件及其用法

  • <iostream>:提供输入输出流。例如,使用cout输出一段文字:

    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "Hello, World!" << endl;
        return 0;
    }
    
  • <algorithm>:提供常用的算法函数,例如排序、查找、去重等。例如,使用sort()函数对一个整型数组进行排序:

    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    int main() {
        int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
        int n = sizeof(arr) / sizeof(arr[0]);
        sort(arr, arr + n);
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        return 0;
    }
    
  • <string>:提供字符串容器和字符串操作函数。例如,定义一个字符串并输出其长度:

    #include <string>
    #include <iostream>
    using namespace std;
    
    int main() {
        string str = "Hello, World!";
        cout << str.length() << endl;
        return 0;
    }
    
  • <queue>:提供队列容器。例如,定义一个存储整数的队列,并在其中添加和删除元素:

    #include <queue>
    #include <iostream>
    using namespace std;
    
    int main() {
        queue<int> q;
        q.push(1);
        q.push(2);
        q.push(3);
        while (!q.empty()) {
            cout << q.front() << " ";
            q.pop();
        }
        return 0;
    }
    
  • <stack>:提供栈容器。例如,定义一个存储整数的栈,并在其中添加和删除元素:

    #include <stack>
    #include <iostream>
    using namespace std;
    
    int main() {
        stack<int> s;
        s.push(1);
        s.push(2);
        s.push(3);
        while (!s.empty()) {
            cout << s.top() << " ";
            s.pop();
        }
        return 0;
    }
    
  • <map><unordered_map>:提供映射容器,可以实现键值对的存储和查找。例如,定义一个存储字符串和整数的映射,并在其中添加和查找元素:

    #include <map>
    #include <iostream>
    using namespace std;
    
    int main() {
        map<string, int> m;
        m["one"] = 1;
        m["two"] = 2;
        m["three"] = 3;
        cout << m["two"] << endl;
        return 0;
    }
    
  • <vector>头文件的常用用法举例:

    • 定义一个存储整数的动态数组,并在其中添加元素:

      #include <vector>
      #include <iostream>
      using namespace std;
      
      int main() {
          vector<int> v;
          v.push_back(1);
          v.push_back(2);
          v.push_back(3);
          for (int i = 0; i < v.size(); i++) {
              cout << v[i] << " ";
          }
          return 0;
      }
      
    • 定义一个存储字符串的动态数组,并在其中添加元素:

      #include <vector>
      #include <iostream>
      using namespace std;
      
      int main() {
          vector<string> v;
          v.push_back("Hello");
          v.push_back("World");
          for (int i = 0; i < v.size(); i++) {
              cout << v[i] << " ";
          }
          return 0;
      }
      
    • 定义一个存储自定义类型的动态数组,并在其中添加元素:

      #include <vector>
      #include <iostream>
      using namespace std;
      
      class Person {
      public:
          string name;
          int age;
          Person(string name, int age): name(name), age(age) {}
      };
      
      int main() {
          vector<Person> v;
          v.push_back(Person("Alice", 20));
          v.push_back(Person("Bob", 25));
          for (int i = 0; i < v.size(); i++) {
              cout << v[i].name << " " << v[i].age << endl;
          }
          return 0;
      }
      
  • <algorithm>头文件中常用的算法函数及其用法:

    • sort():对一个数组进行排序。例如,对一个整型数组进行排序:

      #include <algorithm>
      #include <iostream>
      using namespace std;
      
      int main() {
          int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
          int n = sizeof(arr) / sizeof(arr[0]);
          sort(arr, arr + n);
          for (int i = 0; i < n; i++) {
              cout << arr[i] << " ";
          }
          return 0;
      }
      
    • binary_search():在一个已排序的数组中查找某个元素是否存在。例如,在一个已排序的整型数组中查找数字9:

      #include <algorithm>
      #include <iostream>
      using namespace std;
      
      int main() {
          int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
          int n = sizeof(arr) / sizeof(arr[0]);
          if (binary_search(arr, arr + n, 9)) {
              cout << "Found" << endl;
          } else {
              cout << "Not found" << endl;
          }
          return 0;
      }
      
    • reverse():将一个数组反转。例如,将一个整型数组反转:

      #include <algorithm>
      #include <iostream>
      using namespace std;
      
      int main() {
          int arr[] = {1, 2, 3, 4, 5};
          int n = sizeof(arr) / sizeof(arr[0]);
          reverse(arr, arr + n);
          for (int i = 0; i < n; i++) {
              cout << arr[i] << " ";
          }
          return 0;
      }
      
    • max()min():返回两个数中的最大值和最小值。例如,返回两个整数中的最大值:

      #include <algorithm>
      #include <iostream>
      using namespace std;
      
      int main() {
          int a = 3, b = 5;
          cout << max(a, b) << endl;
          return 0;
      }
      
    • count():统计一个数组中某个元素出现的次数。例如,统计一个整型数组中数字5出现的次数:

      #include <algorithm>
      #include <iostream>
      using namespace std;
      
      int main() {
          int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
          int n = sizeof(arr) / sizeof(arr[0]);
          cout << count(arr, arr + n, 5) << endl;
          return 0;
      }
      
  • <string>头文件。

    • +:字符串拼接。例如,将两个字符串拼接在一起:

      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          string str1 = "Hello";
          string str2 = "World";
          string str3 = str1 + " " + str2;
          cout << str3 << endl;
          return 0;
      }
      
    • size():获取字符串长度。例如,获取一个字符串的长度:

      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          string str = "Hello, World!";
          cout << str.size() << endl;
          return 0;
      }
      
    • substr():获取子串。例如,获取一个字符串的子串:

      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          string str = "Hello, World!";
          string sub = str.substr(7, 5);
          cout << sub << endl;
          return 0;
      }
      
    • find():查找子串。例如,查找一个字符串中是否包含某个子串:

      #include <iostream>
      #include <string>
      using namespace std;
      
      int main() {
          string str = "Hello, World!";
          if (str.find("World") != string::npos) {
              cout << "Found" << endl;
          } else {
              cout << "Not found" << endl;
          }
          return 0;
      }
      
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值