1. 对istringstream 进行扼要的说明 C++独有
istringstream是一个比较有用的c++的输入输出控制类。用于string类型的输入输出
C++引入了ostringstream、istringstream、stringstream这三个类,
要使用他们创建对象就必须包含< sstream >这个头文件。
istringstream类用于执行C++风格的串流的输入操作。
ostringstream类用于执行C++风格的串流的输出操作。
strstream类同时可以支持C++风格的串流的输入输出操作。
istringstream的构造函数原形如下:
explicit istringstream (const string& str,
ios_base::openmode which = ios_base::in);
插入器: << 向流输出数据。cout默认为标准的输出流,一般指的是显示器。cout<<
析取器: >> 从流中输入数据。cin默认为标准的输入流,一般指键盘。cin>>
1、它的作用是从string对象str中读取字符。标准用法
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string str = "i am a boy";
istringstream iss(str); // 从string对象str中读取字符。
string s;
while (iss >> s) //采用while循环,则避免了计算循环次数的问题。
{
cout << s << endl;
}
return 0;
}
2、利用stringstream构建模板类,将string类型转换为float类型:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//模板类,用于实现将string类型转换成其他类。
template<class T>
T stringToType(const string &str)
{
istringstream iss(str); //把一个字符串复制过来作为流传输,可以当作cin或者cout那种去使用。
T num; // iss >> num;
iss >> num; //从stringstream流 输入数据,也就是说num的值是由iss赋给的。在输入的时候就实现了数据类型的改变。
return num;
}
int main()
{
string str = "100 200 300 400";
float f1 = stringToType<float>(str);
cout << f1 << endl;
return 0;
}
输出结果:
3、利用C风格的代码将string类型转换为float类型
c++中string是一个定义的类,要将其转换为float 或者 int 应先转为 char* 。
如
string --> int
string str; int i=atoi(str.c_str());string -->float
string str; float f=atof(str.c_str());其中 c_str() 表示 返回一个c风格的字符串。
#include <iostream>
#include <string>
using namespace std;
float stringToFloat(string &str1)
{
float f1 = atof(str1.c_str()); //
return f1;
}
int main()
{
string str = "100 200 300 400";
const char* ch = str.c_str();
cout << ch << endl;
float f = stringToFloat(str);
cout << f << endl;
return 0;
}
输出结果:
4、含有空格的字符串的string类型转换为其他类型。
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
template<class T>
vector<T> stringToType(const string &str)
{
istringstream iss(str);
vector<T> Tvec;
T t;
while (iss) //每一循环输出一个字符。
{
iss >> t;
Tvec.push_back(t);
}
return Tvec;
}
int main()
{
string str = "100 200 300 400";
vector<float> v = stringToType<float>(str);
cout << v[0] << endl;
cout << v[1] << " " << v[2] << " " << v[3] << endl;
cout << typeid(v[0]).name() << endl;
return 0;
}
5、将float类型转换为string类型。用字符串流ssteam
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
stringstream b; //创建流对象,用于类型的转换。
string a = "123456";
int c;
b << a; //向流输出数据,此时的流指的是b【入流】 cout<< 一般指显示器(现在指流b)。
b >> c; //从流中输入数据。【出流】cin>> 一般指键盘(现在是流b)。
cout <<typeid(c).name() <<" "<< c << endl;
return 0;
}
6、
原型:int atoi(const char *nptr);
作用:把字符串转换成整型数。
头文件: #include<stdlib.h>/#include< cstdlib >
如果第一个非空格字符不存在或者不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字或结束符 \0 时停止转换,返回整型数。atoi()不会做范围检查,如果超出范围的话,超出上界,则输出上界,超出下界,则输出下界;
stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则会runtime error!
c_str()
头文件:include
作用:将string对象转化为char*对象。
它是String类中的一个函数,它返回当前字符串的首字符地址。用法示例:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a = "123456";
int i = atoi(a.c_str()); // 此时 a.c_str()类型为char*;
cout <<typeid(i).name() <<" "<< i << endl;
return 0;
}