C++:向量的第一个元素“损坏”
我有一个包含向量的类 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您执行
foo.getVector()
时,会返回临时向量,并在foo.getVector().begin(); 之后遇到
因此迭代器在循环内变得无效。;
时被销毁。如果将
foo.getVector();
的值存储在向量 v (v = foo.getVector();
) 中,然后使用向量 v,则效果很好。 这是因为向量 v 在整个循环中都是有效的。The temporary vector is returned when you do
foo.getVector()
and it gets destroyed the moment;
is encountered afterfoo.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.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.
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.
您的错误出现在 getVector() 方法中。
通过引用返回。
如果您不通过引用返回,您将创建一个临时副本。 该副本在使用后将被销毁。 在这种情况下,在 begin() 执行后,临时对象被销毁,因此 begin() 返回的迭代器无效。
You error is in the getVector() method.
Return by reference.
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.
修改 getVector 函数以返回对象引用,如下所示:& getVector() {返回speech_hits;}
std::vector
modify the getVector function to return the object reference like this:
std::vector<word_idx_value>& getVector() {return speech_hits;}