1、double类型的变量,在赋值时必须用1.0去乘要赋的值,否则会出现dou输出值为整型
double dou = 1.0 * abs(n)/x;
2、处理字符串时常用
atoi(const char *str); 用于c字符串转换为整型
stoi(const string &str); 用于c++字符串转换成整型
c++字符串为空时,使用stoi会报错,这是建议使用atoi
to_string(数); 把数值转换成字符串
c_str(); 把c++string转换成c字符串
3、字符串合并并去重,可以借助ASCII码(128)
//字符串合并、去重,即a+b输出的字符串中不可以有重复的字符
#include <iostream>
using namespace std;
int main(){
int a[128]={0};
string s1,s2,s3;
getline(cin,s1);
getline(cin,s2);
s3=s1+s2;
for(int i=0;i<s3.length() ;i++){
if(a[s3[i]]==0){
cout<<s3[i];
a[s3[i]]++;
}
}
return 0;
}
4、map、set的使用
5、输出时字符间用空格隔开,最后不得有空格,设置标志位
int flag = 0; //开始置0,表示不输出空格,因开头无空格
for(int i = 0; i < v.size(); i++)
{
if(flag == 1) //先输出空格,在输出数据,确保最后无空格
{
cout << " ";
}
cout << v.at(i);
flag = 1;
}
6、输出时间时,格式为hh:mm:ss,输出时格式写法
printf("%02d:%02d:%02d\n", hour, minute, second);
7、c++陷阱:键盘输入一个字符串,中间若有空格,则只把空格前的字符串输给相应字符串
注:本人在“说反话”习题中遇到此类问题,很久没想到,有点崩溃!!!
//输入 hello world my name is xxx
//1、直接用字符串接
string str;
cin >> str; // 此时str的值为hello
//2、要想抓取所有的终端输入,需使用getline函数
string str;
getline(cin, str);