C++之数据类型的扩展

文章目录

结构体

  • C++中定义结构型变量,可以省略struct关键字
  • C++结构体中可以直接定义函数,谓之成员函数(方法)
c 复制代码
#include <iostream>
#include <cstring>
using namespace std;
int main(void){
    struct stu{
            int age;
            char name[20];
            void who(void){
            cout <<"我是:" << name << " 我今年:" << age <<endl;
        }
    };
    stu s1;
    s1.age = 21;
    strcpy(s1.name, "张飞");
    s1.who();
    return 0;
}

联合体

  • 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;
}

枚举

  • 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;
}

布尔

  • C++中布尔(bool)是基本数据类型,专门表示逻辑值
  • 布尔类型的字面值常量:
    • true 表示逻辑真
    • false表示逻辑假
  • 布尔类型的本质: 单字节的整数,使用1表示真,0表示假
  • 任何基本类型都可以被隐式转换为布尔类型
cpp 复制代码
#include <iostream>
using namespace std;
int main(void){
    bool b = true;
    cout << b <<endl;
    cout <<boolalpha << b <<endl;
    b = 3 + 2;
    cout <<boolalpha << b <<endl;
    return 0;
}

字符串

  • 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; }
if(s1 != s2){ cout << "true"<< endl; }

随机访问

cpp 复制代码
string s = "hello";
s[0] ="H"; //Hello

获取字符串长度

cpp 复制代码
size_t size();
size_t length();

转换为C风格的字符串

cpp 复制代码
const char* c_str();

字符串交换

cpp 复制代码
void swap(string s1,string s2);

demo

cpp 复制代码
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    /*定义*/
    string s1; //定义空字符串
    string s2("aaa");
    string s3 = string("bbb");
    string s4 = "cccc";
    /*字符串的拷贝*/
    string s5 = s2; // char *p5 = p2;
    cout << "s5 = " << s5 << endl;
    /*拼接*/
    s5 += s3;
    cout << "s5 = " << s5 << endl;
    /*字符串比较*/
    if(s2 == s3){ //strcmp(.....)
        cout << "true" << endl;
    } else {
         cout << "false" << endl;
    }
   
    /*取字符串长度*/
    cout << "s5 length = "<< s5.length() << endl;
    /*转换为C风格字符串*/
    const char *p = s5.c_str();
    printf("%s\n", p);
    /*交换*/
    swap(s2, s3);
    cout << "s2= "<< s2 << " s3= "<< s3<< endl;
    return 0;
}
相关推荐
郝学胜-神的一滴4 分钟前
[简化版 GAMES 104] 现代游戏引擎 06:从Tick时序到邮局模型,拆解确定性世界的底层密码
开发语言·c++·游戏引擎·图形渲染·软件开发·opengl
不会代码的小猴5 分钟前
6. Qt网络编程
开发语言·c++·笔记·qt·算法
沐风老师9 分钟前
从零开始学3dMax插件开发!
c++·3dmax插件·3dmax·maxscript
wuyk55511 小时前
98.C语言易混难点:字符数组与字符串指针的底层差异
c语言·开发语言·c++·stm32·嵌入式硬件·算法
峥无11 小时前
从0到1手撕红黑树:封装实现 my_map 与 my_set(SGI-STL 源码级深度解析)
开发语言·c++·笔记·算法·stl
波特率11520011 小时前
C++新特性---属性说明符与标准属性
开发语言·c++
不会代码的小猴12 小时前
3. 控件学习1
开发语言·c++·笔记·qt·算法
烧酒同学16 小时前
【C++】记录size of std::vector的巧妙坑
开发语言·c++·图形渲染
@呱呱爱学习19 小时前
C++大成之路_ STL容器_ Vector
开发语言·c++
码匠许师傅19 小时前
【C++ 面试真题】19. 聊聊 C++ 的迭代器
开发语言·c++·面试