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.
在这里,我们声明了一个具有两个成员A和CH的结构。
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.
翻译自: https://www.includehelp.com/cpp-tutorial/structures-find-output-programs-set-4.aspx
c ++查找字符串