Sunday, October 24, 2021

[C++] stringstream

 WHAT is stringStream?

typedef basic_stringstream<char> stringstream;


it's a char templated basic_stringstream implementation : iostream implements both istream and istream methods.

 WHYstringStream?

1 transforme types to string
<sstream> 定义了三个类:istringstreamostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。
<sstream> 主要用来进行数据类型转换,由于 <sstream> 使用 string 对象来代替字符数组(snprintf方式),就避免缓冲区溢出的危险;而且,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。简单说,相比c库的数据类型转换而言,<sstream> 更加安全、自动和直接。” "itoa/atoi is not in std, it's windows owned methods, better to use sstream" 

int i;
strm >> i;
i >> strm;


2 formating output
ss<<a<< " + " << b <<"="<<c<<endl;

3 reading input text
"

It is more efficient to use a stream rather than getting the entire entity as a String because the latter means that

  1. the entire contents of the response need to be read before they can be returned to your code, and
  2. control cannot be returned to your code until the entire response has been sent by the server.

If you processed the response as a stream, then what you are actually doing is processing it N bytes at a time. This means that you can begin processing the first response segment while the remote server is still sending back the next segment of data. Therefore this makes more sense as an access method if your use-case allows you to process the data as it is received.

"


 HOW stringStream?

1 clear() and str("")
s.clear(); //clear before output
s.str("");//set string buffer content
ex : --------------------------
    // 插入字符串
    ss<<"666";
    // 转换为int类型
    ss>>first;
    cout<<"output to int : "<< first<<endl;
    
    ss.clear();
    cout<<"in buffer after clear: "<<ss.str()<<endl;
    cout<<"in buffer : "<<ss.str()<<endl;
     
    ss<<true;
    ss>>second;
    cout<<"output int: "<<second<<endl;
    
    cout<< "in buffer: " <<ss.str()<<endl;//611
     
    ss.clear();//流出去过了所以必须用clear
    //ss.str("");//再用str("")把之前ss中的字符串清空
    ss<<"123456";
    cout<<"in buffer: " << ss.str()<<endl;//6661123456
    ss.str("");//set buffer to ""
    ss<<"789";
    cout<<"in buffer: " << ss.str()<<endl;//789 
--------------------------


2 <<, >>, ignore()
input (with space), output.
ex:--------------------------
    string test = "-123 9.87 welcome to, 989, test!";
    istringstream iss;//istringstream提供读 string 的功能
    iss.str(test);//将 string 类型的 test 复制给 iss,返回 void
    string s;
    cout << "按照空格读取字符串:" << endl;
    while (iss >> s){
        cout << s << endl;//按空格读取string
    }
    int i;
    float f;
    char c;
    char buff[1024];
  
    strm >> i;
    cout <<"读取int类型:"<< i << endl;
    strm >> f;
    cout <<"读取float类型:"<<f << endl;
    strm >> c;
    cout <<"读取char类型:"<< c << endl;
    strm >> buff;
    cout <<"读取buffer类型:"<< buff << endl;
    strm.ignore(100',');
    int j;
    strm >> j;
    cout <<"忽略‘,’读取int类型:"<< j << endl;
-------------------------

Attention : if nothing was inputed and we do an output, will make the stream in a bad state.
ex:--------------------------
    // 插入字符串
    ss<<"666";
    // 转换为int类型
    int first = 1;
    ss>>first;
    cout<< first<<endl;//666
    cout<< ss.str()<<endl;//666
    
    ss.clear();
    int second = 2;
    ss>>second;
    cout<< second<<endl;//2
    cout<< ss.str()<<endl;//666

    ss<<"777";
    int third = 3;
    ss>>third; //somethingwrong with stream, we cannot get 777
    cout<< third <<endl;//3
    
    cout<< ss.str()<<endl;//666
--------------------------

for comparing : 
  1. 若在原字符串上扩展拼接,请使用 +=或 append 的方法,切勿使用 stringA = stringA + stringB 的方式;output+="a"
  2. 若拼接的结果需要存入新的字符串,且被拼接字符串较短,拼接较频繁,使用先赋值再 append(或 +=)更加有效率;string new(a); new +="b";
  3. 若拼接的结果需要存入新的字符串,且被拼接字符串较长,直接使用 + 拼接再赋值即可;string new ( longStr + a);
  4. 大量字符串拼接为一个字符串,请使用 stringstream。

There's no reason to expect std::string's appending functions to be slower than stringstream's insertion functions. std::string will generally be nothing more than a possible memory allocation/copy plus copying of the data into the memory. stringstream has to deal with things like locales, etc, even for basic write calls.

No comments:

Post a Comment