1.代码
// ConsoleApplication14.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<string>
//#include<string.h>
#include<cstring>
using namespace std;
int main()
{
std::cout << "Hello World!\n";
//err
//char* str1 = "dddd";
const char* str2 = "12345";
char str3[] = "12345";
string sstr = str2;
printf("str2:%s",str2);
printf("\n");
printf("sizefo and strlen--------------------------\n");
printf("strlen(str2):%d",strlen(str2));
printf("\n");
printf("sizeof(str2):%d", sizeof(str2));
printf("\n");
printf("sizeof(*str2):%d", sizeof(*str2));
printf("\n");
printf("sstr.length():%d", sstr.length());
printf("\n");
printf("char[]/* string互相转换--------------------------\n");
//互相转换
//string sstr2 = string(str2);
//printf(sstr2);
//string sstr3 = "1234";
//printf(sstr3);
string sstr4 = "1234";
cout << sstr4 << "\n";
string sstr5 = string(str2);
cout << sstr5 << "\n";
string sstr6 = string(str3);
cout << sstr6 << "\n";
printf("char string互相转换--------------------------\n");
//char c1 = '5';
//string sstr7 = string(c1);
char c1 = '5';
string sstr8 = string(c1,1);
getchar();
}
2.运行结果
3. 要点
3.1strlen()和string.length()的结果均不宝行'\n'
3.3 strlen需要string.h 或 sstring 头文件
3.3 给char* 赋值字符串需要char* 是只读的