欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++中stringstream的用法和實(shí)例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

之前在leetcode中進(jìn)行string和int的轉(zhuǎn)化時(shí)使用過(guò)istringstream,現(xiàn)在大致總結(jié)一下用法和測(cè)試用例。

介紹:C++引入了ostringstream、istringstream、stringstream這三個(gè)類,要使用他們創(chuàng)建對(duì)象就必須包含sstream.h頭文件。

istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。

ostringstream類用于執(zhí)行C風(fēng)格的串流的輸出操作。

stringstream類同時(shí)可以支持C風(fēng)格的串流的輸入輸出操作。

下圖詳細(xì)描述了幾種類之間的繼承關(guān)系:

istringstream是由一個(gè)string對(duì)象構(gòu)造而來(lái),從一個(gè)string對(duì)象讀取字符。

ostringstream同樣是有一個(gè)string對(duì)象構(gòu)造而來(lái),向一個(gè)string對(duì)象插入字符。

stringstream則是用于C++風(fēng)格的字符串的輸入輸出的。

代碼測(cè)試:

#include<iostream> 
#include <sstream>  
using namespace std;<pre name="code" class="cpp">int main(){ 
  string test = "-123 9.87 welcome to, 989, test!"; 
  istringstream iss;//istringstream提供讀 string 的功能 
  iss.str(test);//將 string 類型的 test 復(fù)制給 iss,返回 void  
  string s; 
  cout << "按照空格讀取字符串:" << endl; 
  while (iss >> s){ 
    cout << s << endl;//按空格讀取string 
  } 
  cout << "*********************" << endl; 
 
  istringstream strm(test);  
  //創(chuàng)建存儲(chǔ) test 的副本的 stringstream 對(duì)象 
  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; 
 
  system("pause"); 
  return 0; 
} 

輸出:

總結(jié):

1)在istringstream類中,構(gòu)造字符串流時(shí),空格會(huì)成為字符串參數(shù)的內(nèi)部分界;

2)istringstream類可以用作string與各種類型的轉(zhuǎn)換途徑

3)ignore函數(shù)參數(shù):需要讀取字符串的最大長(zhǎng)度,需要忽略的字符

代碼測(cè)試:

int main(){ 
  ostringstream out; 
  out.put('t');//插入字符 
  out.put('e'); 
  out << "st"; 
  string res = out.str();//提取字符串; 
  cout << res << endl; 
  system("pause"); 
  return 0; 
} 

輸出:test字符串;

注:如果一開始初始化ostringstream,例如ostringstream out("test"),那么之后put或者<<時(shí)的字符串會(huì)覆蓋原來(lái)的字符,超過(guò)的部分在原始基礎(chǔ)上增加。

stringstream同理,三類都可以用來(lái)字符串和不同類型轉(zhuǎn)換。

以上就是小編為大家?guī)?lái)的C++中stringstream的用法和實(shí)例全部?jī)?nèi)容了,希望大家多多支持我們~

上一篇:淺談C++ 基類指針和子類指針的相互賦值

欄    目:C語(yǔ)言

下一篇:基于C++中覆蓋,重載,隱藏的一點(diǎn)重要說(shuō)明

本文標(biāo)題:C++中stringstream的用法和實(shí)例

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1919.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有