21xrx.com
2025-03-27 06:47:40 Thursday
文章检索 我的文章 写文章
C++常用库函数大全
2023-07-07 17:43:27 深夜i     12     0
C++ 常用库函数 大全 函数库 编程

C++是一门广泛应用于计算机科学和编程领域的高级编程语言。它的特点是强类型、静态类型和面向对象的编程范式。在开发过程中,我们会使用到很多C++库函数来完成各种操作。本文将介绍一些常用的C++库函数。

1.

iostream库提供了基本的输入和输出函数。我们可以使用cout函数来输出数据,使用cin函数来读取数据。头文件 还包含了其他函数,如endl、setw等。例如:

#include <iostream>
using namespace std;
int main()
  cout << "Hello World!" << endl;
  int num;
  cin >> num;
  cout << "You entered " << num;
  return 0;

2.

string库提供了string类,可以处理字符串。string类有很多成员函数,如size()、substr()、find()等。例如:

#include <iostream>
#include <string>
using namespace std;
int main(){
  string str = "C++ is awesome!";
  cout << str << endl;
  cout << str.size() << endl;
  cout << str.substr(0, 3) << endl;
  cout << str.find("awesome") << endl;
  return 0;
}

3.

cmath库提供了数学函数。例如:

#include <iostream>
#include <cmath>
using namespace std;
int main(){
  double x = 3.0, y = 4.0;
  cout << sqrt(x * x + y * y) << endl;
  cout << atan2(y, x) << endl;
  return 0;
}

4.

vector库提供了vector类,可以管理动态数组。例如:

#include <iostream>
#include <vector>
using namespace std;
int main(){
  vector<int> v;
  for(int i = 0; i < 5; i++){
    v.push_back(i);
  }
  for(int i = 0; i < v.size(); i++){
    cout << v[i] << " ";
  }
  return 0;
}

5.

algorithm库提供了一些模板函数,如sort、find等。例如:

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int arr[] = 2;
  sort(arr, arr + 5);
  for(int i = 0; i < 5; i++){
    cout << arr[i] << " ";
  }
  cout << endl;
  cout << binary_search(arr, arr + 5, 3);
  return 0;
}

总之,C++提供了很多强大的库函数,可以使编程变得更加简单和高效。以上是常用的C++库函数,希望对大家有所帮助。

  
  

评论区