结构体大小“再理解”

1.首先看一个列子,在VC++6.0中用C语言进行写如下代码
会报错,
改成printf(“%d”,sizeof(stuct test));就可以
再用C++进行书写是可以的,

2.在C语言中是不能有空结构体的,在C++中是可以,大小视编辑器
而定,G++空结构大小为1

3.在C 语言中的用struct 在c++中用class 区别:
struct 成员默认public
class 成员默认private
4 在结构体嵌套时要小心,结构体的声明列如:
里面的结构体大小不算
5 注意公用体大小的计算特别之处

下面几道例子,如果没懂,看我博客.《内存对齐》

struct   test  
{
      int  a;
      char b;
      struct student
      {
          char  c;
          int d   
      };   
};

5,结构体大小是重点,参照内存对齐

#include<stdio.h>
int main()
{

    struct test
    {
        int a;
        char b;
    };
    printf("%d",sizeof(test));
    return 0;

}

Compiling...
ll.cpp
l.c
c:\users\liu\desktop\test1\lll\l.c(10) : error C2065: 'test' : undeclared identifier
执行 cl.exe 时出错.

lll.exe - 1 error(s), 0 warning(s)
#include<iostream>
int main()
{
    struct test 
    {
        int a;
        char b;
    };
    printf("%d",sizeof(test));
    return 0;

}

Compiling...
Skipping... (no relevant changes detected)
ll.cpp

ll.obj - 0 error(s), 0 warning(s)
#include<iostream>
using namespace std;

int main()
{

    int *p = NULL;
    cout <<"指针的大小"<<sizeof(p)<<endl;
    cout<< "long 的大小"<<sizeof(long)<<endl;
    struct test
    {
         short data1[2];
         double b;
         long g;
         char a;
         double data;
    };

cout << sizeof(test)  <<endl;

    return 0;
}

指针的大小4
long 的大小4
32
Press any key to continue
#include<iostream>
using namespace std;

int main()
{
    union  
         {
             char name[10];  //10     +2    ----12  
             int data;      //4           ----16                 
             double  count;//8                                  
         }sd;
    cout<<"sd的大小"<<sizeof(sd)<<endl;




    int *p = NULL;
    cout <<"指针的大小"<<sizeof(p)<<endl;
    cout<< "long 的大小"<<sizeof(long)<<endl;


    struct test
    {
         short data1[2];  //4   +4
         double b;        //8       ---16
         long g;          //8    ---24
         char a;          //1
                          //        ---25   +  7    ---32 

         union  
         {
             char name[10];  //10     +2    ----12  
             int data;      //4              ----16   
             double  count;//8                                  
         }sd;               //                        ---16

                            //                       -----38    +2
                            //                            --------40
    };

cout << sizeof(test)  <<endl;

    return 0;
}

sd的大小16
指针的大小4
long 的大小4
40
Press any key to continue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您讲解一下结构小根堆的实现。 首先,我们需要定义一个结构来表示堆中的元素。假设我们需要维护一个包含学生姓名和分数的小根堆,那么结构可以定义如下: ```c++ struct Student { std::string name; int score; // 重载小于运算符,用于比较结构对象大小 bool operator<(const Student& other) const { return score > other.score; // 注意这里是小于号,因为要实现小根堆 } }; ``` 接下来,我们可以使用 STL 中的 priority_queue 来实现小根堆。 ```c++ #include <queue> using namespace std; priority_queue<Student> q; // 创建一个空的小根堆 // 往小根堆中插入一个元素 Student s1 = {"Tom", 80}; q.push(s1); // 从小根堆中取出堆顶元素(即分数最低的学生) Student top_student = q.top(); q.pop(); ``` 在上面的代码中,我们使用 priority_queue 来创建一个空的小根堆,并使用结构 Student 来表示堆中的元素。往堆中插入一个元素时,只需要调用 push 函数即可。从堆中取出堆顶元素时,只需要调用 top 函数获取堆顶元素,调用 pop 函数将其从堆中删除即可。 需要注意的是,我们需要在结构中重载小于运算符,以便 priority_queue 可以正确比较结构对象的大小。在上面的代码中,重载的小于运算符返回的是分数较低的学生应该排在前面。 希望这个简单的例子可以帮助您理解结构小根堆的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值