C++对C语言数据类型的扩展

    • [1.4 C++对C语言数据类型的扩展](#1.4 C++对C语言数据类型的扩展)
      • [1.4.1 结构体](#1.4.1 结构体)
      • [1.4.2 联合](#1.4.2 联合)
      • [1.4.3 枚举](#1.4.3 枚举)
      • [1.4.4 布尔](#1.4.4 布尔)
      • [1.4.5 字符串](#1.4.5 字符串)

1.4 C++对C语言数据类型的扩展

基本的数据类型 char、unsigned char、int、short、unsigned shor、long、unsigned long、float、double、long double与C语言相同。扩展了bool类型,对结构体、联合、枚举做了改进。

1.4.1 结构体

  • C++中定义结构型变量,可以省略struct关键字
  • C++结构体中可以直接定义函数,谓之成员函数(方法)
cpp 复制代码
#include <iostream>
#include <cstring>

using namespace std;

int main (void) {

	struct std {
		int age;
		char name[20];
		void who() {
			cout << " 我是: " << name << " 今年: " << age << endl;
		}
	};

	std s1; //不需要 struct std s1; 这样写,可以省略struct
	s1.age = 20;
	strcpy(s1.name, "王五");
	s1.who();

	return 0;
}

1.4.2 联合

  • C++中定义联合体变量,可以省略union关键字

    cpp 复制代码
    union XX{......};
    XX x; //定义联合体变量直接省略union
  • 支持匿名联合

    cpp 复制代码
    union {//没有名字
     ......   
    };
cpp 复制代码
#include <iostream>

using namespace std;

int main (void) {
    
    union { //匿名联合
        int num;
        char c[4];
    };
    num = 0x12345678;
    cout << hex << (int)c[0] << " " << (int)c[1] << endl;
    return 0;
}

1.4.3 枚举

  • C++中定义枚举变量,可以省略enum关键字

  • C++中枚举是独立的数据类型,不能当做整型数使用

    cpp 复制代码
    #include <iostream>
    using namespace std;
    
    int main (void) {
    	enum COLOR{RED, GREEN, BLUE};
        
        COLOR c = GREEN;
    //    c = 2; //error
      	cout << c << endl;
        return 0;
    }

1.4.4 布尔

C++中布尔(bool)是基本数据类型,专门表示逻辑值

布尔类型的字面值常量:

​ true 表示逻辑真

​ false 表示逻辑假

布尔类型的本质:单字节的整数,使用1表示真,0表示假

任何基本类型都可以被隐式转换为布尔类型

cpp 复制代码
#include <iostream>
using namespace std;

int main (void) {
	bool a = true;

	cout << a << endl;				//输出:1
	cout << boolalpha << a << endl; //输出:true

	a = 5 + 3;
	cout << boolalpha << a << endl; //隐式转换bool型 输出:true
	return 0;
}

1.4.5 字符串

  • C++兼容C中的字符窜表示方法和操作函数

  • C++专门设计了string类型表示字符串

    • string类型字符串定义

      cpp 复制代码
      string s; //定义空字符串
      string s("hello");
      string s = "hello";
      string s = string("hello");
    • 字符串拷贝

      cpp 复制代码
      string s1 = "hello";
      string s2 = s1;
    • 字符串连接

      cpp 复制代码
      string s1 = "hello", s2 = " world";
      string s3 = s1 + s2; // s3: hello world
      s1 += s2; //s1: hello world
    • 字符串比较

      cpp 复制代码
      string s1 = "hello", s2 = " world";
      if(s1 == s2){ cout << "false" << endl; } //类似C语言中strcmp(......)
      if(s1 != s2){ cout << "true" << endl; }
    • 随机访问

      cpp 复制代码
      string s = "hello";
      s[0] = 'H'; //Hello
    • 获取字符串长度

      cpp 复制代码
      size_t size();//都不统计'\0'
      size_t length();//都不统计'\0'
    • 转换为C风格的字符串

      cpp 复制代码
      const char* c_str();
      
      	string s2("hello");
      	const char *p = s2.c_str();
      	printf("%s\n", p);
    • 字符串交换

      cpp 复制代码
      void swap(string s1, string s2)
相关推荐
zh_xuan14 分钟前
java Optional
java·开发语言
盐烟20 分钟前
C语言-函数练习1
c语言·开发语言·笔记
旧故新长24 分钟前
MyBatis 类型处理器(TypeHandler)注册与映射机制:JsonListTypeHandler和JsonListTypeHandler注册时机
java·开发语言·mybatis
simple_whu28 分钟前
Visual Studio C/C++编译器cl.exe的/source-charset与/execution-charset设置项
c语言·c++·visual studio
GOATLong38 分钟前
网络基础概念
linux·运维·服务器·网络·arm开发·c++
我是苏苏1 小时前
消息中间件RabbitMQ02:账号的注册、点对点推送信息
开发语言·后端·ruby
工藤新一¹1 小时前
C++/SDL 进阶游戏开发 —— 双人塔防(代号:村庄保卫战 14)
开发语言·c++·游戏引擎·游戏开发·sdl·实践项目
钢铁男儿1 小时前
C#核心技术解析:静态类型、dynamic与可空类型
开发语言·c#
卓越进步2 小时前
层级时间轮的 Golang 实现原理与实践
开发语言·后端·golang
zandy10112 小时前
嵌入式BI开发指南:如何通过衡石API将分析能力集成到业务系统?
开发语言·python·嵌入式