您好,登錄后才能下訂單哦!
C++ 流庫(iostream)是一個功能強大的輸入/輸出庫,它提供了格式化輸入/輸出功能,支持各種數據類型
#include<iostream>
#include<string>
int main() {
int age;
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Hello, "<< name << "! You are "<< age << " years old."<< std::endl;
return 0;
}
#include<iostream>
#include <fstream>
#include<vector>
int main() {
std::ifstream input_file("data.txt");
std::vector<double> numbers;
double number;
if (!input_file) {
std::cerr << "Error opening file."<< std::endl;
return 1;
}
while (input_file >> number) {
numbers.push_back(number);
}
input_file.close();
double sum = 0;
for (const auto &num : numbers) {
sum += num;
}
double average = sum / numbers.size();
std::cout << "The average of the numbers in the file is: "<< average<< std::endl;
return 0;
}
#include<iostream>
#include <fstream>
int main() {
std::ofstream output_file("output.txt");
if (!output_file) {
std::cerr << "Error opening file."<< std::endl;
return 1;
}
std::string text = "Hello, World!";
output_file<< text<< std::endl;
output_file.close();
std::cout << "Text has been written to the file."<< std::endl;
return 0;
}
這些實例展示了 C++ 流庫的基本用法。你可以根據需要修改和擴展這些代碼以滿足你的需求。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。