這個問題在這里已經有了答案: 檔案寫入,插入運算子和寫入函式的區別? (2 個回答) 15 小時前關閉。
我想知道std::basic_ostream<CharT,Traits>::operator<<和之間有什么區別std::basic_ostream<CharT,Traits>::write。性能呢?
#include <iostream>
#include <string>
int main()
{
std::string tempMsg;
tempMsg.reserve( 100 );
tempMsg = "This is a string";
std::cout.write( tempMsg.data( ), tempMsg.size( ) ).write( "\n", 1 );
std::cout << tempMsg << '\n';
}
它們都列印相同的字串。但它們各自的優勢是什么?
uj5u.com熱心網友回復:
該函式允許指定要為字符陣列輸出的字符數。
例如你有宣告
const char *s = "Hello World!";
并且只想從字串文字中輸出單詞“Hello”然后你可以寫
std::cout.write( s, 5 );
如果你會寫
std::cout << s;
然后將輸出整個字串文字。
因此,使用該函式,您可以輸出字符陣列的任何部分,例如
std::cout. write( s 6, 2 ) << 'w' << s 11 << '\n';
至于性能,則沒有差異或差異微不足道。重要的是功能。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373623.html
上一篇:c -如何從文本檔案中讀入陣列
