C++中的参考名称和变量名称 - 它们如何用于内存分配?

发布于 2025-01-23 16:49:33 字数 1297 浏览 2 评论 0原文

这是我目前正在进行的项目的多部分问题。我将尝试使其尽可能简短,同时仍然充分解释问题,很抱歉,如果这有点长。

  1. 当涉及std :: vector在C ++中时,它们与变量的究竟如何确切使用?例如,如果我有以下代码:
int myInt = 4;
std::vector<int> myIntVector;
myIntVector.push_back(myInt);

会发生什么? myintVector现在是否具有相同的 value 在添加时myint中的数据相同,但仍然完全分开它们?存储区域是否在myint的位置会物理地移动到myintVector内存内的指定区域?

  1. 假设我在上一个语句上是正确的,为什么std :: cout&lt;&lt; Myint仍然正确打印4,假设它没有被更改,而std :: cout&lt;&lt;&lt; MyintVector [0]还打印出4?

  2. 现在,要提示以下问题:#define指令。我正在为我的项目尝试这个,并注意到一些有趣的东西。我使用#define get_name(variable)(#variable),将输入变量的名称返回为字符数组。如果我有以下代码:

#define GET_NAME(variable) (#variable)

int myInt = 4;
std::vector<int> myIntVector;
myIntVector.push_back(myInt);

std::cout << GET_NAME(myInt) << "\n";
std::cout << GET_NAME(myIntVector[0]);

我会收到以下输出:

myInt
myIntVector[0]

为什么?假设问题1中的第一个语句是正确的,这是预期的输出,但是我们回到问题2。假设来自问题2的第二个语句是正确的,那么这是意想不到的输出,如myintMyintVector [0]应返回两次。

提前致谢!

This is a multi part question based on a project I'm currently undertaking. I will try to make it as brief as possible while still fully explaining the question, so sorry if this is a bit long.

  1. When it comes to std::vectors in C++, how exactly do they work with variables? For example, if I have the following code:
int myInt = 4;
std::vector<int> myIntVector;
myIntVector.push_back(myInt);

what happens? Does that area of memory inside myIntVector now have the same value of data stored inside myInt at the time of adding, but still completely separate them? Does the area of memory where myInt is stored get physically moved into a designated area inside of the memory of myIntVector?

  1. Assuming I was correct on the last statement, why would std::cout << myInt still correctly print 4, assuming it was not changed, while std::cout << myIntVector[0] also prints out 4?

  2. Now, for what prompted the question: the #define directive. I was experimenting with this for my project, and noticed something interesting. I used #define GET_NAME(variable) (#variable), which returns the name of the inputted variable as a character array. If I were to have the following code:

#define GET_NAME(variable) (#variable)

int myInt = 4;
std::vector<int> myIntVector;
myIntVector.push_back(myInt);

std::cout << GET_NAME(myInt) << "\n";
std::cout << GET_NAME(myIntVector[0]);

I would receive the following output:

myInt
myIntVector[0]

Why? Assuming the first statement from question 1 is correct, this is the expected output, but then we circle back to question 2. Assuming the second statement from question 2 is correct, this is the unintended output, as myInt or myIntVector[0] should be returned twice.

Thanks in advance!

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

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

发布评论

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

评论(1

小瓶盖 2025-01-30 16:49:33

当涉及到C ++中的std ::向量时,它们如何与变量一起使用?

默认情况下,所有STL容器仅复制值。因此,当您通过int变量时,它将被复制,并且副本完全独立于原始值存在

为什么std :: cout&lt;&lt; Myint仍然正确打印4,假设它没有更改,而STD :: Cout&lt;&lt; MyintVector [0]还打印出4?

这是两个不同的值,都等于4

如果要具有以下代码,我将收到以下输出。为什么?

宏只是操纵文本,并且不要在代码中做任何奇特的事情。此陈述:

std::cout << GET_NAME(myInt) << "\n";

只是在宏观下变成这一点:

std::cout << "myInt" << "\n";

When it comes to std::vectors in C++, how exactly do they work with variables?

All STL containers just copy values by default. So when you pass an int variable, it gets copied and the copy exist completely independently from the original value

why would std::cout << myInt still correctly print 4, assuming it was not changed, while std::cout << myIntVector[0] also prints out 4?

These are two different values, both equal to 4

If I were to have the following code, I would receive the following output. Why?

The macros just manipulate the text, and don't do anything fancy in your code. This statement:

std::cout << GET_NAME(myInt) << "\n";

Just turns into this under the macro:

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