雷火电竞-中国电竞赛事及体育赛事平台

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

C語言

當前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

最近在項目工程中碰到一段代碼, 頗為費解, string居然也可以用 <<和>>, 于是我單獨寫了個小程序測了一下:

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

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

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

// 工程中的部分代碼
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;
}

結果為:

size is 22
1
2
1
hello world
size is 0

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支持。如果你想了解更多相關內容請查看下面相關鏈接

上一篇:C++直接cout指針名的含義?

欄    目:C語言

下一篇:mfc文件操作CFile類之創(chuàng)建文件的方法

本文標題:string居然也可以用&amp;lt;&amp;lt;和&amp;gt;&amp;gt

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/337.html

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

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

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

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