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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。