//#include <afx.h>
#include <assert.h>
#include <iostream>
#include <sstream> //using stringstream
#include <string.h>
#include <vector>
using namespace std;
typedef basic_string<char>::size_type S_T;
static const S_T npos = -1;
vector<string> split(const string& src, string delimit, string null_subst="")
{
if( src.empty() || delimit.empty() )
throw "split: empty string\0";
vector<string> v;
S_T deli_len = delimit.size();
long index = npos, last_search_position = 0;
while( (index=src.find(delimit,last_search_position))!=npos )
{
if(index==last_search_position)
v.push_back(null_subst);
else
v.push_back( src.substr(last_search_position, index- last_search_position) );
last_search_position = index + deli_len;
}
string last_one = src.substr(last_search_position);
v.push_back( last_one.empty()? null_subst:last_one );
return v;
}
string cvt(const float f, const int prec)
{
stringstream ss;
ss.precision(prec);
ss << f;
return ss.str();
}
int main()
{
int a[10];
int i,j;
for (i = 0; i < 10; i ++)
{
a[i] = (float)i;
}
/* unsigned char *pa = NULL;
int charLen = sizeof(int) * 10;
unsigned char *a_char = new char [charLen];
memset(a_char,0,charLen);
int elemLen = sizeof(int);
int index = 0;
//int --> char
for (i = 0; i < 10; i ++)
{
pa = (char*)a[i];
for (j = 0; j < elemLen; j ++)
{
assert(index < charLen);
a_char[index] = *pa;
pa ++;
index ++;
}
}
*/
string str;
char buffer[20];
str = "";
for (i = 0; i < 10; i ++)
{
_itoa(a[i],buffer,20);//int --> string
string strmid(buffer);
str += strmid;
if(i != 9)
str += '+';
}
cout<<str.c_str();
//split
try
{
string del("+");
vector<string> v3 = split(str, del, "<null>");
for(int k=0; k<v3.size();k++)
{
cout<<v3[k].c_str()<<endl;
}
}
catch (char *s) {
cout<<s<<endl;
}
//char buffer[50];
double source = -3.1415e5;
// float source = 100.114f;
_gcvt( source, 7, buffer );//float --> string
printf( "source: %f buffer: '%s'\n", source, buffer );
_gcvt( source, 7, buffer );
printf( "source: %e buffer: '%s'\n", source, buffer );
char str1[260];
float f = 3.14159;
float f1 = 263e+5;
sprintf(str1,"%f",f);
printf("%s\n",str1);
sprintf(str1,"%f",f1);
printf("%s\n",str1);
string tmp1;
tmp1 = cvt(f,7);
cout<<tmp1.c_str()<<endl;
//string --> float
// char str2[]="111.1000";
// char str2[]="1e+5";
string str2("1e+5");
float f2;
f2=atof(str2.c_str());
printf("%f\n",f2);
return 0;
}
/************************************************************************/
/* 从int 到string的几种方法
1. int sprintf( char *buffer, const char *format [, argument] ... );
<stdio.h>
例如:
int ss;
char temp[64];
string str;
ss = 1000;
sprintf(temp, "%d", ss);
string s(temp);
//调用string的方法
cout<<s.c_str()<<endl;//1000
cout<<s.size()<<endl; //长度为4
2.char *_itoa( int value, char *string, int radix );
<stdlib.h>
例如:
char buffer[20];
int i = 3445;
_itoa( i, buffer, 10 );
string s(buffer);
3. stringstream( )
<sstream.h>
例如:
int hello=4;
stringstream ss;
ss<<hello;
string s=ss.str();
//调用string的方法
cout<<s.c_str()<<endl;
*/
/************************************************************************/
没有合适的资源?快使用搜索试试~ 我知道了~
float_char_int_string.rar_char float_char int 转换_char to string_
共8个文件
ncb:1个
opt:1个
txt:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 78 浏览量
2022-09-23
07:53:47
上传
评论
收藏 106KB RAR 举报
温馨提示
各种基本数据类型转换源程序。 本程序涉及到的数据类型有:float, int , char, string.
资源推荐
资源详情
资源评论
收起资源包目录
float_char_int_string.rar (8个子文件)
www.pudn.com.txt 218B
float char int string
interchange.dsp 3KB
interchange.exe 544KB
interchange.plg 256B
interchange.cpp 4KB
interchange.dsw 547B
interchange.opt 48KB
interchange.ncb 41KB
共 8 条
- 1
资源评论
我虽横行却不霸道
- 粉丝: 72
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Docker容器配置进阶
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功