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
3
votes
2 answers

vector::push_back vs vector::operator[]

Below in c++ program, include #include using namespace std; int main() { vector numbers; numbers.push_back(2); numbers.push_back(10); numbers.push_back(5); numbers.push_back(3); …
Eight
  • 4,194
  • 5
  • 30
  • 51
3
votes
1 answer

Memory error: free(): invalid next size (fast)

I'm trying to implement a radix sort, and this code produce a memory failure: (free(): invalid next size (fast)) The code is the following: unsigned int * radix(unsigned int *x,int n, int g,bool verbose) { int elem_size = sizeof(unsigned…
RSFalcon7
  • 2,241
  • 6
  • 34
  • 55
2
votes
3 answers

How to push_back to subvector?

I have a vector< vector > and want to add a string to the inside vector with push_back(). This is what I tried: vector< vector > sorted_word_vecs; char word_read[MAX_WORD_LENGTH]; for(int i = 0; i < num_of_pipes; i++) { while…
Marty
  • 5,926
  • 9
  • 53
  • 91
2
votes
1 answer

Store data in a vector from another class

I have a vector orderQueue that is stored in a class called OrderFood and a function called processUserOption in another class called MerkelMain. I am trying to get processUserOption to store orders in that orderQueue vector using the push_back…
ahmadalibin
  • 133
  • 8
2
votes
2 answers

vector and push_back()

#include #include #include class my { public: my() { counter++; std::cout << "class constructor" << counter << " \n";} ~my() { std::cout << "class destructor" << counter << " \n"; …
GregK1
  • 21
  • 2
2
votes
2 answers

How to ensure a std::vector will not be re-allocated?

I know that when using push_back() on a vector, it is possible that that vector will be re-allocated (if there is no space left) to have more space. When using pop_back(), can the vector be re-allocated, in order to free unused space? In general,…
Yahav
  • 211
  • 3
  • 4
2
votes
5 answers

Operation of push_back on an element of Vector (C++)

I am a Python programmer trying to develop C++ proficiency. I have a silly question about vectors. Suppose I have a vector like this vector adj; Assume it contains some values. What does this operation do ? adj[v].push_back(w) Does it create…
2
votes
1 answer

What's the difference between the following ways of adding elements to a c++ vector

Snippet1: The following snippet prints out 0 1 but returns an empty vector. vector trial() { vector ret; ret.reserve(2); ret[0] = 0; ret[1] = 1; cout << ret[0] << " " << ret[1] << "\n"; return ret; } Snippet 2:…
user3828311
  • 907
  • 4
  • 11
  • 20
2
votes
1 answer

Vector empty Push_back calls for expected primary-expression before '{' token

im having trouble with my compiler Im using codeblocks with gnugcc and i want to do: table.push_back({""}); and it says main.cpp|22|error: expected primary-expression before '{' token| code works in VS and other compilers... What the…
Gadzin
  • 73
  • 7
2
votes
1 answer

does thrust::device_vector.pushback() cause a call to memcpy?

Summary I'd like some clarification on how the thrust::device_vector works. AFAIK, writing to an indexed location such as device_vector[i] = 7 is implemented by the host, and therefore causes a call to memcpy. Does device_vector.push_back(7) also…
John Mansell
  • 624
  • 5
  • 16
2
votes
1 answer

Vector of class not storing separate Textures

I'm creating a car simulation using SFML. As a matter of organization and logic, I created a single class "car", which also inherits sf::RectangleShape, and within this class there are other SFML objects, among them a Texture and a method to setup…
Rogério Dec
  • 801
  • 8
  • 31
2
votes
3 answers

Why is it unsafe to call push_back in a for loop even when there is no memory reallocation?

When I read this post: https://stackoverflow.com/a/42448319/3336423 I understand, that calling push_back in a for loop is unsafe because: If the new size() is greater than capacity() then all iterators and references (including the past-the-end…
jpo38
  • 20,821
  • 10
  • 70
  • 151
2
votes
2 answers

Why cant you push_back in 2D vector? c++

I am learning recursion in c++ and was stuck on why you cant simply use the .push_back() instead of creating a function to copy the specific_previous_result elements, then .push_back(). vector> get_every_n_elements(vector arr, int…
2
votes
2 answers

Why vector hold a class type will call the copy constructor one more time when push_back()?

I have the following code: #include using std::cin; using std::cout; using std::endl; #include using std::vector; class Quote { public: Quote() = default; Quote(const std::string &book, double sales_price): …
zhenguoli
  • 2,268
  • 1
  • 14
  • 31
2
votes
1 answer

How can I distinguish my characters in a vector? SFML 2.4.1

I am using push_back to add identical characters and for some reason these characters all have the same stats all the time. Battle.cpp: void Battle::InitalizeUser() { user.t_Pokemon.clear(); user.t_Item.clear(); user.currentPokemon_U…
MrFreeze
  • 23
  • 5