欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

string居然也可以用<<和>>

 更新時(shí)間:2019年04月08日 16:29:00   作者:stpeace  
今天小編就為大家分享一篇關(guān)于string居然也可以用<<和>>,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

最近在項(xiàng)目工程中碰到一段代碼, 頗為費(fèi)解, string居然也可以用 <<和>>, 于是我單獨(dú)寫了個(gè)小程序測(cè)了一下:

#include <iostream>
#include <string>
using namespace std;
int main()
{
 int a = 1;
 string s;
 s << a;
 return 0;
}

編譯錯(cuò)誤:error: no match for 'operator<<' in 's << a'   這是正常的。 

但為什么在工程項(xiàng)目中就可以呢? 請(qǐng)教了一下別的同事, 才發(fā)現(xiàn), 是對(duì)string進(jìn)行了擴(kuò)展, 在項(xiàng)目工程中寫測(cè)試代碼, 部分代碼如下:

// 工程中的部分代碼
int main()
{
 int a = 1;
 float b = 2.0f;
 bool c = true;
 string d = "hello world";
 string s;
 s << a;
 s << b;
 s << c;
 s << d;
 cout << "size is " << s.size() << endl;
 int a2;
 float b2;
 bool c2;
 string d2;
 s >> a2;
 s >> b2;
 s >> c2;
 s >> d2;
 cout << a2 << endl;
 cout << b2 << endl; 
 cout << c2 << endl;
 cout << d2 << endl;
 cout << "size is " << s.size() << endl;
 return 0;
}

結(jié)果為:

size is 22
1
2
1
hello world
size is 0

可見, string在這里具備了類似流的功能。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論