<iostream>he<iostream.h>的区别

本文解析了C++中&lt;iostream&gt;与&lt;iostream.h&gt;的区别,详细介绍了命名空间std的作用及其使用方法,帮助读者理解如何正确地使用C++标准库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 :

<iostream>和<iostream.h>是不一样,前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。

后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。

因此,当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用<iostream>的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。

二:

所谓namespace,是指标识符的各种可见范围。

C++标准程序库中的所有标识符都被定义于一个名为std的namespace中。

由于namespace的概念,使用C++标准程序库的任何标识符时,可以有三种选择:

1、直接指定标识符。例如std::ostream而不是ostream。完整语句如下:

std::cout << std::hex << 3.4 << std::endl;

2、使用using关键字。

using std::cout;
using std::endl;

以上程序可以写成

cout << std::hex << 3.4 << endl;

3、最方便的就是使用using namespace std;

例如:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写:

cout << hex << 3.4 << endl;

因为标准库非常的庞大,所程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。所以为了避免这种情况所造成的名字冲突,就把标准库中的一切都被放在名字空间std中。但这又会带来了一个新问题。无数原有的C++代码都依赖于使用了多年的伪标准库中的功能,他们都是在全局空间下的。  

         所以就有了<iostream.h>和<iostream>等等这样的头文件,一个是为了兼容以前的C++代码,一个是为了支持新的标准。

命名空间std封装的是标准程序库的名称,标准程序库为了和以前的头文件区别,一般不加".h"

参考以上文件,用C语言神经网络模型,编写代码,预测成熟度。改成一下几种特征firmness hue saturation brightness color_category sound_db weight_g size_cm3 最后成熟度有5种程度 #include <iostream> #include "rapidcsv.h" #include <opencv2/opencv.hpp> using namespace std; using namespace cv; using namespace cv::ml; typedef struct { double x1,x2,x3,x4; //嘴峰长度、嘴峰深度、脚掌长度、 体重 int x5_0,x5_1; //是否male(1:male,0:female);是否famale(1:female,0:male) int label; //类别标签, 0:Adelie,1:Gentoo,2: Chinstrap } DataPoint; int ReadCsv(const char* fileName, DataPoint data[]) { rapidcsv::Document doc(fileName, rapidcsv::LabelParams(), rapidcsv::SeparatorParams(),rapidcsv::ConverterParams(true));//解析csv文件 int j=0;//样本计数 for (int i=0;i<doc.GetRowCount();i++){ //每次处理一行 double bill_length_mm = doc.GetCell<double>("bill_length_mm", i);//嘴峰长度 double bill_depth_mm = doc.GetCell<double>("bill_depth_mm", i);//嘴峰深度 double flipper_length_mm = doc.GetCell<double>("flipper_length_mm", i);//脚掌长度 double body_mass_g = doc.GetCell<double>("body_mass_g", i);//体重 std::string sex = doc.GetCell<std::string>("sex", i); //性别 std::string type_name = doc.GetCell<std::string>("species", i); //种族 if ( !std::isnan(bill_length_mm) && !std::isnan(bill_depth_mm) && !std::isnan(flipper_length_mm) && !std::isnan(body_mass_g) && sex!="NA" ){//处理缺失值 data[j].x1 = bill_length_mm;data[j].x2 = bill_depth_mm; data[j].x3 = flipper_length_mm;data[j].x4 = body_mass_g; data[j].x5_0 = sex=="male"?1:0; data[j].x5_1 = sex=="female"?1:0; if (type_name == "Adelie") data[j].label=0; else if (type_name == "Gentoo") data[j].label=1; else data[j].label=2; j++;//剔除缺失值后的样本数目 } } return j; } int main() { DataPoint data[500]; int n = ReadCsv("penguins.csv", data);//从文件读入数据 //将数据拷贝到Mat矩阵中 , 75%训练数据,25%测试数据 int n1=n*3/4, n2=n-n1; Mat train_samples(n1, 6, CV_32F); //训练数据集 Mat train_labels(n1, 3, CV_32F);//训练数据的标签(独热编码) Mat test_samples(n2, 6, CV_32F); //测试数据集 Mat test_labels(n2, 1, CV_32S);//测试数据的标签 int i=0, j=0, k=0; for(i = 0; i<n; i++){ if (i%4==0){//每3个训练数据搭配1个测试数据 test_samples.at<float>(j, 0) = data[i].x1;test_samples.at<float>(j, 1) = data[i].x2; test_samples.at<float>(j, 2) = data[i].x3;test_samples.at<float>(j, 3) = data[i].x4; test_samples.at<float>(j, 4) = data[i].x5_0;test_samples.at<float>(j, 5) = data[i].x5_1; test_labels.at<int>(j, 0) = data[i].label; j++; }else{ train_samples.at<float>(k, 0) = data[i].x1;train_samples.at<float>(k, 1) = data[i].x2; train_samples.at<float>(k, 2) = data[i].x3;train_samples.at<float>(k, 3) = data[i].x4; train_samples.at<float>(k, 4) = data[i].x5_0;train_samples.at<float>(k, 5) = data[i].x5_1; if (data[i].label==0) {//标签转换为独热编码 train_labels.at<float>(k, 0)=1;train_labels.at<float>(k, 1)=0;train_labels.at<float>(k, 2)=0; }else if (data[i].label==1) { train_labels.at<float>(k, 0)=0;train_labels.at<float>(k, 1)=1;train_labels.at<float>(k, 2)=0; }else{ train_labels.at<float>(k, 0)=0;train_labels.at<float>(k, 1)=0;train_labels.at<float>(k, 2)=1; } k++; } } cout<<"n="<<n<<",n1="<<n1<<",n2="<<n2<<endl; Ptr<ANN_MLP> nn = ANN_MLP::create(); //创建神经网络模型 nn->setLayerSizes(vector<int>{6, 10, 3});//输入层6个节点,隐藏层10个节点,输出层3个节点(假设3类企鹅) nn->setActivationFunction(ANN_MLP::SIGMOID_SYM);//激活函数目前只支持Sigmoid nn->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER, 10000, 1e-5));//设置迭代次数与收敛条件 nn->setTrainMethod(ANN_MLP::RPROP); //弹性反向传播算法 nn->train(train_samples, ROW_SAMPLE, train_labels);//训练模型 Mat predictions; //预测结果 nn->predict(test_samples,predictions);//对测试集进行预测 int correct = 0; //正确分类的次数 for (int i = 0; i < predictions.rows; i++){ Point max_loc; // 找到预测值中的最大索引,即预测的类别 minMaxLoc(predictions.row(i), nullptr, nullptr, nullptr, &max_loc); cout<<"predictions:"<<predictions.row(i)<<", max_loc.x:"<<max_loc.x<<", label:"<<test_labels.at<int>(i, 0)<<endl; if (max_loc.x == test_labels.at<int>(i, 0)) correct++; } double accuracy = (double)correct / predictions.rows; //计算准确率 cout<<"Accuracy: " << accuracy * 100 << "%" << endl; return 0; }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值