C++ istringstream,ostringstream,stringstream的使用

x33g5p2x  于2021-09-24 转载在 C/C++  
字(3.1k)|赞(0)|评价(0)|浏览(3329)

一,基本介绍

头文件:/#include

istringstream类用于执行C++风格的串流的输入操作,支持 >> 操作。

ostringstream类用于执行C风格的串流的输出操作,支持 << 操作。

stringstream类可以同时用于C风格的串流的输入输出操作, 同时支持 >> 和 << 操作,所以,stringstream将上述两个类的功能都包括在内。

二,istringstream

构造方法:

(1)

istringstream(string str);

(2)

istringstream istr;  
istr.str(string str);

用法:

1,将字符串类型转换为其他类型

#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{  
    istringstream istr("123.54");  
  
    string str = istr.str();//str()函数返回一个字符串 
    cout<<str<<endl;   //输出 123.54
     
    double d;
    istr >> d;
    cout << d << endl;  //输出 123.54, 字符串被转换为double类型 
     
    int a;
    istr >> a;
    cout << a << endl;  //注意,流中的数据已经输出完,这里输出的是随机数
	
	istringstream istr2("123.54");
	istr2 >> a; 
	cout << a << endl;   //输出 123
	
	istr2 >> d;
	cout << d << endl;   //注意,这里输出的是 0.54 
	
    return 0;  
}

2,以空格为分隔符将字符串分离

#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{  
    istringstream istr("12 345 678 912");  
    string str;  
 
    while(istr >> str)//以空格为界,每次读取一部分内容 
    {  
        cout<<str<<endl;  
    }  
    /* 输出: 12 345 678 912 */
  	
  	istringstream istr2("26 32.5");
  	int a;
  	double b;
  	
  	istr2 >> a;
  	cout << a << endl;   //输出26 
  	
  	istr2 >> b;
  	cout << b << endl;   //输出32.5 
    return 0;  
}

三,ostringstream

构造方法
(1)

ostringstream(string str);

(2)

ostringstream ostr;
ostr.str(string str);

用法:

构造字符串或修改字符串

#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{   
    ostringstream ostr("abcdef");  
    cout << ostr.str() <<endl; //输出abcdef 
      
    ostr.put('1');   //字符'1'替换了字符'a' 
    cout << ostr.str() << endl;  //输出1bcdef 
  
    ostr << "23";//字符串"23"替换了字符'b''c' 
    cout << ostr.str() << endl;   //输出123def 
    
    ostr << "456789"; //字符串"456789"替换了字符'c''d''e''f'
	cout << ostr.str() << endl;  //输出 123456789,长度增加 
	
    return 0;  
}

四,stringstream

构造方法
(1)

stringstream(string str);

(2)

stringstream str;
str.str(string str);

用法:

1,字符串转换为基本数据类型

#include <fstream> 
#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{   
    //string转为int
    stringstream stream;
    string str = "45";
    int a;
    
    stream << str;  
    stream >> a; 
    
    cout << a << endl; //输出45 
    
    stream.clear(); //清空;
    
    //string转为double 
    string str1 = "5.28";   
	double d;
	
    stream << str1;  
    stream >> d;  
    
    cout << d << endl;  //输出5.28 
	  
    stream.clear(); //清空; 
  
    //string转为char* 
    string str2 = "first";  
    char c[10];  
      
    stream << str2;  
    stream >> c;  
  
    cout << c << endl;//输出first 
    return 0;  
}

2,基本数据类型转换为字符串

#include <fstream> 
#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{  
    //整型转为字符串 
    int a = 10;  
    string str;  
    stringstream stream;  
      
    stream << a;  
    stream >> str;  
      
    cout << str << endl;  //输出10 
    
    stream.clear(); //清空 
  
    //char* 转为 string 
    char c[10] = "first";  
      
    stream << c;  
    stream >> str;  
  
    cout << str << endl;   //输出first 
    
    return 0;  
}

3,以空格为分隔符将字符串分离

#include <iostream> 
#include <sstream> 
using namespace std;  
int main()  
{  
    stringstream stt("12 345 678 912");  
    string str;  
 
    while(stt >> str)//以空格为界,每次读取一部分内容 
    {  
        cout<<str<<endl;  
    }  
    /* 输出: 12 345 678 912 */
  	
  	stringstream stt2("26 32.5");
  	int a;
  	double b;
  	
  	stt2 >> a;
  	cout << a << endl;   //输出26 
  	
  	stt2 >> b;
  	cout << b << endl;   //输出32.5 
    return 0;  
}

相关文章

微信公众号

最新文章

更多