sizeof 继续返回 4 而不是实际大小

发布于 2024-08-28 02:13:23 字数 575 浏览 12 评论 0原文

#include <iostream>

using namespace std;

int main()
{
    cout << "Do you need to encrypt or decrypt?" << endl;
    string message;
    getline(cin, message);

    int letter2number;

    for (int place = 1; place < sizeof(message); place++)
    {
        letter2number = static_cast<int>(message[place]);
        cout << letter2number << endl;
    }
}

问题示例:我输入了十五个字母,但只打印了四个整数。我输入了七个字母,但只打印了四个整数。

该循环在我的计算机上只发生了四次,而不是字符串中的字符数。

这是我遇到的唯一问题,所以如果您看到其他错误,请不要告诉我。 (这样更有趣。)

谢谢您的宝贵时间。

#include <iostream>

using namespace std;

int main()
{
    cout << "Do you need to encrypt or decrypt?" << endl;
    string message;
    getline(cin, message);

    int letter2number;

    for (int place = 1; place < sizeof(message); place++)
    {
        letter2number = static_cast<int>(message[place]);
        cout << letter2number << endl;
    }
}

Examples of problem: I type fifteen letters but only four integers are printed. I type seven letters but only four integers are printed.

The loop only occurs four times on my computer, not the number of characters in the string.

This is the only problem I am having with it, so if you see other errors, please don't tell me. (It is more fun that way.)

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小苏打饼 2024-09-04 02:13:24

sizeof 返回表达式的大小。对于您来说,这是一个 std::string,对于您的 std::string 实现来说,这是四个。 (可能是内部指向缓冲区的指针。)

但是您会看到,该缓冲区仅由字符串指向,它对 std::string 本身的大小没有影响。您需要 message.size() 来实现该目的,它可以为您提供该缓冲区指针所指向的字符串的大小。

当字符串的内容发生变化时,缓冲区指针指向的内容也会发生变化,但指针本身的大小始终相同。


考虑以下情况:

struct foo
{
    int bar;
};

此时,sizeof(foo) 已知;它是一个编译时常量。它是 int 的大小以及任何附加的填充编译器可能会添加。

您可以让 bar 取您想要的任何值,并且大小保持不变,因为 bar 的值与 bar 的类型和大小无关>栏本身。

sizeof returns the size of an expression. For you, that's a std::string and for your implementation of std::string, that's four. (Probably a pointer to the buffer, internally.)

But you see, that buffer is only pointed to by the string, it has no effect on the size of the std::string itself. You want message.size() for that, which gives you the size of the string being pointed to by that buffer pointer.

As the string's contents change, what that buffer pointer points to changes, but the pointer itself is always the same size.


Consider the following:

struct foo
{
    int bar;
};

At this point, sizeof(foo) is known; it's a compile-time constant. It's the size of an int along with any additional padding the compiler might add.

You can let bar take on any value you want, and the size stays the same because what bar's value is has nothing to do with the type and size of bar itself.

﹏半生如梦愿梦如真 2024-09-04 02:13:24

您想要使用 message.size() 而不是 sizeof(message)

sizeof 仅给出数据类型或表达式中的字节数。您想要存储在字符串中的字符数,该字符数是通过调用 size() 给出的。

另外索引从 0 开始,请注意,下面我从 1 更改为 0。

for (int place = 0; place < message.size(); place++)
{
    letter2number = static_cast<int>(message[place]);
    cout << letter2number << endl;
}

x86 系统上的任何指针都只有 4 个字节。即使它指向堆上包含 100 个元素的数组的第一个元素。

示例:

char * p = new char[5000];
assert(sizeof(p) == 4);

假设没有填充,将 p 包装在 classstruct 中将得到相同的结果。

You want to use message.size() not sizeof(message).

sizeof just gives the number of bytes in the data type or expression. You want the number of characters stored in the string which is given by calling size()

Also indexing starts at 0, notice I changed from 1 to 0 below.

for (int place = 0; place < message.size(); place++)
{
    letter2number = static_cast<int>(message[place]);
    cout << letter2number << endl;
}

Any pointer on an x86 system is only 4 bytes. Even if it is pointing to the first element of an array on the heap which contains 100 elements.

Example:

char * p = new char[5000];
assert(sizeof(p) == 4);

Wrapping p in a class or struct will give you the same result assuming no padding.

缺⑴份安定 2024-09-04 02:13:24
class string
{
    char * ptr;
    //...
    size_t size();  // return number of chars (until null) in buffer pointed to by ptr
};

sizeof(message) == sizeof(string) == sizeof(ptr) == 4; // size of the struct

message.size() == number of characters in the message...
class string
{
    char * ptr;
    //...
    size_t size();  // return number of chars (until null) in buffer pointed to by ptr
};

sizeof(message) == sizeof(string) == sizeof(ptr) == 4; // size of the struct

message.size() == number of characters in the message...
思念满溢 2024-09-04 02:13:24

sizeof(type) 返回类型的大小,而不是对象的大小。使用 length() 方法查找字符串的长度。

sizeof(type) returns the size of the type, not the object. Use the length() method to find the length of the string.

烂人 2024-09-04 02:13:24
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    cout << "Do you need to encrypt or decrypt?" << endl;
    string message;
    getline(cin, message);

    int letter2number;

    for (int place = 0; place < message.size(); place++)
    {
        letter2number = static_cast<int>(message[place]);
        cout << letter2number << endl;
    }


    getch();
    return 0;
}    
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

    cout << "Do you need to encrypt or decrypt?" << endl;
    string message;
    getline(cin, message);

    int letter2number;

    for (int place = 0; place < message.size(); place++)
    {
        letter2number = static_cast<int>(message[place]);
        cout << letter2number << endl;
    }


    getch();
    return 0;
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文