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
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
  • 542
  • 4
  • 15
6
votes
1 answer

K&R C Exercise 4-9: Why ignore EOF?

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 pushed back, then implement your design". With the…
PhilPotter1987
  • 1,306
  • 2
  • 12
  • 20
6
votes
6 answers

Displaying a vector of strings in C++

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 display the stored strings as a single string. I am…
RudolphRedNose
  • 163
  • 1
  • 6
  • 16
6
votes
1 answer

In C++, is the amortized complexity of std::string::push_back() O(1)?

I know the standard specifies that it is for vectors, but what about strings?
Ari
  • 3,460
  • 3
  • 24
  • 31
5
votes
5 answers

How can I push_back a map into a vector> via an iterator?

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 it then stores in a map. The strings are…
weatherwax007
  • 97
  • 1
  • 2
  • 6
5
votes
4 answers

How do I copy the strings in a vector> to vector?

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 should contain only string elements of source vector. Is it possible…
Hardwired
  • 804
  • 1
  • 8
  • 19
5
votes
1 answer

Cannot emplace_back() a braced initializer on a vector of vectors

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 pertains to using emplace_back on a vector of vectors. Here…
24n8
  • 1,898
  • 1
  • 12
  • 25
5
votes
3 answers

Is there a back_inserter variant that takes advantage of move?

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_inserter operation. #include //…
alfC
  • 14,261
  • 4
  • 67
  • 118
5
votes
2 answers

Elegant way to push back std::array to std::vector N times

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 #include #include #include #include #include int main…
Jan SE
  • 337
  • 5
  • 13
5
votes
3 answers

c++ can't put data into vector

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 acceleration[3]; ... }; class…
havarc
  • 934
  • 5
  • 11
5
votes
6 answers

C++, with vector can I push_back({someNum1,someNum2})?

I have the vector: vector storeInventory; //storeInventory[INDEX#]{ITEMNUM, QUANTITY} and I am wanting to use the push_back() method to add new arrays to the inventory vector. Something similar to this: const int ORANGE = 100001; const int…
Brook Julias
  • 2,085
  • 9
  • 29
  • 44
4
votes
5 answers

Vector error , cannot get push_back to work

This is just a snippet of the uncommented code. The packing vector keeps causing an error at the push_back(), and I'm not quite sure why: EDIT: It has been updated to say vector* > > packing = new…
user1058359
  • 183
  • 1
  • 2
  • 13
4
votes
5 answers

Being Smart About Vector Memory Allocation

Let's say I have to iterate over a potentially very large vector of numbers and copy the even and odd elements into new, separate vectors. (The source vector may have any proportion of evens to odds; it could be all evens, all odds, or somewhere…
Maxpm
  • 24,113
  • 33
  • 111
  • 170
4
votes
3 answers

C++ vector push back crashes after object is pushed back and function exits

The problem was a stupid error from another class accessing the vector and deleting iterators. Nothing to do with the code below. Sorry to waste your time. I must be missing something elementary. I've got a function which creates an object,…
Ælex
  • 14,432
  • 20
  • 88
  • 129
4
votes
1 answer

inserting into the back of std::list during range based for loop

I came across "Add elements to a vector during range-based loop c++11", and wasn't surprised it's not allowed using std::vector because the append can invalidate iterators. However, inserting into std::list doesn't invalidate any of the iterators,…
Daniel McIntosh
  • 522
  • 3
  • 17
1
2
3
27 28