選單
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C++ 教程

C++ 主頁 C++ 簡介 C++ 入門 C++ 語法 C++ 輸出 C++ 註釋 C++ 變數 C++ 使用者輸入 C++ 資料型別 C++ 運算子 C++ 字串 C++ 數學 C++ 布林值 C++ If...Else C++ Switch C++ While 迴圈 C++ For 迴圈 C++ Break/Continue C++ 陣列 C++ 結構體 C++ 列舉 C++ 引用 C++ 指標

C++ 函式

C++ 函式 C++ 函式引數 C++ 函式過載 C++ 作用域 C++ 遞迴

C++ 類

C++ OOP C++ 類/物件 C++ 類方法 C++ 建構函式 C++ 訪問修飾符 C++ 封裝 C++ 繼承 C++ 多型 C++ 檔案 C++ 異常 C++ 日期

C++ 資料結構

C++ 資料結構與 STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ 迭代器 C++ 演算法

C++ How To

C++ 兩數相加 C++ 隨機數

C++ 參考

C++ 參考 C++ 關鍵字 C++ <iostream> C++ <fstream> C++ <cmath> C++ <string> C++ <cstring> C++ <ctime> C++ <vector> C++ <algorithm>

C++ 示例

C++ 示例 C++ 現實生活中的例子 C++ 編譯器 C++ 練習 C++ 測驗 C++ 證書


C++ cout 物件

❮ iostream 物件


示例

使用 cout 物件輸出不同資料型別

string myStr = "Hello World!";
bool myBool = false;
int myInt = 50;
float myFloat = 19.99;

cout << myStr << "\n";
cout << myBool << "\n";
cout << myInt << "\n";
cout << myFloat << "\n";

自己動手試一試 »


定義和用法

The cout object is used to output values/print text.

The cout object is used to output values/print text.

cout << "Hello World!";

自己動手試一試 »

The insertion operator can be used more than once on the same line to output multiple values

cout << "The answer is: " << x;

自己動手試一試 »

注意: cout 物件定義在 <iostream> 標頭檔案中。


運算子

Manipulators allow you to change the formatting of the output. They are used with the << insertion operator in the same way as literal values and variables, and they affect output that follows them.

除了 setw(),運算子的效果會一直保持,直到另一個運算子改變它。

The table below shows a list of useful manipulators

運算子 描述 示例
boolalpha Displays boolean values as "true" and "false" instead of "1" and "0". cout << boolalpha << false;
dec 將整數表示為十進位制數字。 cout << dec << 12;
endl Outputs a newline character. This manipulator also flushes the output buffer which makes it less efficient than printing \n. cout << "Line 1" << endl << "Line 2";
ends Outputs the null terminating character used to end C-style strings. Mainly used when writing into files. cout << "Hello World!" << ends;
fixed 用固定的小數位數表示浮點數。小數位數可以使用 setprecision() 運算子來確定。 cout << fixed << 19.99;
hex 將整數表示為十六進位制數字。 cout << hex << 12;
internal 如果指定了寬度(使用 setw() 運算子),數字的符號將左對齊,而值將右對齊,其他資料型別的輸出將右對齊。 cout << setw(10) << internal << -12345;
left 如果指定了寬度(使用 setw() 運算子),則將輸出左對齊。 cout << setw(10) << left << "Hello";
noboolalpha 用於重置由 boolalpha 運算子所做的更改。 cout << noboolalpha << false;
noshowbase 用於重置由 showbase 運算子所做的更改。 cout << hex << noshowbase << 12;
noshowpoint 用於重置由 showpoint 運算子所做的更改。 cout << noshowpoint << 12345.0;
noshowpos 用於重置由 showpos 運算子所做的更改。 cout << noshowpos << 12;
nouppercase 用於重置由 uppercase 運算子所做的更改。 cout << hex << nouppercase << 12;
oct 將整數表示為八進位制數字。 cout << oct << 12;
right 如果指定了寬度(使用 setw() 運算子),則將輸出右對齊。 cout << setw(10) << right << "Hello";
fixed 用科學記數法表示浮點數。小數位數可以使用 setprecision() 運算子來確定。 cout << fixed << 19.99;
setfill() 選擇一個字元用作填充。
需要 <iomanip> 庫。
cout << setfill('.') << setw(10) << 19.99;
setprecision() 選擇浮點數的精度。如果使用了 fixedscientific 運算子,它指定小數位數,否則它指定有效數字的數量。
需要 <iomanip> 庫。
cout << setprecision(4) << 12.3456;
setw() 指定下一個輸出的最小字元寬度。如果輸出不夠寬,則新增填充以填充剩餘空間。
需要 <iomanip> 庫。
cout << setw(10) << "Hello";
showbase 將整數表示為十六進位制或八進位制時,在數字前加上 "0x" 或 "0" 以顯示其基數。 cout << hex << showbase << 12;
showpoint Always displays the decimal point for floating point numbers even if it is not needed. cout << showpoint << 12345.0;
showpos Always displays a + sign next to positive numbers. cout << showpos << 12;
uppercase 用大寫字母表示十六進位制數字和科學記數法中的 "e"。 cout << hex << uppercase << 12;

示例

Use manipulators to change how output is formatted

// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout << boolalpha << false << "\n";

// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << dec << myInt << "\n";
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << showbase << uppercase;
cout << hex << myInt << "\n";
cout << oct << myInt << "\n";
cout << dec;

// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout << showpos << showpoint << 12345.0 << "\n";
cout << noshowpos << noshowpoint;
cout << fixed << myFloat << "\n";
cout << scientific << myFloat << "\n";

// Alignment
cout << "\nAlignment\n";
cout << setw(10) << left << "Left" << "\n";
cout << setw(10) << right << "Right" << "\n";
cout << setw(10) << internal << -12345 << " (Internal)\n";

自己動手試一試 »


方法

The cout object also has methods which can do the same operations as the << insertion operator.

Output methods

The cout.write(str, n) method outputs the first n characters from the char array str without any formatting.

示例

char myStr[] = "Hello World!";
cout.write(myStr, 5);

自己動手試一試 »

The cout.put(c) method outputs the specified character c without any formatting.

示例

char grade = 'B';
cout.put(grade);

自己動手試一試 »

Formatting methods

The cout.precision(p) method specifies how many digits are used to represent floating point numbers. By default it specifies the number of significant digits to display. If the ios::fixed or ios::scientific flag is enabled then it specifies how many digits follow the decimal point.

示例

cout.precision(4);
cout << 12.3456;

自己動手試一試 »

The cout.width(w) method specifies the minimum number of characters wide the next output should occupy. If the output does not have enough characters then padding characters will be added to fill up the remaining space. By default the padding characters are spaces and they are added to the left so that the content is aligned to the right. The alignment can be changed by using one of the ios::adjustfield flags described in the Flags section below.

示例

cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";

自己動手試一試 »

The cout.fill(c) method specifies which character will be used as padding.

示例

cout.fill('.');
cout.width(10);
cout << 5 << "\n";
cout.width(10);
cout << 25 << "\n";
cout.width(10);
cout << 125 << "\n";

自己動手試一試 »

Flags

The setf() and unsetf() methods are used to set or unset flags which change the formatting of the output. There are a variety of different flags. Some flags belong to a group and, in that case, the setf() method should have a second parameter specifying which group it belongs to so that the other flags of the group can be reset.

Flag 語法 描述
ios::boolalpha cout.setf(ios::boolalpha) Displays boolean values as "true" and "false" instead of "1" and "0".
ios::showbase cout.setf(ios::showbase) 將整數表示為十六進位制或八進位制時,在數字前加上 "0x" 或 "0" 以顯示其基數。
ios::showpoint cout.setf(ios::showpoint) Always displays the decimal point for floating point numbers even if it is not needed.
ios::showpos cout.setf(ios::showpoint) Always displays a + sign next to positive numbers.
ios::uppercase cout.setf(ios::uppercase) 用大寫字母表示十六進位制數字和科學記數法中的 "e"。
ios::dec cout.setf(ios::dec, ios::basefield) Represents integers as decimal digits. Belongs to the ios::basefield group.
ios::hex cout.setf(ios::hex, ios::basefield) Represents integers as hexadecimal digits. Belongs to the ios::basefield group.
ios::oct cout.setf(ios::oct, ios::basefield) Represents integers as octal digits. Belongs to the ios::basefield group.
ios::fixed cout.setf(ios::fixed, ios::floatfield) Represents floating point numbers with a fixed number of decimal places. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group.
ios::scientific cout.setf(ios::scientific, ios::floatfield) Represents floating point numbers in scientific notation. The number of decimal places can be established with the cout.precision() method. Belongs to the ios::floatfield group.
ios::internal cout.setf(ios::internal, ios::adjustfield) If a width is specified, for numbers the sign is left-aligned while the value is right-aligned, for other data types the output is aligned to the right. Belongs to the ios::adjustfield group.
ios::left cout.setf(ios::left, ios::adjustfield) Aligns output to the left when a width is specified. Belongs to the ios::adjustfield group.
ios::right cout.setf(ios::right, ios::adjustfield) Aligns output to the right when a width is specified. Belongs to the ios::adjustfield group.

示例

Use flags to change how output is formatted

// Booleans
cout << "Booleans\n";
cout << false << "\n";
cout.setf(ios::boolalpha);
cout << false << "\n";

// Hexadecimal and octal numbers
cout << "\nHexadecimal and octal numbers\n";
int myInt = 14;
cout << myInt << "\n";
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::showbase);
cout.setf(ios::uppercase);
cout.setf(ios::hex, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::oct, ios::basefield);
cout << myInt << "\n";
cout.setf(ios::dec, ios::basefield);

// Floating point numbers
cout << "\nFloating point numbers\n";
float myFloat = 19.99;
cout << myFloat << "\n";
cout.setf(ios::fixed, ios::floatfield);
cout << myFloat << "\n";
cout.setf(ios::scientific, ios::floatfield);
cout << myFloat << "\n";
cout.unsetf(ios::floatfield);
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout << 12345.0 << "\n";

// Alignment
cout << "\nAlignment\n";
cout.setf(ios::left, ios::adjustfield);
cout << setw(10) << "Left" << "\n";
cout.setf(ios::right, ios::adjustfield);
cout << setw(10) << "Right" << "\n";
cout.setf(ios::internal, ios::adjustfield);
cout << setw(10) << 12345.0 << " (Internal)\n";

自己動手試一試 »


❮ iostream 物件

×

聯絡銷售

如果您想將 W3Schools 服務用於教育機構、團隊或企業,請傳送電子郵件給我們
sales@w3schools.com

報告錯誤

如果您想報告錯誤,或想提出建議,請傳送電子郵件給我們
help@w3schools.com

W3Schools 經過最佳化,旨在方便學習和培訓。示例可能經過簡化,以提高閱讀和學習體驗。教程、參考資料和示例會不斷審查,以避免錯誤,但我們無法保證所有內容的完全正確性。使用 W3Schools 即表示您已閱讀並接受我們的使用條款Cookie 和隱私政策

版權所有 1999-2024 Refsnes Data。保留所有權利。W3Schools 由 W3.CSS 提供支援