字符串常量与字符串变量 字符串与其他数据类型的任意转换
2011-02-28 17:59
555 查看
在VC++6.0中,我们这样定义两个字符串常量
char* sz1 = "hello1";
char* sz2 = "hello1";
经过跟踪,我们发现两个char*指针指向的是同一个地址,并且地址位置为不可改写的静态内存区域。
那么如何能定义这个的字符串呢,我们可以定义变量如下:
char charAry[] = "hello1";
char charAry[] = "hello1";
这样,就可以对这个字符串进行修改了。我们把这种作为变量。
char*声明方式存储于全局数据区,char存储于栈中
用标准C++ 模拟csharp的tostring()的实现,转换各种数据类型到字符串类型
要实现这个目标,非stringstream类莫属。这个类在<sstream>头文件中定义,
<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。简单起见,我主要以stringstream为中心,因为每个转换都要涉及到输入和输出操作。示例1示范怎样使用一个stringstream对象进行从
string到int类型的转换
注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
示例1:
std::stringstream stream;
string result="10000";
int n = 0;
stream << result;
stream >> n;//n等于10000
int到string类型的转换
string result;
int n = 12345;
stream << n;
result =stream.str();// result等于"12345"
示例2:
std::stringstream strsql;
for (int i= 1; i < 10; ++i)
{
strsql << "insert into test_tab values(";
strsql << i << ","<< (i+10) << ");";
std::string str = strsql.str(); // 得到string
res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
std::cout << strsql.str() << std::endl;
strsql.clear();
strsql.str("");
}
转换中使用模板
也可以轻松地定义函数模板来将一个任意的类型转换到特定的目标类型。例如,需要将各种数字值,如int、long、double等等转换成字符串,要使用以一个string类型和一个任意值t为参数的to_string()函数。to_string()函数将t转换为字符串并写入result中。使用str()成员函数来获取流内部缓冲的一份拷贝:
示例3:
template<class T>
void to_string(string & result,const T& t)
{
ostringstream oss;//创建一个流
oss<<t;//把值传递如流中
result=oss.str();//获取转换后的字符转并将其写入result
}
这样,你就和衣轻松地将多种数值转换成字符串了:
to_string(s1,10.5);//double到string
to_string(s2,123);//int到string
to_string(s3,true);//bool到string
可以更进一步定义一个通用的转换模板,用于任意类型之间的转换。函数模板convert()含有两个模板参数out_type和in_value,功能是将in_value值转换成out_type类型:
template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
这样使用convert():
double d;
string salary;
string s=”12.56”;
d=convert<double>(s);//d等于12.56
salary=convert<string>(9000.0);//salary等于”9000”
结论
在过去留下来的程序代码和纯粹的C程序中,传统的<stdio.h>形式的转换伴随了我们很长的一段时间。但是,如文中所述,基于stringstream的转换拥有类型安全和不会溢出这样抢眼的特性,使我们有充足得理由抛弃<stdio.h>而使用<sstream>。
当然现在还有一个更好的选择,那就是使用boost库中的lexical_cast,它是类型安全的转换。如下例:
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
int main(void)
try{
//以下是内置类型向string转换的解决方案
//lexical_cast优势明显
int ival;
char cval;
ostringstream out_string;
string str0;
string str1;
ival = 100;
cval = 'w';
out_string << ival << " " << cval;
str0 = out_string.str();
str1 = lexical_cast<string>(ival)
+ lexical_cast<string>(cval);
cout << str0 << endl;
cout << str1 << endl;
//以下是string向内置类型转换的解决方案
//几乎和stringstrem相比,lexical_cast就是类型安全的,
int itmpe;
char ctmpe;
str0 = "100k";
str1 = "100h";
istringstream in_string( str0 );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
itmpe = lexical_cast<int>(str1);
ctmpe = lexical_cast<char>(str1);
system( "PAUSE" );
return 0;
}
catch(bad_lexical_cast e)
{
cout << e.what() << endl;
cin.get();
}
char* sz1 = "hello1";
char* sz2 = "hello1";
经过跟踪,我们发现两个char*指针指向的是同一个地址,并且地址位置为不可改写的静态内存区域。
那么如何能定义这个的字符串呢,我们可以定义变量如下:
char charAry[] = "hello1";
char charAry[] = "hello1";
这样,就可以对这个字符串进行修改了。我们把这种作为变量。
char*声明方式存储于全局数据区,char存储于栈中
用标准C++ 模拟csharp的tostring()的实现,转换各种数据类型到字符串类型
要实现这个目标,非stringstream类莫属。这个类在<sstream>头文件中定义,
<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。简单起见,我主要以stringstream为中心,因为每个转换都要涉及到输入和输出操作。示例1示范怎样使用一个stringstream对象进行从
string到int类型的转换
注意,<sstream>使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。
示例1:
std::stringstream stream;
string result="10000";
int n = 0;
stream << result;
stream >> n;//n等于10000
int到string类型的转换
string result;
int n = 12345;
stream << n;
result =stream.str();// result等于"12345"
示例2:
std::stringstream strsql;
for (int i= 1; i < 10; ++i)
{
strsql << "insert into test_tab values(";
strsql << i << ","<< (i+10) << ");";
std::string str = strsql.str(); // 得到string
res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
std::cout << strsql.str() << std::endl;
strsql.clear();
strsql.str("");
}
转换中使用模板
也可以轻松地定义函数模板来将一个任意的类型转换到特定的目标类型。例如,需要将各种数字值,如int、long、double等等转换成字符串,要使用以一个string类型和一个任意值t为参数的to_string()函数。to_string()函数将t转换为字符串并写入result中。使用str()成员函数来获取流内部缓冲的一份拷贝:
示例3:
template<class T>
void to_string(string & result,const T& t)
{
ostringstream oss;//创建一个流
oss<<t;//把值传递如流中
result=oss.str();//获取转换后的字符转并将其写入result
}
这样,你就和衣轻松地将多种数值转换成字符串了:
to_string(s1,10.5);//double到string
to_string(s2,123);//int到string
to_string(s3,true);//bool到string
可以更进一步定义一个通用的转换模板,用于任意类型之间的转换。函数模板convert()含有两个模板参数out_type和in_value,功能是将in_value值转换成out_type类型:
template<class out_type,class in_value>
out_type convert(const in_value & t)
{
stringstream stream;
stream<<t;//向流中传值
out_type result;//这里存储转换结果
stream>>result;//向result中写入值
return result;
}
这样使用convert():
double d;
string salary;
string s=”12.56”;
d=convert<double>(s);//d等于12.56
salary=convert<string>(9000.0);//salary等于”9000”
结论
在过去留下来的程序代码和纯粹的C程序中,传统的<stdio.h>形式的转换伴随了我们很长的一段时间。但是,如文中所述,基于stringstream的转换拥有类型安全和不会溢出这样抢眼的特性,使我们有充足得理由抛弃<stdio.h>而使用<sstream>。
当然现在还有一个更好的选择,那就是使用boost库中的lexical_cast,它是类型安全的转换。如下例:
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
int main(void)
try{
//以下是内置类型向string转换的解决方案
//lexical_cast优势明显
int ival;
char cval;
ostringstream out_string;
string str0;
string str1;
ival = 100;
cval = 'w';
out_string << ival << " " << cval;
str0 = out_string.str();
str1 = lexical_cast<string>(ival)
+ lexical_cast<string>(cval);
cout << str0 << endl;
cout << str1 << endl;
//以下是string向内置类型转换的解决方案
//几乎和stringstrem相比,lexical_cast就是类型安全的,
int itmpe;
char ctmpe;
str0 = "100k";
str1 = "100h";
istringstream in_string( str0 );
in_string >> itmpe >> ctmpe;
cout << itmpe << " " << ctmpe << endl;
itmpe = lexical_cast<int>(str1);
ctmpe = lexical_cast<char>(str1);
system( "PAUSE" );
return 0;
}
catch(bad_lexical_cast e)
{
cout << e.what() << endl;
cin.get();
}
相关文章推荐
- 17.2015.08.04第十八节课 C#2 (数值类型及调用、引用类型及调用、装拆箱、常量、变量、数据类型转换、算术运算符、赋值运算符、关系运算符、逻辑运算符、字符串的常用方法)
- 数据类型转字符串及CString与其他数据类型的转换
- Go语言中其他数据与字符串类型的转换
- 将字符串数据转换为其他类型
- JSON: Python Objects与String之间转换【json.dumps把python任意数据类型转换成字符串,json.loads把字符串转换为unicode编码】
- 字符串数据类型与其他类型之间的转换
- Swift3字符串转换为其他数据类型
- Go语言中其他数据与字符串类型的转换
- Java下枚举变量与其他数据类型的转换
- Java将其他数据类型转换成JSON字符串格式
- 字符串类型与其他数据类型的转换
- [bxd学习java基本点]1.基本数据类型与字符串型转换与其他类型转成字符串
- Swift3字符串转换为其他数据类型
- 句柄转化为字符串 与 其他数据类型转换为字符串
- java中字符串和其他类型之间的互相转换
- leetcode_345. Reverse Vowels of a String 转换字符串中的元音字符,list类型数据转换成str类型,join()函数
- php中变量的数据类型转换
- time_t、struct tm,ctime各数据类型、函数详解,转换以及跟时间字符串的转换
- CString 与其他数据类型的转换