6.29-每日面试题
1、什么是快表,快表作用
2、分页存储为什么每页大小是4K
3、字符串和数值的转换如何实现
C++中字符串与数值转换(超详细)
1、使用stringstream
1)、stringstream
#include<iostream>
#include<sstream>
using namespace std;
int main(){
int i = 1;
float f = 1.2;
double d = 1.23;
//数值->字符串
stringstream ss1;
ss1 << i << " " << f << " " << d << endl;
string result = ss1.str();
cout << "result = " << result;
//字符串->数值
string str = "123.5";
//stringstream ss2(str);
stringstream ss2;
ss2 << str;
ss2 >> i >> f;
cout << "i = " << i << "\nf = " << f << endl;
system("pause");
return 0;
}
2)、istringstream和ostringstream(这块我不太懂,是想说明写的方式不同吗,但是可以实现相同的功能?)
#include<iostream>
#include<sstream>
using namespace std;
int main(){
int i = 1;
float f = 1.2;
double d = 1.23;
//数值->字符串
ostringstream oss;
oss << i << " " << f << " " << d << endl;//输出流
string result = oss.str();
cout << "result = " << result;
//字符串->数值
string str = "123.5";
istringstream iss(str);
iss >> i >> f; //输入流
cout << "i = " << i << "\nf = "<<f << endl;
system("pause");
return 0;
}
2、使用函数
字符串转换成数值: stoi()等等
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str = "123.345";
int i = stoi(str);
float f = stof(str);
double d = stod(str);
cout << "i = " << i << "\nf = " << f <<"\nd = "<<d<< endl;
system("pause");
return 0;
}
3、char数组转换成数值
4、数值转换成字符串
#include头文件中:inline string to_string(val);
举例:string to_string (int val);
#include <iostream>
#include<sstream>
using namespace std;
int main()
{
int i = 1;
float f = 1.2;
double d = 1.23;
cout<< to_string(i) << " " << to_string(f) << " " << to_string(d) << endl;// 1 1.200000 1.230000
system("pause");
return 0;
}