Questions tagged [push-back]

is the action of adding an element at the end of a container.

push-back is the action of adding an element at the end of a container.

389 questions
130
votes
9 answers

Is it safe to push_back an element from the same vector?

vector v; v.push_back(1); v.push_back(v[0]); If the second push_back causes a reallocation, the reference to the first integer in the vector will no longer be valid. So this isn't safe? vector v; v.push_back(1); v.reserve(v.size() +…
Neil Kirk
  • 20,002
  • 5
  • 48
  • 79
97
votes
2 answers

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object? For instance, is…
Riot
  • 13,698
  • 3
  • 55
  • 59
81
votes
5 answers

Vector of structs initialization

I want know how I can add values to my vector of structs using the push_back method struct subject { string name; int marks; int credits; }; vector sub; So now how can I add elements to it? I have function that initializes string…
SRN
  • 2,144
  • 3
  • 19
  • 26
64
votes
8 answers

How do I pass multiple ints into a vector at once?

Currently when I have to use vector.push_back() multiple times. The code I'm currently using is std::vector TestVector; TestVector.push_back(2); TestVector.push_back(5); TestVector.push_back(8); TestVector.push_back(11); …
Elliott
  • 987
  • 3
  • 15
  • 22
43
votes
1 answer

Why emplace_back is faster than push_back?

I thought that emplace_back would be the winner, when doing something like this: v.push_back(myClass(arg1, arg2)); because emplace_back would construct the object immediately in the vector, while push_back, would first construct an anonymous object…
gsamaras
  • 66,800
  • 33
  • 152
  • 256
34
votes
4 answers

C++ std::string append vs push_back()

This really is a question just for my own interest I haven't been able to determine through the documentation. I see on http://www.cplusplus.com/reference/string/string/ that append has complexity: "Unspecified, but generally up to linear in the…
Memento Mori
  • 3,117
  • 2
  • 20
  • 29
32
votes
2 answers

Weird behaviour with class fields when adding to a std::vector

I have found some very weird behaviour (on clang and GCC) in the following situation. I have a vector, nodes, with one element, an instance of class Node. I then call a function on nodes[0] that adds a new Node to the vector. When the new Node is…
Qq0
  • 935
  • 4
  • 10
23
votes
6 answers

vector of vectors push_back

I'm designing a multilevel queue process simulator in C++ but I've got a problem when trying to implement several queues (my queues are vectors).So, "multilevel" is a 4 elements array (not vector). Inside each of those elements there is a vector…
karl71
  • 1,034
  • 2
  • 14
  • 27
13
votes
7 answers

What happens under the hood of vector::push_back memory wise?

My question is regarding the effect of vector::push_back, I know it adds an element in the end of the vector but what happens underneath the hood? IIRC memory objects are allocated in a sequential manner, so my question is whether vector::push_back…
dtech
  • 44,350
  • 16
  • 93
  • 165
13
votes
2 answers

std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

I have a class with a deleted move constructor and when I try to call std::vector::push_back() in MSVC (v.15.8.7 Visual C++ 2017) I get an error saying that I am trying to access the deleted move constructor. If however I define the move…
Brent
  • 133
  • 3
12
votes
2 answers

How would one push back an empty vector of pairs to another vector?

std::vector > > offset_table; for (int i = 0; i < (offset.Width()*offset.Width()); ++i) { offset_table.push_back( std::vector< std::pair > ); } This is my code, but I am getting the…
pearbear
  • 903
  • 4
  • 16
  • 22
10
votes
1 answer

How to get a pointer to last inserted element of a std::vector?

I came up with the following snipped but it looks quite hacky. vector collection; collection.push_back(42); int *pointer = &(*(collection.end()--)); Is there an easy way to get a pointer to the last inserted element?
danijar
  • 27,743
  • 34
  • 143
  • 257
10
votes
3 answers

C++ push_back vs Insert vs emplace

I'm currently making an application using vectors with C++. I know how pre-optimization is the root of all evil. But I really can't help being curious. I'm adding parts of other vectors into another vector. We'll say the vector will have a size that…
Darkalfx
  • 259
  • 3
  • 12
7
votes
4 answers

C++ reference changes when push_back new element to std::vector

I am not sure what to make of this - please tell me what's wrong with the code below. I modified my code to reduce it to the simplest terms. There is a std::vector with a bunch of MyNode objects. The first step is to get a constant reference to one…
Phoeniyx
  • 532
  • 4
  • 14
6
votes
3 answers

push_back() and push_front() in Java

Is there any collection class in java, that implements push_back() and push_front() methods?
folone
  • 8,177
  • 11
  • 61
  • 105
1
2 3
25 26