String对象和C字符串之间的转换


String对象和C字符串之间的转换

  1. // String.cpp : Defines the entry point for the console application.  
  2. //  
  3.  
  4. #include "stdafx.h"  
  5. #include<iostream>  
  6. #include<string>  
  7.  
  8. int main(int argc, char* argv[]) 
  9.     using namespace std; 
  10.     string string_variable; 
  11.     char a_c_string[] = "This is my C string."
  12.      
  13.     string_variable = a_c_string;     ///合法  
  14.  
  15.     //a_c_string = string_variable;   //非法  
  16.  
  17. //  strcpy(a_c_string ,  string_variable);  //非法  
  18.  
  19.     ///使用string类的成员函数<SPAN style="COLOR: #ff0000">c_str()</SPAN>执行强制类型转换  
  20.     strcpy(a_c_string , string_variable.c_str());     ///合法  
  21.      
  22.     return 0; 

相关内容