C++:向量的第一个元素“损坏”

发布于 2024-07-20 03:54:31 字数 1135 浏览 1 评论 0原文

我有一个包含向量的类 (foo)。

如果我尝试像这样迭代向量中的元素:

for(vector<random>::iterator it = foo.getVector().begin();
        it != foo.getVector().end(); ++it) {
  cout << (*it) << endl;

}

第一个元素总是损坏并返回垃圾数据。

但是,如果执行以下操作:

 vector<random> v = foo.getVector();
 for(vector<random>::iterator it = v.begin();
            it != v.end(); ++it) {
      cout << (*it) << endl;

 }

一切似乎都工作正常。 是否有我不知道的“陷阱”?

我也尝试过 cout << foo.getVector()[0] << 结束; 在循环之外,但似乎工作正常。

谢谢。

编辑:

这是我的头文件:

#ifndef HITS
#define HITS

#include <vector>
#include "wrappers.h"

class Hits {

    public:
        Hits();
        std::vector<word_idx_value> getVector() {return speech_hits;}
        const std::vector<word_idx_value> getVector() const {return speech_hits;}
        void add(const word_idx_value&);
        Hits &operator+=(const Hits&);
    private:
        std::vector<word_idx_value> speech_hits;
};

#endif

I have a class (foo) that contains a vector.

If i try iterating over the elements in the vector like so:

for(vector<random>::iterator it = foo.getVector().begin();
        it != foo.getVector().end(); ++it) {
  cout << (*it) << endl;

}

The first element is always corrupted and returns garbage data.

However, if do something like:

 vector<random> v = foo.getVector();
 for(vector<random>::iterator it = v.begin();
            it != v.end(); ++it) {
      cout << (*it) << endl;

 }

Everything appears to be working fine. Is there a "gotcha" that I do not know about?

I've also tried doing cout << foo.getVector()[0] << endl; outside of the loop but that appears to be working ok.

Thanks.

Edit:

Here's my header file:

#ifndef HITS
#define HITS

#include <vector>
#include "wrappers.h"

class Hits {

    public:
        Hits();
        std::vector<word_idx_value> getVector() {return speech_hits;}
        const std::vector<word_idx_value> getVector() const {return speech_hits;}
        void add(const word_idx_value&);
        Hits &operator+=(const Hits&);
    private:
        std::vector<word_idx_value> speech_hits;
};

#endif

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

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

发布评论

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

评论(5

戴着白色围巾的女孩 2024-07-27 03:54:31
for(vector<random>::iterator it = foo.getVector().begin();

当您执行 foo.getVector() 时,会返回临时向量,并在 foo.getVector().begin(); 之后遇到 ; 时被销毁。 因此迭代器在循环内变得无效。

如果将 foo.getVector(); 的值存储在向量 v (v = foo.getVector();) 中,然后使用向量 v,则效果很好。 这是因为向量 v 在整个循环中都是有效的。

for(vector<random>::iterator it = foo.getVector().begin();

The temporary vector is returned when you do foo.getVector() and it gets destroyed the moment ; is encountered after foo.getVector().begin(); Hence iterator becomes invalid inside the loop.

If you store the value of foo.getVector(); in vector v ( v = foo.getVector();) and then use the vector v, it works fine. It is because the vector v is valid throughout the loop.

ㄖ落Θ余辉 2024-07-27 03:54:31

getVector() 按值返回向量。 getVector 的两次调用(begin() 和 end())返回向量的不同副本,因此您对一个对象调用 begin(),对另一个对象调用 end()。 您得到的是两个迭代器到两个不同的容器中。 将这两个迭代器与 != 进行比较会产生一个未定义的值。

getVector() returns a vector by value. The two invocations of getVector (begin() and end()) return different copies of the vector, so you call begin() on one object and end() on another. What you get is two iterators into two different containers. Comparing those two iterators with != yields an undefined value.

梦一生花开无言 2024-07-27 03:54:31

getVector() 按值返回向量,在第一种情况下,您会得到一个临时变量,一旦进入循环,该变量就会被销毁。 在第二种情况下,您将结果复制到在循环内仍然有效的局部变量中。 可能的解决方案是通过 const 引用返回向量。

getVector() returns vector by value and in the first case you get a temporary variable that gets destroyed once you're inside the loop. In the second case you copy the result into a local variable that is still alive while inside the loop. Possible solution is to return vector by const reference.

独闯女儿国 2024-07-27 03:54:31

您的错误出现在 getVector() 方法中。
通过引用返回。

class Hits
{
    public:
    std::vector<word_idx_value>&   getVector() {return speech_hits;}
    //                         ^
    //                      Add the & to return by reference.

    // You may also want a const version at some point.
    std::vector<word_idx_value> const&   getVector() const {return speech_hits;}

如果您不通过引用返回,您将创建一个临时副本。 该副本在使用后将被销毁。 在这种情况下,在 begin() 执行后,临时对象被销毁,因此 begin() 返回的迭代器无效。

You error is in the getVector() method.
Return by reference.

class Hits
{
    public:
    std::vector<word_idx_value>&   getVector() {return speech_hits;}
    //                         ^
    //                      Add the & to return by reference.

    // You may also want a const version at some point.
    std::vector<word_idx_value> const&   getVector() const {return speech_hits;}

If you don;t return by reference you are creating a temporary copy. The copy is then destroyed after it has been used. In this case after the begin() has executed the temporary object is destroyed, and thus the iterator returned by begin() is not valid.

勿挽旧人 2024-07-27 03:54:31

修改 getVector 函数以返回对象引用,如下所示:
std::vector& getVector() {返回speech_hits;}

modify the getVector function to return the object reference like this:
std::vector<word_idx_value>& getVector() {return speech_hits;}

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