C++çstringç±»æä¾äº3个å°å
¶å
容转æ¢ä¸ºå符æ°ç»çå½æ°data()ãc_str()åcopy()ãå两个è¿åçæ°ç»é½æ¯constæ§è´¨çï¼ä¸è½æ¹åå
¶å
容ï¼copy()å¯å°å
¶å
容ç´æ¥æ·è´å°charåæ°ç»ä¸ï¼ä½æ²¡æç»æ符ãæè§å¾æ好çåæ³å°±æ¯èªå·±å段代ç æ·è´ï¼å 为stringæ¯æ¯æåæä½å符æ°ç»ä¸æ ·æä½çãä¸é¢æä¾ä¸¤å®ä»£ç ä¾åèï¼
//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;
int main(int argc,char *argv[]){
string s="1234567890";
int i;
char p[100];
for(i=0;i<s.length();p[i++]=s[i]);//èªå代ç æ·è´,æ¬äººæ¨èè¿ä¸ª,ç®åæäº
p[i]='\0';//stringçå符串ä¸ä»¥'\0'ç»å°¾,è½®æ¢åå ä¸'\0'
cout << p << endl;
return 0;
}//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;
int main(int argc,char *argv[]){
string s="1234567890";
int ln;
char p[100];
s.copy(p,ln=s.length());//è°ç¨stringçæåå½æ°copy
p[ln]='\0';//copyä¸èªå¨æ·»å ç»æ'\0',è¦æ工添å
cout << p << endl;
return 0;
}
以ä¸ä»£ç çæ§è¡ä¸¾ä¾ï¼
ç±äºstringç对象å¯è½åæä½charåæ°ç»ä¸æ ·æä½ï¼æ以é¤éä¸ä¸å¾å·²ä¸æå¡è½¬æ¢ãä¾åèã