istringstream
生活随笔
收集整理的這篇文章主要介紹了
istringstream
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
istringstream類用于執行C++風格的字符串流的輸入操作。
具體分析
istringstream類
描述:從流中提取數據,支持 >> 操作
這里字符串可以包括多個單詞,單詞之間使用空格分開
istringstream的構造函數原形: istringstream::istringstream(string str);
初始化:使用字符串進行初始化
istringstream istr("1 56.7"); istr.str("1 56.7");//把字符串"1 56.7"存入字符串流中
#include <iostream> #include <sstream> using namespace std; int main() { ? istringstream istr("1 56.7"); cout<<istr.str()<<endl;//直接輸出字符串的數據 "1 56.7" string str = istr.str();//函數str()返回一個字符串 cout<<str<<endl; int n; double d; //以空格為界,把istringstream中數據取出,應進行類型轉換 istr>>n;//第一個數為整型數據,輸出1 istr>>d;//第二個數位浮點數,輸出56.7 //假設換下存儲類型 istr>>d;//istringstream第一個數要自動變成浮點型,輸出仍為1 istr>>n;//istringstream第二個數要自動變成整型,有數字的階段,輸出為56 //測試輸出 cout<<d<<endl; cout<<n<<endl; system("pause"); return 1; }
#include<iostream> #include<sstream> #include<string> using namespace std; int main() {string a = "jb b jlha lag lajsi";istringstream in(a);string s;while (in >> s){cout << s << endl;}system("pause");return 0; }
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的istringstream的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Remove Duplicates fr
- 下一篇: Reverse Vowels of a