在 GNU C++ 中声明字符串数组

发布于 2024-12-10 20:22:32 字数 734 浏览 0 评论 0原文

对我来说,这似乎应该如此简单,但我只是觉得我错过了一些东西。

我的头文件中有一个私有变量 _stoplist 当我在头文件中声明它时,代码看起来就像这样。

private:
    std::string _stoplist[];

但是当我稍后在我的函数中决定访问它时,它会出现任何错误。

_stoplist[_length];
//cout << _length << prints 104 (its a valid int and everything)
_stoplist[0] = "b";

std::string.assign() 代码因段错误而崩溃。我有一种直觉,我在这里遗漏了一些明显的东西,但我还没有完全弄清楚是什么。

提前致谢!

编辑:好的,感谢您的所有帮助。对于任何可能阅读本文的人,我建议使用下面的答案之一,因为这是明智的方法。但就我而言,由于我需要在不使用向量的情况下动态分配它,所以我只使用了以下代码。

private:
    std::string *_stoplist;

然后在我的 cpp 文件中

_stoplist = new string[_length];

是的,事实证明它确实很简单,我只是忽略了那部分。

To me it seems like it should be so simple, but I just feel like I'm missing something.

I have in my header file a private variable _stoplist When I declared it in the header file the code looks just like this.

private:
    std::string _stoplist[];

But when later in my function I decide to access this, it segfaults on anything.

_stoplist[_length];
//cout << _length << prints 104 (its a valid int and everything)
_stoplist[0] = "b";

Crashes in the std::string.assign() code with a segfault. I have a gut feeling that I'm missing something obvious here but I haven't quite found out what yet.

Thanks in advance!

EDIT: Ok, thanks for all the help. For anyone else who may read this, I would recommend to use one of the answers below since that is the smart way to do it. In my case though since I needed to dynamically allocate it without using vector I just used the following code.

private:
    std::string *_stoplist;

and then in my cpp file

_stoplist = new string[_length];

Yeah, turns out that it really was way simple, and I just was over looking that part.

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

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

发布评论

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

评论(2

冬天的雪花 2024-12-17 20:22:32

由于 _stoplist 没有大小,因此出现数组越界错误。您应该给它一个大小,并且只访问该范围内的元素,例如:

private:
    std::string _stoplist[100];

现在您应该能够通过 _stoplist[99] 索引 _stoplist[0]。然而,更好的解决方案可能会使用 std::vector 代替,因为它更安全。

private:
    std::vector< std::string > _stoplist;

然后,您可以使用其成员函数(例如 resize())将其增长到您需要的任何大小。

You're getting an array out of bounds error because _stoplist doesn't have a size. You should either give it a size, and only access elements within that range, such as:

private:
    std::string _stoplist[100];

now you should be able to index _stoplist[0] through _stoplist[99]. However a better solution would probably to use std::vector instead, as it's a lot safer.

private:
    std::vector< std::string > _stoplist;

Then you can use its member functions such as resize() to grow it to whatever size you need.

寂寞美少年 2024-12-17 20:22:32

这是因为声明为 std::string[] 的变量基本上只是一个指针。要使用它,您需要为其分配内存。如果您想为其分配固定内存,请尝试将其声明为例如 std::string _stoplist[5];

That's because a variable declared as std::string[] is basically just a pointer. To use it, you need to allocate memory for it. If you want to allocate fixed memory for it, try declaring it as e.g. std::string _stoplist[5]; instead.

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