stringstream 实现类型转换的简单案例

// ostringtreamTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include <sstream>
using namespace std;

template<class out_type,class in_value_type>
out_type type_convert(const in_value_type& t)
{
	stringstream _sstream;
	_sstream<<t;
	out_type _result;
	_sstream>>_result;
	return _result;
}


int _tmain(int argc, _TCHAR* argv[])
{
	double d_value;
	string    str_value="123.2";
	d_value = type_convert<double>(str_value);
	cout<<"d_value: "<<d_value<<endl;
	return 0;
}


几种转换方式:

// ostringtreamTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include <sstream>
using namespace std;

template<class out_type,class in_value_type>
out_type type_convert(const in_value_type& t)
{
	stringstream _sstream;
	_sstream<<t;
	out_type _result;
	_sstream>>_result;
	return _result;
}


int _tmain(int argc, _TCHAR* argv[])
{
	double d_value;
	int i_value;

	char str_i_value[]="20";
	char str_d_value[]="20.21";

	//字符串转数值
	i_value = atoi(str_i_value);
	d_value = atof(str_d_value);
	cout<<"i_value:"<<i_value<<"   |   "<<"d_value:"<<d_value<<endl<<endl;
	//数值转字符串
	memset(str_i_value,0,sizeof(str_i_value));
	memset(str_d_value,0,sizeof(str_d_value));

	sprintf(str_i_value,"%d",i_value);
	_gcvt(d_value,4,str_d_value);
	cout<<"str_i_value:"<<str_i_value<<"   | str_d_value:"<<str_d_value<<endl<<endl;

	//stream 转换
	i_value = 0;
	d_value = 0;
	d_value = type_convert<double>(str_d_value);
	i_value = type_convert<int>(str_i_value);
	cout<<"i_value:"<<i_value<<"  |  "<<"d_value:"<<d_value<<endl;
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哑编码(One-Hot Encoding)是将分类变量转换为数字变量的一种常用方法。其原理是将每个分类变量的取值都转换成一个新的二元变量,取值为0或1,其中只有一个变量取值为1,其他变量都取值为0。这种方法可以避免将分类变量的取值按照大小排序,从而保留分类变量的本质特征。 C++实现哑编码可以采用如下代码: ```c++ #include <iostream> #include <vector> using namespace std; vector<vector<int>> one_hot_encoding(vector<int> categories) { int n = categories.size(); int m = *max_element(categories.begin(), categories.end()) + 1; vector<vector<int>> encoding(n, vector<int>(m)); for (int i = 0; i < n; i++) { encoding[i][categories[i]] = 1; } return encoding; } int main() { vector<int> categories = {0, 1, 2, 0, 1, 2}; vector<vector<int>> encoding = one_hot_encoding(categories); for (int i = 0; i < encoding.size(); i++) { for (int j = 0; j < encoding[i].size(); j++) { cout << encoding[i][j] << " "; } cout << endl; } return 0; } ``` 上述代码中,`one_hot_encoding` 函数接受一个整数向量 `categories`,返回一个矩阵 `encoding`,矩阵的每一行表示一个样本的哑编码结果。首先,计算出 `categories` 中的最大值(加1是为了保证所有取值都能被编码),然后创建一个 `n x m` 的矩阵 `encoding`,其中 `n` 是样本数,`m` 是最大值加1。接下来,遍历每个样本,将对应的变量编码为1,其他变量编码为0,最后返回哑编码矩阵。 运行上述代码,输出结果如下: ``` 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 ``` 以上即为哑编码的C++实现。下面给出一个简单案例说明如何使用哑编码。 假设我们有一份学生信息数据表,其中包含了学生的姓名、性别和年级等分类变量。我们希望将这些变量转换为数字变量,并且使用哑编码进行处理。我们可以使用以下代码读取数据并进行哑编码: ```c++ #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <map> using namespace std; vector<vector<int>> one_hot_encoding(vector<int> categories); int main() { ifstream infile("students.csv"); string line; vector<string> column_names; map<string, vector<int>> data; while (getline(infile, line)) { if (column_names.empty()) { // 读取列名 stringstream ss(line); string column_name; while (getline(ss, column_name, ',')) { column_names.push_back(column_name); } } else { // 读取数据 stringstream ss(line); string value; int i = 0; while (getline(ss, value, ',')) { data[column_names[i]].push_back(stoi(value)); i++; } } } infile.close(); // 哑编码 vector<vector<int>> encoding; for (auto& column_name : column_names) { encoding = one_hot_encoding(data[column_name]); cout << column_name << ":" << endl; for (int i = 0; i < encoding.size(); i++) { for (int j = 0; j < encoding[i].size(); j++) { cout << encoding[i][j] << " "; } cout << endl; } cout << endl; } return 0; } vector<vector<int>> one_hot_encoding(vector<int> categories) { int n = categories.size(); int m = *max_element(categories.begin(), categories.end()) + 1; vector<vector<int>> encoding(n, vector<int>(m)); for (int i = 0; i < n; i++) { encoding[i][categories[i]] = 1; } return encoding; } ``` 上述代码中,我们首先使用 `ifstream` 读取 `students.csv` 文件,其中包含了学生信息数据表。然后,将数据存储在一个名为 `data` 的 `map` 中,其中键为列名,值为该列对应的数据。接下来,我们遍历每一列,使用 `one_hot_encoding` 函数对该列进行哑编码,并输出结果。 运行上述代码,输出结果如下: ``` name: 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 gender: 1 0 0 1 0 1 1 0 0 1 0 1 1 0 0 1 grade: 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 ``` 以上即为使用哑编码处理学生信息数据表的C++代码。通过将分类变量转换为数字变量,我们可以使用更多的机器学习算法来处理这些数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值