C++将字符串的副本存储在对向量中

发布于 2024-09-26 23:46:31 字数 286 浏览 1 评论 0 原文

我在一个类中有一个私有属性,定义为 vector; >数据;。我使用 data.push_back(make_pair(p, r)); 将数据添加到该向量中。后来当我从向量中获取数据时,我得到了 p 值的错误数据。返回的数据如��U3。我认为这是因为正在存储指向 char 数组的指针。我将如何在向量中存储实际副本。如果有帮助的话,字符数组将永远不会超过 255 个字符 + 1(以 null 终止)。

I have a private attribute in a class that is defined as vector<pair<char *, int> > data;. I add data to this vector with data.push_back(make_pair(p, r));. Later when I go to get the data out of the vector I get bad data for the p value. The data returned is like ��U3. I think this is because a pointer to the char array is being stored. How would I go about storing an actual copy in the vector. If it helps the char array will never exceed 255 chars + 1 for null terminating.

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

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

发布评论

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

评论(5

霊感 2024-10-03 23:46:32

如果你有一个 vector; >您正在处理裸指针,并且需要手动管理字符串的生命周期。这是一个主要的 PITA,很可能你做错了(特别是因为你甚至懒得声称自己做对了,这似乎建议你甚至不知道你必须这样做)。

与其他人一样,我建议您使用 vector; > 代替。

If you have a vector<pair<char *, int> > you're dealing with naked pointers and need to manually manage the lifetime of your strings. That's a major PITA and it is very likely that you're doing something wrong (especially so since you didn't even bother to claim to do this right, which seems to suggest you don't even know you have to do this).

As the others, I'd suggest you use vector<pair<std::string, int> > instead.

清醇 2024-10-03 23:46:32

您是否通过堆栈变量分配字符串并存储它的地址?当您从分配的函数返回时,这会导致您描述的问题。

最好在堆上分配字符串(使用 new 运算符),然后存储堆分配的地址。例如,

char* pNext = new char[50];
strcpy(pNext, ...);
data.push_back(make_pair(pNext, r));

您必须使用 char 数组完成您自己的所有工作 - 例如,您必须确保它们以 null 终止,否则当您打印它们时,您将得到意外的输出结果。

例如

pNext[49] = '\0';

,另外,请记住,当您完成堆分配的字符串时,您也必须删除它们。因此删除 char*:

delete [] pNext;

正如其他人所说,还有其他字符串实现可以减少您的麻烦。

最容易使用的可能是 CString,但会产生包含 MFC 实现或 ATL 实现的开销。正如其他人提到的那样, std::string 是一种替代方案。

希望这有帮助!

Are you allocating the string via a stack variable and storing it's address? That would cause the problem you describe when you return from the function you allocate in.

Better to allocate the string on the heap (with the new operator), then store the heap allocated address. e.g.

char* pNext = new char[50];
strcpy(pNext, ...);
data.push_back(make_pair(pNext, r));

You have to do all your own work with char arrays -e.g. You must ensure they're null terminated, or you'll have unexpected output results when you go to print them.

e.g.

pNext[49] = '\0';

Also, remember that when you're finished with a heap allocated string, you must delete them too. char*'s are deleted thus:

delete [] pNext;

As others have said, there are other string implementations that will cause you less headaches.

The easiest to use is probably CString, but incurs the overhead of either including MFC's implementation or ATL's implementation of it. std::string's, as others have mentioned are an alternative.

hope this helps!

小姐丶请自重 2024-10-03 23:46:32

由于 STL 基于复制和值语义,因此如果将整个字符串(而不仅仅是指向它的指针)存储在容器(此处为向量)中,则会更容易。我说“更容易”是因为可以将指针保留在STL容器中,但是指向的内存必须由用户代码管理。从提问来看,好像还没有做到。

因此,也许您可​​以使用 string 而不是 char *,例如:

typedef std::pair< std::string, int > MyPair;
std::vector< MyPair > data;

Since STL is based on copy and value semantics, it is easier if the entire character string, not merely a pointer to it, is stored in the container (here, vector). I said "easier" because it is possible to keep pointers in STL containers, but then the memory pointed to must be managed by user code. From the question, it seems that it is not been done.

So, perhaps you can use string instead of char *, for e.g.:

typedef std::pair< std::string, int > MyPair;
std::vector< MyPair > data;
只等公子 2024-10-03 23:46:31

使用 char* 的真正原因是什么?

使用 std::string,这将使您的问题消失。

Any real reason to using a char*?

Use an std::string, that will make your problems go away.

奢欲 2024-10-03 23:46:31

看起来好像有一个指向 p (当时定义的)的指针放置在堆栈上。一旦堆栈帧弹出,您仍然拥有指针,但它指向的内存可能是垃圾。这些悬空指针问题可能会很烦人,因此我建议使用 #include 中定义的 std::string 类来存储字符串数据。

It looks as though you have a pointer to p (which is defined at the time) placed on the stack. Once the stack frame gets popped off you still have the pointer but the memory it points to may be garbage. These sort of dangling pointer problems can get annoying so I'd recommend using the std::string class that is defined in #include <string> to store the string data.

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