2009-08-19 10:54
C++ int,char,string,CString类型转换
#include <string> //使用C++标准库的string类时
using namespace std; //同上
#include <sstream>
#include <iostream>
#include <> //要将string类和int类型直接转换最好有这些包含,
//因为自己写一个转换函数比较方便,函数定义参考如下
string getstring ( const int n )
{
std::stringstream newstr;
newstr<<n;
return ();
}
string 转 CString
(”%s”, ());
char 转 CString
(”%s”, char*);
char 转 string
string s(char *);
string 转 char *
char *p = ();
CString 转 string
string s(());
1,string -> CString
(”%s”, ());
用c_str()确实比data()要好.
2,char -> string
string s(char *);
只能初始化,在不是初始化的地方最好还是用assign().
3,CString -> string
string s(());
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.
《C++标准函数库》中说的
有三个函数可以将字符串的内容转换为字符数组和C—string
(),返回没有”\0“的字符串数组
2,c_str(),返回有”\0“的字符串数组
3,copy()
—————————————————————
CString与int、char*、char[100]之间的转换- -
CString与int、char*、char[100]之间的转换- -
CString互转int
将字符转换为整数,可以使用atoi、_atoi64或atol。
而将数字转换为CString变量,可以使用CString的Format函数。如
CString s;
int i = 64;
(”%d”, i)
Format函数的功能很强,值得你研究一下。
void CStrDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString
ss=”121
int,char,string,CString类型转换 来自淘豆网m.daumloan.com转载请标明出处.