获取指向映射中结构的指针 C++

发布于 2024-08-03 11:04:13 字数 817 浏览 1 评论 0原文

好吧,我有这样的结构

typedef struct
{
float x;
float y;
char name[];
} pTip;

,我创建了另一个结构

typdef struct
{
    float xx;
    float yy;
    pTip *tip;
}finalTip;

并填充了一个 map 映射,

效果很好。 我现在正在尝试生成

我所做的 FinalTips 向量:

map<string, pTip>::const_iterator iter = maps.find(p_name);

所以它工作得很好,我的迭代器现在有了我需要的东西,我可以使用提取信息

(iter->second).x

但我现在想使用该迭代器将其保存在我的 FinalTip struc obj Final 中 所以我尝试了:

finalTip final;
final.tip = iter->second;

对于这种情况,我得到错误:

错误:无法在分配中将'const pTip'转换为'pTip*'

所以我修复了:

*final.tip = iter->second;

这是正确的修复还是我做错了。 这似乎有效,但我想确保我做得正确

Ok so I have struct like this

typedef struct
{
float x;
float y;
char name[];
} pTip;

And another struc

typdef struct
{
    float xx;
    float yy;
    pTip *tip;
}finalTip;

I create and populate a map<string, pTip> maps

That works fine.
I am now trying to generate vector of finalTips

I do:

map<string, pTip>::const_iterator iter = maps.find(p_name);

So it works great my iterator now has what I need and I can extract info with

(iter->second).x

But I want to now using that iterator save it in my finalTip struc obj final
So I tried:

finalTip final;
final.tip = iter->second;

And for this case I get error:

error: cannot convert 'const pTip' to 'pTip*' in assignment

So I fixed by:

*final.tip = iter->second;

Was this correct fix or am I doing it wrong.
This seems to work but I want to make sure I am doing it right

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

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

发布评论

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

评论(4

贵在坚持 2024-08-10 11:04:13

由于 iter 是一个 map 迭代器,因此

final.tip = &iter->second;

iter->second 是对 pTip 的引用。用 & 获取其地址得到一个指针。

不幸的是,由于你有一个 const_iterator,

所以,要么得到一个非-const 迭代器,或者使 .tip 成员成为 const pTip *,或者如果你绝望的话,放弃 const:

final.tip = const_cast<pTip*>(&iter->second);

最后注意:你可能更喜欢 pTip const*const pTip * - 它们的含义相同。

You want

final.tip = &iter->second;

Since iter is a map<string, pTip> iterator, iter->second is a reference to pTip. Take its address with & to get a pointer.

Unfortunately, since you have a const_iterator, &iter->second will be a (const pTip *)

So, either get a non-const iterator, or make the .tip member a const pTip *, or if you're desperate, cast away the const:

final.tip = const_cast<pTip*>(&iter->second);

Final note: you may prefer pTip const* to const pTip * - they mean the same thing.

熊抱啵儿 2024-08-10 11:04:13

您是否已初始化 final.tip 以指向“空”提示...?否则,您将复制到未初始化的内存中。也许您的意思是 final.tip = new pTip(iter->second),即复制构造函数到新分配的存储中?还要小心 char name[] 构造——如果没有长度,自动生成的复制构造函数可能无法正确执行操作,因此您可能需要显式编码所需的复制构造函数!

Had you initialized final.tip to point to an "empty" tip...? Otherwise you're copying into uninitialized memory. Maybe you mean final.tip = new pTip(iter->second), i.e., a copy constructor into newly allocated storage? Also be careful about that char name[] construct -- without a length there, an automatically generated copy constructor may be in trouble doing things right, so you may need to explicitly code the copy ctor you need!

哭泣的笑容 2024-08-10 11:04:13

我很惊讶这有效。除非您有初始化此处所示的 Final.tip 的代码,否则这会将 iter->second 的值写入内存中的随机位置,这可能会导致不可预测的结果。

I am surprised this works. Unless you have a code that initializes final.tip that you have shown here, this would write the value of iter->second in a random place in the memory, which can lead to unpredictable results.

小嗷兮 2024-08-10 11:04:13

此代码:

*final.tip = iter->second

很可能最终导致您的程序开始表现不稳定。你正在践踏记忆。 Final.tip 是一个未初始化的指针,它可以指向任何地方。您会导致它指向的任何地方都被 iter->second 的内容覆盖。

我不完全确定你想在这里完成什么。你的问题的答案很大程度上取决于这一点。

pTip 中的 char name[] 构造也很有趣,但也存在问题。看起来您正在尝试在结构的末尾使用弹性缓冲区,这是一种常见的 C 技术。当与 STL 容器混合时,这不太可能很好地工作。

我怀疑您希望映射映射到指针而不是结构副本。我怀疑你的意思更像是:

typedef struct
{
float x;
float y;
char name[];
} *pTip;

This code:

*final.tip = iter->second

is quite likely to eventually cause your program to start behaving erratically. You are stomping on memory. final.tip is an uninitialized pointer and it could be pointing anywhere. You are causing wherever it's pointing to be overwritten with the contents of iter->second.

I'm not completely sure what you want to accomplish here. The answer to your question is highly dependent on this.

The char name[] construct in pTip is also interesting and problematic. It looks like you're trying to use a stretchy buffer at the end of your structure, which is a common C technique. That is unlikely to work well when mixed with STL containers.

My suspicion is that you intend your map to map to pointers instead of struct copies. I suspect you meant something more like:

typedef struct
{
float x;
float y;
char name[];
} *pTip;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文