push-back Questions

10

Solved

vector<int> 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...
Erbium asked 13/9, 2013 at 14:27

2

Solved

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, whil...
Erupt asked 17/5, 2014 at 23:33

8

Solved

Currently when I have to use vector.push_back() multiple times. The code I'm currently using is std::vector<int> TestVector; TestVector.push_back(2); TestVector.push_back(5); TestVector.push_...
Demisemiquaver asked 28/1, 2013 at 12:21

4

Solved

I'm using GCC 9.2.0 and boost 1.55. I have 2 vectors: vector< pair< string, int > > source; vector< string > dest; I need to transform the source vector to the dest, such that it...
Debate asked 19/1, 2021 at 14:44

3

Solved

In generic code I was trying to tell an output iterator (in practice a std::back_inserter_iterator to move a range of elements. To my surprise it looked as if elements were moved in a move-to-back_...
Barrelchested asked 22/1, 2019 at 8:12

2

Solved

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 ad...
Kepner asked 2/3, 2020 at 19:13

5

Solved

I'm currently studying the book Accelerated C++ (Koening/Moo) and I'm having trouble with one of the exercises. The task is to write a program which takes as an input some sequence of words which i...
Fakery asked 24/1, 2012 at 23:28

1

Solved

This is somewhat related to a previous question I asked regarding using emplace_back on a vector of pairs. emplace_back() vs push_back when inserting a pair into std::vector Now my question pertai...
Excusatory asked 6/6, 2019 at 19:1

2

Solved

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 c...
Goldfinch asked 23/10, 2018 at 9:29

2

Solved

the following codes pushed back an std::array to a std::vector N times. Is there a more elegant and shorter way of doing this? #include <iostream> #include <vector> #include <array&...
Vinia asked 19/9, 2018 at 8:56

4

Solved

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. T...
Eschar asked 25/10, 2013 at 10:47

5

Solved

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<subject> sub; So now how can I add ...
Inescutcheon asked 9/11, 2011 at 15:33

6

Solved

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)....
Sachsse asked 5/4, 2013 at 16:13

4

Solved

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 complexit...
Forworn asked 26/2, 2013 at 5:41

2

Solved

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 alrea...
Bauhaus asked 11/11, 2014 at 8:41

1

Solved

Just a little confusion I'm hoping someone can clear up - this question asks: "Our getch and ungetch do not handle a pushed-back EOF correctly. Decide what their properties ought to be if an EOF is...
Orianna asked 15/9, 2013 at 23:0

5

I'm sorry if this is a repeat question but I already tried to search for an answer and came up empty handed. So basically I just want to add strings (single words) to the back of a vector and then ...
Cowpoke asked 30/7, 2013 at 4:12

3

Solved

I have a data-structure and a processor-class for the data, the data is stacked without pointers for faster SIMD processing: struct trajectory_data { float position[3]; float velocity[3]; float...
Liederkranz asked 20/6, 2013 at 15:49

1

Solved

I came up with the following snipped but it looks quite hacky. vector<int> collection; collection.push_back(42); int *pointer = &(*(collection.end()--)); Is there an easy way to get a ...
Abrahamabrahams asked 19/6, 2013 at 20:11

2

std::vector<std::vector< std::pair<int, int> > > offset_table; for (int i = 0; i < (offset.Width()*offset.Width()); ++i) { offset_table.push_back( std::vector< std::pair...
Concessionaire asked 26/4, 2013 at 0:24

3

Solved

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 ano...
Pantoja asked 21/2, 2013 at 18:16

0

I've been scratching my head for far too long, I'm finding using MPL very difficult to get around and hoping somebody can get me started. Here is some partial code from a class I am developing whic...
Dulciana asked 18/3, 2013 at 6:47

1

Solved

I know the standard specifies that it is for vectors, but what about strings?
Coexecutor asked 15/12, 2012 at 14:18

3

Solved

Possible Duplicate: What is The Rule of Three? I have the a problem of the double freeing of memory in the following program. The debugger shows that the issue is in the push_back() ...
Unstudied asked 12/11, 2012 at 16:10

7

Solved

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...
Taneshatang asked 26/10, 2011 at 7:48

© 2022 - 2025 — McMap. All rights reserved.