c++基本类型&&&c++string与c风格字符串的相互转化

**********************************************************

c++基本类型: char <= short <= int(float,long) <= double(long long, long double)

                        1            2                  4                                     8

***********************************************************

  基础类型分为三个类别:整数、浮点和 void。 整数类型能够处理整数。 浮点类型能够指定可具有小数部分的值。

void 类型描述了值的空集。void 类型的变量无法指定 - 它主要用于声明不返回值的函数或用于声明指向非类型化或任意类型化数据的一般指针。 任何表达式都可以显示或强制转换为类型 void。 

C++ 语言的基础类型

类别类型内容
整数 char 类型 char 是通常包含基本执行字符集成员的整数类型 - 默认情况下,这是 Microsoft C++ 中的 ASCII。

C++ 编译器将 charsigned char 和 unsigned char 类型的变量视为不同类型。char 类型的变量将提升到 int,就像它们在默认情况下是 signed char 类型一样,除非使用 /J 编译选项。 在这种情况下,它们被视为 unsigned char 类型并提升为 int(没有符号扩展)。
  bool bool 类型是可以具有 true 或 false 这两个值之一的整数类型。 其大小未指定。
  short short int 类型(或 short)是大于或等于 char 类型的大小但小于或等于 int 类型的大小的整型类型。

 short 类型的对象可声明为 signed short 或 unsigned shortSigned short 是 short 的同义词。
  int int 类型是大于或等于 short int 类型的大小但小于或等于 long 类型的大小的整数类型。

 int 类型的对象可声明为 signed int 或 unsigned intSigned int 是 int 的同义词。
  __int8,__int16,__int32,__int64,__int128 固定大小的整数 __int``n,其中 n 是整数变量的大小(以比特为单位)。 (__int8__int16__int32__int64 和 __int128 是 Microsoft 专用的关键字。 并非所有类型在所有体系结构上都可用。)
  long long 类型(或 long int)是大于或等于 int 类型的大小的整数类型。

 long 类型的对象可声明为 signed long 或 unsigned longSigned long 是 long 的同义词。
  long long 大于无符号 long

 long long 类型的对象可声明为 signed long long 或 unsigned long longSigned long long 是 long long 的同义词。
  wchar_t,__wchar_t wchar_t 类型的变量指定宽字符或多字节字符类型。 默认情况下,wchar_t 是本机类型,但可以使用 /Zc: wchar_t- 使 wchar_t 成为 unsigned short 的 typedef。__wchar_t 类型是本机 wchar_t 类型的 Microsoft 专用同义词。

在字符或字符串文本前使用 L 前缀可指定宽字符类型。
浮点 float float 类型是最小的浮点类型。
  double double 类型是大于或等于 float 类型的大小但小于或等于 long double 类型的大小的浮点类型。

Microsoft 专用:long double 和 double 的表示形式完全相同。 但是,long double 和 double 是不同的类型。
  long double long double 类型是大于或等于 double 类型的浮点类型。

基础类型的大小

类型大小
boolcharunsigned charsigned char__int8 1 个字节
__int16shortunsigned shortwchar_t__wchar_t 2 个字节
float__int32intunsigned intlong, unsigned long 4 个字节
double__int64long doublelong long 8 个字节
__int128 16 个字节

**************************************

c++string与c风格字符串的相互转化

**************************************

string---->c风格字符串:

string str = "Hello World";

const char *str = str.c_str();

 

c风格字符串------->string

char* str = "Hello World"; // char str[] = "Hello World";

string s(str);//string s = str;

posted @ 2016-10-18 21:20  lp3318  阅读(953)  评论(0编辑  收藏  举报