c++ const char * c_str()

  头文件:#include <string>


const char* c_str();

The function c_str() returns a const pointer to a regular C string, identical to the current string. The returned string is null-terminated.(返回的字符串以/0结尾—这是c语言中字符串的默认结尾符,所以该函数是将c++中的字符串string格式的变量转化为c中的字符串格式)

Note that since the returned pointer is of type const, the character data that c_str() returns cannot be modified. Furthermore, you do not need to call free() or delete on this pointer.

因为c_str函数的返回值是const char*的,不能直接赋值給char*。

c_str()返回一个客户程序可读不可改(因返回值是const char*)的指向字符数组的指针,不需要手动释放或删除这个指针。

+++++++++++++++++++++++++++++++++

Visual C++ Standard Library
basic_string::c_str

Converts the contents of a string as a C-style, null-terminated string.(返回以"/0"结尾的C-串,标准C中的字符串格式)


Return Value
A pointer to the C-style version of the invoking string. The pointer value is not valid after calling a non-const function, including the destructor, in the basic_string class on the object.

Remarks
Objects of type string belonging to the C++ template class basic_string<char>are not necessarily null terminated. The null character' /0 ' is used as a special character in a C-string to mark the end of the string but has no special meaning in an object of type string and may be a part of the string just like any other character.There is an automatic conversion from const char* into strings,but the string class does not provide for automatic conversions from C-style strings to objects of type basic_string<char>.

The returned C-style string should not be modified, as this could invalidate the pointer to the string, or deleted, as the string has a limited lifetime and is owned by the class string.

++++++++++++++++++++++++++++++++

 

       string::c_str()函数返回一个const char*指针,作用就是把string类型的变量转化为C-字符串char* (以"/0"作为字符串的默认结尾)

      【如果要把一个char 转换成string, 可以使用string s(char *);   】

       c_str函数的返回值是const char*的,不能直接赋值给char* ,所以就需要我们进行相应的操作转化(利用strcpy()函数)。

  c++语言提供了两种字符串实现,其中较原始的一种只是字符串的c语言实现。与C语言的其他部分一样,它在c++的所有实现中可用,我们将这种实现提供的字符串对象,归为c-串每个c-串char*类型的

  标准头文件<cstring>包含操作c-串的函数库。当调用库函数,客户程序提供的是string类型参数,而库函数内部实现用的是c-串,因此需要将string对象,转化为char*对象,而c_str()提供了这样一种方法,它返回一个客户程序可读不可改的指向字符数组的指针

+++++++++++++++++++++++++++++++++

语法:
const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()等函数来操作c_str()返回的指针
比如:最好不要这样:
char* c;
string s="1234";
c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理

应该这样用:
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作(只能对其拷贝)

再举个例子
c_str() 以 char* 形式传回 string 内含字符串
如果一个函数要求char*参数,可以使用c_str()方法:
string s = "Hello World!";
printf("%s", s.c_str()); //输出 "Hello World!"

c_str() 一个将string转换为 const* char的函数
2009-06-07 09:51

const * char c_str()
    一个将string转换为 const* char的函数。

    string的c_str()返回的指针是由string管理的。它的生命期是string对象的生命期。然后可以按C的方式使用这个指针,或把它的内容复制出来。

    例如:
        string s;
        cin>>s;
        const char *ch=s.c_str();

   这样就可以从标准输入里输入任意长的字符串,并按const *char来使用。

   如果要把一个char 转换成string, 可以使用 string s(char *);  

其他类型转换方式:

string 转 CString  
CString.format("%s", string.c_str());  

char 转 CString  
CString.format("%s", char*);  

----------------------------------------------------------------------------------------------------------

以下内容原文出处:
http://www.cnblogs.com/pxhszcn/archive/2008/03/08/1096946.html

1,string -> CString  
CString.format("%s", string.c_str());  
用c_str()确实比data()要好.  
2,char -> string  
string s(char *);  
你的只能初始化,在不是初始化的地方最好还是用assign().  
3,CString -> string  
string s(CString.GetBuffer());  
GetBuffer()后一定要ReleaseBuffer(),否则就没有释放缓冲区所占的空间.  


《C++标准函数库》中说的  
有三个函数可以将字符串的内容转换为字符数组和C—string  
1.data(),返回没有”/0“的字符串数组  
2,c_str(),返回有”/0“的字符串数组  
3,copy()  


CString互转int  

将字符转换为整数,可以使用atoi、_atoi64或atol。  
而将数字转换为CString变量,可以使用CString的Format函数。如  
CString s;  
int i = 64;  
s.Format("%d", i)  
Format函数的功能很强,值得你研究一下。  

void CStrDlg::OnButton1()  
{  
// TODO: Add your control notification handler code here  
CString  
ss="1212.12";  
int temp=atoi(ss);  
CString aa;  
aa.Format("%d",temp);  
AfxMessageBox("var is " + aa);  
}  

sart.Format("%s",buf);  

CString互转char*  

///char * TO cstring  
CString strtest;  
char * charpoint;  
charpoint="give string a value";  
strtest=charpoint;  


///cstring TO char *  
charpoint=strtest.GetBuffer(strtest.GetLength());  

标准C里没有string,char *==char []==string  

可以用CString.Format("%s",char *)这个方法来将char *转成CString。要把CString转成char *,用操作符(LPCSTR)CString就可以了。  


CString转换 char[100]  

char a[100];  
CString str("aaaaaa");  
strncpy(a,(LPCTSTR)str,sizeof(a));  
2 CString类型的转换成int  
CString类型的转换成int  
将字符转换为整数,可以使用atoi、_atoi64或atol。  

//CString aaa = "16" ;
//int int_chage = atoi((lpcstr)aaa) ;  


而将数字转换为CString变量,可以使用CString的Format函数。如  
CString s;  
int i = 64;  
s.Format("%d", i)  
Format函数的功能很强,值得你研究一下。  
如果是使用char数组,也可以使用sprintf函数。

//CString ss="1212.12";  
//int temp=atoi(ss);  
//CString aa;  
//aa.Format("%d",temp);  


数字->字符串除了用CString::Format,还有FormatV、sprintf和不需要借助于Afx的itoa  

3 char* 在装int  
#include <stdlib.h>
  
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
long long atoq(const char *nptr);  

4 CString,int,string,char*之间的转换  
string aa("aaa");
char *c=aa.c_str();


cannot convert from 'const char *' to 'char *'
const char *c=aa.c_str();  

5 CString,int,string,char*之间的转换  
string.c_str()只能转换成const char *,
要转成char *这样写:

string mngName;
char t[200]; memset(t,0,200); strcpy(t,mngName.c_str());  

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值