1.vector
一维数组的输入
/**
*一维数组vector<int>用cin输入
**/
#include<iostream>
#include<vector>
using namespace std;
int main(){
int N;
cin>>N;
vector<int> inputArray(N); //注意若无(N),用cin输入会失败
for(int i = 0;i < N;i++){
cin>>inputArray[i];
}
}
二维数组的输入
/**
*二维数组vector<vector<int>>用cin输入
**/
#include<iostream>
#include<vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> temp(N,vector<int>(M));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> temp[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cout << temp[i][j];
}
cout << endl;
}
}
2. #include<math.h>
对数运算
/**
*常见对数运算
*double log(); //e为底数
*double log2();
*double log10();
*上面几个函数都重载过,参数可以是整型或者浮点型,返回的结果会有四五位小数
*其他对数运算,可以用换底公式来进行
**/
#include<iostream>
#include<math.h>
using namespace std;
int main(){
cout<<log(10)<<endl;
cout<<log2(10)<<endl;
cout<<log10(10)<<endl;
}
//output:
/**
*2.71828
*2.31254
*1
**/
向上取整或向下取整
/**
*double ceil( double num ); //向上取整
*double floor( double num ); //向下取整
*这两个函数也是被重载过,参数整型或浮点型都可以
**/
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int upInt = ceil(6.1);
int dpInt = floor(6.5);
cout<<upInt<<endl<<dpInt;
}
//output:
/**
*6
*7
**/
cout输出小数点位数
/**
*cout控制输出的小数点位数
**/
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
cout<<fixed<<setpricision(4)<<3.14159<<endl;
cout<<fixed<<3.14159<<endl;
cout<<setpricision(4)<<3.14159<<endl;
}
//output:
/**
* 3.1416 //控制小数点位数
* 3.141590 //fixed默认输出六位小数点
* 3.142 //单独的setpricision输出的是有效数字位数,包括左侧整数部分
**/
小数参与的除法运算
/**
*小数参与的除法运算
*C++的除法中必须有浮点数参与结果才会是浮点数,int/int=int
**/
#include<iostream>
using namespace std;
int main(){
cout<<10/3<<endl;
//output: 3
cout<<10.0/3<<endl;
//output: 3.33333
}