c ++查找字符串_C ++结构| 查找输出程序| 套装4

c ++查找字符串

Program 1:

程序1:

#include <iostream>
using namespace std;

struct st {
    unsigned int A : 5;
    unsigned int B : 6;
    unsigned int C : 7;
    unsigned int D : 8;
} S;

int main()
{
    cout << sizeof(S);
    return 0;
}

Output:

输出:

4

Explanation:

说明:

We compiled this program on a 32-bit based system

我们在基于32位的系统上编译了该程序

The above program is using the concept of bit fields; it is used to optimize the allocation of memory space.

上面的程序使用位域的概念; 它用于优化内存空间的分配。

Here, we declared 4 variables in the structure and specified the required bits for each integer. But due to structure padding, it will allocate 4-byte space. Because the block size in the 32bit system is 4 byte.

在这里,我们在结构中声明了4个变量,并为每个整数指定了所需的位。 但是由于结构填充,它将分配4个字节的空间。 因为32位系统中的块大小为4字节。

This, 4 integer variables will take only in 4 bytes. Because the total size of all is less than 32 bits, if it exceeds more than 32 bits then it will allocate one more block of 4 bytes.

这4个整数变量将仅占用4个字节。 因为所有字节的总大小小于32位,所以如果它大于32位,则它将再分配一个4字节的块。

unsigned int A:5;
unsigned int B:6;
unsigned int C:7;
unsigned int D:8;

= 5 + 6 + 7 + 8
= 26

26 is less than 32 bits then the structure allocated 4 bytes of memory.

26小于32位,则该结构分配了4个字节的内存。

Program 2:

程式2:

#include <iostream>
using namespace std;

struct st {
} S;

int main()
{
    cout << sizeof(S);
    return 0;
}

Output:

输出:

1

Explanation:

说明:

In C language, the size of the empty structure is 0, while the size of the empty structure in C++ is 1. Here, it takes 1 byte to distinguish the object of structures.

C语言中 ,空结构的大小为0,而在C ++中,空结构的大小为1。这里,需要1个字节来区分结构的对象。

Program 3:

程式3:

#include <iostream>
using namespace std;

struct st {
    int A;
    char CH;
};

int main()
{
    struct st s[] = { { 10, 'A' }, { 20, 'B' } };

    int X, Y;

    X = s[0].A + s[0].CH;
    Y = s[1].A + s[1].CH;

    cout << (X * Y);

    return 0;
}

Output:

输出:

6450

Explanation:

说明:

Here, we declared a structure with two members A and CH.

在这里,我们声明了一个具有两个成员ACH的结构。

In the main() function, we created the array of structure and initialized values.

main()函数中,我们创建了结构数组和初始化值。

Evaluate the given expression,

计算给定的表达式,

X = s[0].A + s[0].CH;
	X = 10 + 'A';
	X = 10 + 65; // ASCII value of 'A' is 65.
	X = 75;
Y = s[1].A + s[1].CH;
	Y = 20 + 'B';
	Y = 20 + 66;
	Y = 86;

Then
	cout<

The above cout statement will print the multiplication of 75 and 86 that is 6450.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


翻译自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-4.aspx

c ++查找字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值