问题标签 [push-back]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
225 浏览

c++ - 使用新创建的向量的操作

有人能帮忙吗...

我得到:错误 C2228:'.reserve' 的左边必须有类/结构/联合

我正在使用 new 运算符创建一个向量,以便它比创建它的函数的寿命更长。因此,这给了我一个指向堆上那个向量的指针,而不是一个实际的向量对象本身。因此它不会让我执行任何 push_backs 的 .reserve()。我看不到解决方法,有人可以帮忙吗?

0 投票
3 回答
3127 浏览

c++ - 导致向量中的 push_back对看似简单的操作进行分段错误

我正在为Project Euler开发一个程序来添加 2^1000 的所有数字。到目前为止,我已经能够在程序分段错误达到大约 5 位时跟踪它,并尝试在函数 carry() 的第 61 行将一个推到向量上。

这可能是什么原因造成的?

0 投票
3 回答
2702 浏览

c++ - C++ vector push_back() overwrites on another vector of the same type?

I defined a class named nth_best_parse this way:

Then I declared vectors of this nth_best_parse as members of two different classes:

Then I run this program with gdb (by the way, I'm using Linux Ubuntu 9.04, g++ 4.3.3,GNU gdb 6.8-debian) and set a breakpoint at the end of the definition of compute_n_best_parses() with some conditions (to locate the exact call of this function I wanted, I was tracing back from a segmentation fault). When gdb hit the breakpoint, I issued a set of commands and the gdb output was like this:

Line 639 of BPCFGParser.cpp was like this:

This was a macro defined at the beginning of the file as:

By the way, class Traversal is defined as:

So actually I'm pushing something to the vector n_best_pairs_for_traversals, which is a member of an instance of the class BPCFGParser and the push_back() code somehow overwrites on the vector n_best_parses, which is a member of an instance of the class Edge. How can this ever be possible?

0 投票
3 回答
17648 浏览

java - Java 中的 push_back() 和 push_front()

java中是否有任何实现push_back()push_front()方法的集合类?

0 投票
3 回答
1922 浏览

c++ - 向量(push_back);g++ -O2;分段故障

我在使用矢量时遇到问题(在使用 push_back 时),但它仅在使用额外的 g++ 标志 -O2 时出现(我需要它)。

0 投票
2 回答
1146 浏览

boost - 在另一个 boost::ptr_vector 中推回 boost::ptr_vector<...>::iterator?

我有以下代码(只是在这里输入,可能有拼写错误或其他东西):

我现在的问题是我不能以任何方式 push_back 迭代器,我只是没有将指针从迭代器中取出。

有没有我没有看到的简单方法,或者 boosts ptr_vector 是这种情况下的错误选择?

提前致谢。

0 投票
2 回答
1061 浏览

vector - Vector 在每个 push_back 上进行重新分配

IDE - Visual Studio 2008,Visual C++

  1. 我有一个带有复制构造函数的自定义类Class1 。
  2. 我也有矢量图
  3. 使用以下代码插入数据

}

现在在每次插入时,向量都会被重新分配,所有现有内容都被复制到新位置。例如,如果向量有 5 个元素,并且如果我插入第 6 个元素,则前 5 个元素和新元素一起被复制到一个新位置(我通过在复制构造函数中添加日志语句来解决这个问题。)

然而,在使用reserve()时,这并没有按预期发生!我有以下问题

  1. 是否必须始终使用储备声明?
  2. 每次我执行push_back时,vector 是否都会重新分配;还是因为我正在调试而发生?
0 投票
2 回答
1024 浏览

c++ - 具有 2 种方法的 push_back 私有向量,一种不起作用

我有一个带有双打私有向量的类。为了访问或修改这些值,起初我使用了诸如

这工作了一段时间,直到我发现我必须为我需要的东西重载很多运算符,所以我尝试更改它以直接获取和设置向量而不是值,即

现在,假设有一个 classB,它有一个私有的 classA 元素,它也有 get 和 set 方法来读写。问题是当我试图将一个值推回 classA 中的结束向量时。

这对向量绝对没有任何作用。它保持不变,我不知道为什么......有什么想法吗?

0 投票
6 回答
12431 浏览

c++ - 向量 push_back 访问冲突

这可能是一个愚蠢的错误,但它让我疯狂地试图修复它。

我有一个结构:

还有一个类:

现在,在 addMarker 方法中:

此 push_back 导致访问冲突“OgreAR.exe 中 0x00471679 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000018。”。

作为测试,我尝试了这个:

这工作正常。

我究竟做错了什么?!谢谢!

0 投票
4 回答
5630 浏览

c++ - push_back() 是否总是增加向量的大小?

我有一段代码创建一个已知大小的 std::vector<T> :

调用 push_back 是否会将大小增加到 n+1?