1、char * 转string
char outbuf[200];
string tmp_string;
tmp_string = outbuf;
2、提取字符串0到6位数据
out_total = tmp_string.substr(0, 6);
3、去掉字符串开头的0
string out_total;
out_total.erase(0, out_total.find_first_not_of('0'));
4、读取字符串最后一位数据
string intDec1;
string out_total;
intDec1 = out_total.back();
5、 string与int之间的转换
string2int:
#include<string>
strng str = "1001";
int number = stoi(str.c_str());
注:若字符串包含非数字字符,则无法转换成int类型,返回0。
int2string:
#include<string>
int number = 1001;
string str = to_string(number);
6、获取数组最值
<1> 普通数组
int nums[8] = {1,2,3,8,0,33,11,9};
int max_num = *max_element(nums, nums + 8);
int min_num = *min_element(nums, nums + 8);
<2> vector容器
vector<int> nums = {1,2,3,8,0,33,11,9};
int max_num = *max_element(nums.begin(), nums.end()); //获取最大值
int min_num = *min_element(nums.begin(), nums.end());//获取最小值
7、获取数组最大值最小值对应的下标
<1> 普通数组
int nums[8] = {1,2,3,8,0,33,11,9};
int max_num_index = max_element(nums, nums + 8) - nums;
int min_num_index = min_element(nums, nums + 8) - nums;
<2> vector容器
vector<int> nums = {1,2,3,8,0,33,11,9};
int max_num_index = max_element(nums.begin(), nums.end()) - nums.begin(); //获取最大值对应的下标
int min_num_index = min_element(nums.begin(), nums.end()) - nums.begin();//获取最小值对应的下标
8、double转int
double a = "12.889";
int num = int(a);