String对象和C字符串之间的转换
- // String.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include<iostream>
- #include<string>
- int main(int argc, char* argv[])
- {
- using namespace std;
- string string_variable;
- char a_c_string[] = "This is my C string.";
- string_variable = a_c_string; ///合法
- //a_c_string = string_variable; //非法
- // strcpy(a_c_string , string_variable); //非法
- ///使用string类的成员函数<SPAN style="COLOR: #ff0000">c_str()</SPAN>执行强制类型转换
- strcpy(a_c_string , string_variable.c_str()); ///合法
- return 0;
- }