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.

420 questions
134
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
  • 21,327
  • 9
  • 53
  • 91
108
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
  • 15,723
  • 4
  • 60
  • 67
99
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
Elliott
  • 1,347
  • 4
  • 17
  • 23
97
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,418
  • 3
  • 22
  • 26
48
votes
2 answers

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
  • 71,951
  • 46
  • 188
  • 305
39
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,327
  • 2
  • 22
  • 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
  • 955
  • 7
  • 12
25
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,082
  • 2
  • 18
  • 29
16
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
  • 47,916
  • 17
  • 112
  • 190
14
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
  • 143
  • 3
14
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
  • 1,025
  • 4
  • 18
  • 23
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
  • 32,406
  • 45
  • 166
  • 297
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
6 answers

In C++, will the vector function push_back increase the size of an empty array?

Quick question. Let's say I declare a vector of size 20. And then I want to add a few integers to it using push_back. vector myVector(20); myVector.push_back(5); myVector.push_back(14); Is the capacity of my vector now 22, or is it still 20?…
iaacp
  • 4,655
  • 12
  • 34
  • 39
7
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?
George
  • 8,368
  • 12
  • 65
  • 106
1
2 3
27 28