string中c_str(),data(),copy(p,n)函數(shù)的用法總結(jié)
標(biāo)準(zhǔn)庫的string類提供了3個(gè)成員函數(shù)來從一個(gè)string得到c類型的字符數(shù)組:c_str()、data()、copy(p,n)。
1. c_str():生成一個(gè)const char*指針,指向以空字符終止的數(shù)組。
注:
①這個(gè)數(shù)組的數(shù)據(jù)是臨時(shí)的,當(dāng)有一個(gè)改變這些數(shù)據(jù)的成員函數(shù)被調(diào)用后,其中的數(shù)據(jù)就會失效。因此要么現(xiàn)用先轉(zhuǎn)換,要么把它的數(shù)據(jù)復(fù)制到用戶自己可以管理的內(nèi)存中。注意??聪吕?BR>
const char* c;
string s="1234";
c = s.c_str();
cout<<c<<endl; //輸出:1234
s="abcd";
cout<<c<<endl; //輸出:abcd
上面如果繼續(xù)用c指針的話,導(dǎo)致的錯(cuò)誤將是不可想象的。就如:1234變?yōu)閍bcd
其實(shí)上面的c = s.c_str(); 不是一個(gè)好習(xí)慣。既然c指針指向的內(nèi)容容易失效,我們就應(yīng)該按照上面的方法,那怎么把數(shù)據(jù)復(fù)制出來呢?這就要用到strcpy等函數(shù)(推薦)。
//const char* c; //①
//char* c; //②
//char c[20];
char* c=new char[20];
string s="1234";
//c = s.c_str();
strcpy(c,s.c_str());
cout<<c<<endl; //輸出:1234
s="abcd";
cout<<c<<endl; //輸出:1234
注意:不能再像上面一樣①所示了,const還怎么向里面寫入值??;也不能②所示,使用了未初始化的局部變量“c”,運(yùn)行會出錯(cuò)的 。
② c_str()返回一個(gè)客戶程序可讀不可改的指向字符數(shù)組的指針,不需要手動(dòng)釋放或刪除這個(gè)指針。
2. data():與c_str()類似,但是返回的數(shù)組不以空字符終止。
3. copy(p,n,size_type _Off = 0):從string類型對象中至多復(fù)制n個(gè)字符到字符指針p指向的空間中。默認(rèn)從首字符開始,但是也可以指定,開始的位置(記住從0開始)。返回真正從對象中復(fù)制的字符。------用戶要確保p指向的空間足夠保存n個(gè)字符。
// basic_string_copy.cpp
// compile with: /EHsc /W3
#include <string>
#include <iostream>
int main( )
{
using namespace std;
string str1 ( "1234567890" );
basic_string <char>::iterator str_Iter;
char array1 [ 20 ] = { 0 };
char array2 [ 10 ] = { 0 };
basic_string <char>:: pointer array1Ptr = array1;
basic_string <char>:: value_type *array2Ptr = array2;
cout << "The original string str1 is: ";
for ( str_Iter = str1.begin( ); str_Iter != str1.end( ); str_Iter++ )
cout << *str_Iter;
cout << endl;
basic_string <char>:: size_type nArray1;
// Note: string::copy is potentially unsafe, consider
// using string::_Copy_s instead.
nArray1 = str1.copy ( array1Ptr , 12 ); // C4996
cout << "The number of copied characters in array1 is: "
<< nArray1 << endl;
cout << "The copied characters array1 is: " << array1Ptr << endl;
basic_string <char>:: size_type nArray2;
// Note: string::copy is potentially unsafe, consider
// using string::_Copy_s instead.
nArray2 = str1.copy ( array2Ptr , 5 , 6 ); // C4996
cout << "The number of copied characters in array2 is: "
<< nArray2 << endl;
cout << "The copied characters array2 is: " << array2Ptr << endl;
////注意一定要使array3有足夠的空間
//char array3[5]={0};
//basic_string<char>::pointer array3Ptr=array3;
//basic_string<char>::size_type nArray3;
//nArray3 = str1.copy(array3,9); //錯(cuò)誤!?。?!
//cout<<"The number of copied characters in array3 is: "
// <<nArray3<<endl;
//cout<<"The copied characters array3 is: "<<array3Ptr<<endl;
}
上面最后注釋掉的部分,雖然編譯沒有錯(cuò)誤,但是運(yùn)行時(shí)會產(chǎn)生錯(cuò)誤:Stack around the variable 'array3' was corrupted.
上一篇:數(shù)組指針、指針數(shù)組以及二位數(shù)組的深入解析
欄 目:C語言
下一篇:淺析內(nèi)存對齊與ANSI C中struct型數(shù)據(jù)的內(nèi)存布局
本文標(biāo)題:string中c_str(),data(),copy(p,n)函數(shù)的用法總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4179.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10如何尋找數(shù)組中的第二大數(shù)
- 01-10C++大數(shù)模板(推薦)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子