sizeof 继续返回 4 而不是实际大小
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
sizeof
返回表达式的大小。对于您来说,这是一个std::string
,对于您的std::string
实现来说,这是四个。 (可能是内部指向缓冲区的指针。)但是您会看到,该缓冲区仅由字符串指向,它对 std::string 本身的大小没有影响。您需要
message.size()
来实现该目的,它可以为您提供该缓冲区指针所指向的字符串的大小。当字符串的内容发生变化时,缓冲区指针指向的内容也会发生变化,但指针本身的大小始终相同。
考虑以下情况:
此时,sizeof(foo) 已知;它是一个编译时常量。它是
int
的大小以及任何附加的填充编译器可能会添加。您可以让
bar
取您想要的任何值,并且大小保持不变,因为bar
的值与bar
的类型和大小无关>栏本身。sizeof
returns the size of an expression. For you, that's astd::string
and for your implementation ofstd::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 wantmessage.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:
At this point,
sizeof(foo)
is known; it's a compile-time constant. It's the size of anint
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 whatbar
's value is has nothing to do with the type and size ofbar
itself.您想要使用
message.size()
而不是sizeof(message)
。sizeof
仅给出数据类型或表达式中的字节数。您想要存储在字符串中的字符数,该字符数是通过调用size()
给出的。另外索引从 0 开始,请注意,下面我从 1 更改为 0。
x86 系统上的任何指针都只有 4 个字节。即使它指向堆上包含 100 个元素的数组的第一个元素。
示例:
假设没有填充,将
p
包装在class
或struct
中将得到相同的结果。You want to use
message.size()
notsizeof(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 callingsize()
Also indexing starts at 0, notice I changed from 1 to 0 below.
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:
Wrapping
p
in aclass
orstruct
will give you the same result assuming no padding.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.