sscanf、sprintf、stringstream常見用法

轉載自:https://blog.csdn.net/jllongbell/article/details/79092891

前言:

    之前沒有接觸過stringstream這個類的時候,經常使用的字符串和數字轉換函數就是sscanf和sprintf函數。開始的時候就以爲這兩個函數應經很叼了,可是畢竟是屬於c的。c++中引入了流的概念,經過流來實現字符串和數字的轉換方便多了。在這裏,總結以前的,並介紹新學的。c++

常見格式串:  

  %% 印出百分比符號,不轉換。
  %c 整數轉成對應的 ASCII 字元。
  %d 整數轉成十進位。
  %f 倍精確度數字轉成浮點數。
  %o 整數轉成八進位。
  %s 整數轉成字符串。
  %x 整數轉成小寫十六進位。
  %X 整數轉成大寫十六進位。
  %n sscanf(str, "%d%n", &dig, &n),%n表示一共轉換了多少位的字符函數

sprintf函數

   sprintf函數原型爲 int sprintf(char *str, const char *format, ...)。做用是格式化字符串,具體功能以下所示:post

  (1)將數字變量轉換爲字符串。url

  (2)獲得整型變量的16進制和8進制字符串。spa

  (3)鏈接多個字符串。.net

int main(){ char str[256] = { 0 }; int data = 1024; //將data轉換爲字符串
    sprintf(str,"%d",data); //獲取data的十六進制
    sprintf(str,"0x%X",data); //獲取data的八進制
    sprintf(str,"0%o",data); const char *s1 = "Hello"; const char *s2 = "World"; //鏈接字符串s1和s2
    sprintf(str,"%s %s",s1,s2); cout<<str<<endl; return 0; } 

sscanf函數

  sscanf函數原型爲int sscanf(const char *str, const char *format, ...)。將參數str的字符串根據參數format字符串來轉換並格式化數據,轉換後的結果存於對應的參數內。具體功能以下:code

  (1)根據格式從字符串中提取數據。如從字符串中取出整數、浮點數和字符串等。orm

  (2)取指定長度的字符串blog

  (3)取到指定字符爲止的字符串字符串

  (4)取僅包含指定字符集的字符串

  (5)取到指定字符集爲止的字符串

  固然,sscanf能夠支持格式串"%[]"形式的,有興趣的能夠研究一下。

int main(){ char s[15] = "123.432,432"; int n; double f1; int f2; sscanf(s, "%lf,%d%n", &f1, &f2, &n); cout<<f1<<" "<<f2<<" "<<n; return 0; } 

輸出結果:123.432 432 11, 即一共轉換了11位的字符。

vstringstream類:

  <sstream>庫定義了三種類:istringstream、ostringstream和stringstream,分別用來進行流的輸入、輸出和輸入輸出操做。

  1.stringstream::str(); returns a string object with a copy of the current contents of the stream.

  2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.

  3.stringstream清空,stringstream s; s.str("");

  4.實現任意類型的轉換

    template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中傳
      out_type result;//這裏存儲轉換結果
      stream>>result;//向result中寫入值
      return result;
    }

int main(){ string s = "1 23 # 4"; stringstream ss; ss<<s; while(ss>>s){ cout<<s<<endl; int val = convert<int>(s); cout<<val<<endl; } return 0; }

輸出:1 1 23 23 # 0 4 4

相關文章
相關標籤/搜索