一些函数及其头文件(附加粗略解释)

版权声明:闻盲 https://blog.csdn.net/MI55_845/article/details/83825314

C++ :

只是一些我遇到的函数,更多可参考

  1. #include <bits/stdc++.h> // 万能头文件 : 源代码注意 : 开始时不要用,要熟悉函数的头文件。)

  2. #include < cmath >  //定义数学函数

    ① 幂函数 pow

    	例如 : 10 的 平方 的表达 : pow(10,2)
    

    ② 平方根函数 sqrt

    	例如 : 10开平方 的表达 : sqrt(10)
    

    ③ 整数的绝对值 abs 和 浮点数的绝对值 fabs

    	例如 : abs( - 10 ) ; fabs ( - 11. 2 )
    

    ④ 向上取整 ceil

    	例如 : double a = 5.1;
    				int s = ceil(a);
    				s == 6;
    

    ⑤ 向下取整 floor

    	例如 : double a = 5.1;
    				int s = floor(a);
    				s == 5;
    
  3. #include < iomanip >  //参数化输入/输出

    ① 固定小数点输出

    	double a = 1.2345;
    
    	cout << fixed << setprecision(3) << a << endl;((遵循四舍六入五成双的原则,而不是四舍五入的原则)
    
    	输出结果 == 1.234
    

    ② 输出别的进制

    	cout << hex << a << endl; // 输出十六进制。 
    
    	cout << oct << a << endl; // 输出八进制。 
    
  4. #include < algorithm > //STL 通用算法 (参考
    ① 交换函数 swap (可以交换数字,字母等)

       	/*输入 : 3 , 1  , 2 ; 输出 : 1 , 2 , 3*/
       	#include <iostream>
       	#include <algorithm>
       	using namespace std;
       	int  main()
       	{
       		int a,b,c;
       		
       		cin>>a>>b>>c;
       		
       		if(a>b) swap (a,b);
       		
       		if(a>c) swap (a,c);
       		
       		if(b>c) swap (b,c);
       		
       		cout << a << ' ' << b << ' ' << c << endl;
       		
       		return 0;
       	}
    

    ②排序函数 sort
    1. 默认(升序)
    ① 整数型

       	     #include<iostream>
       		#include<algorithm>
       		using namespace std;
       		int main()
       		{
       			int a[5] = {1,3,4,2,5};
       			
       			sort( a , a + 5);
       			
       			for( int i = 0 ; i < 5 ; i ++)
       			
       			cout << a[i] <<' ';
       			
       			return 0;
       	    }	
    

    ② 字符型

    		#include<iostream>
    		#include<algorithm>
    		using namespace std;
    		
    		int main()
    		{
    		 	char a[5] = { 'a','c','b','e','d'};
    			for(int i = 0 ; i < 5 ; i ++ )
    					cout << a[i] << ' ' ;
    			cout << endl;
    			sort( a , a + 5 );
    			for(int i = 0 ; i < 5 ; i ++ )
    					cout << a[i] << ' ' ;
    			return 0;
    		}
    
    1. 定义(基本数据类型)
    	#include<iostream>
    	#include<algorithm>
    	using namespace std;
    	int  main ( )
    	{
    		 int a[10]={2,4,1,23,5,76,0,43,24,65};
    
    		int b[10]={2,4,1,23,5,76,0,43,24,65};
    
    		int i;
    
    		 for( i = 0 ; i < 10 ; i ++)
      
      				cout<< a[i] << ' ' ;
    
    		cout << endl;
    
    		 sort(a,a+10,greater<int>()); // 降序
    
    	 for(i=0;i<10;i++)
      
      			cout<<a[i]<< ' ';
    
    	cout << endl;
    
    	cout << endl;
    
    	 for( i = 0 ; i < 10 ; i ++)
      
     		 cout << b[i] << ' ' ;
    
    	cout << endl;
    
    	 sort( b , b + 10 ,less<int>()); // 升序
    
    
    	 for( i = 0 ; i < 10 ; i ++ )
      
     		 cout << b[i]<< ' ';
    	
    	  return 0;
    }
    
    1. 定义(引用数据类型string)
    	#include<iostream>
    	#include<algorithm>
    	#include<cstring>
    	using namespace std;
    	
    	int main()
    	{
    		string str("hello world");
    
    		sort(str.begin() , str.end()); // 顺序排序
    
    		cout << str << endl;
    
    		sort(str.rbegin(),str.rend()); // 逆序排序
    
    		cout << str;
    
    		return 0;
    }
    

5. #include < string > // 字符串

① 查找字符串 find
② 替换值 replace
③ 求string类对象的成员函数的字符长度 length || size
④ 清除字符串中的元素 erase
⑤ 在字符串中添加元素 insert
⑥ 将C++的string转化为C的字符串数组 c_str()

下面一道我用到前三个函数的题 :
(更多参考 string的使用

题目内容 && 样例输入 && 样例输出 参考
我的代码 :

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1;

	while(getline(cin,str1))
	{
		string str2 = "you";

		string str3 = "we";

		int i , j , m;

		for( i = 0 ; i < str1.length(); i ++)//循环等于字符长度。
		{
			if( str1.find( str2 , i) < str1.length() )
			//从第 i 个位置开始找"you" , 小于字符长度。
			{
				str1.replace( str1.find( str2 , i ) , 3 , str3 );
				// 从找到的有"you"  开头的 i 位置的 3 个字符 "you" 替换成 "we" 。
			}
		}

		cout << str1 << endl;
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/MI55_845/article/details/83825314
今日推荐