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
0
votes
1 answer

Pushing back a vector gives absurd results (C++)

Hello so I've seen some other posts about it but they never seem to solve my issue, or I just can't understand it with my peanut sized brain. If I input something, and push it back to the vector, it would always have some stupidly absurd…
0
votes
1 answer

Vector push back a derived smart pointer into a vector of smart pointers of a base abstract class

Suppose I have the following base abstract classes class Worker { public: struct Details { std::string name; }; public: virtual void work() = 0; }; class Manager { public: virtual ~Manager() =…
Robbie
  • 287
  • 1
  • 9
0
votes
1 answer

Why push_back() will change the previous value?

I want to build a series of paths with serial numbers through a for loop, so I use a series of strcpy and strcat (I know there is a string method, but please forgive me, my technique is really poor). But after the loop, I get a series of the same…
Ahri
  • 5
  • 3
0
votes
2 answers

c++ vector pushback slice of another vector in a concise way

I mean to build a vector of M N-sized-vectors, i.e., with K=M*N total elements, from a K-sized vector with a linear arrangement of the same set of values. Moreover, I would do that in a class constructor, although I guess that is irrelevant. My code…
0
votes
1 answer

Using push_back() on a vector>

So I have a class Board header as followed: class Board { std::vector> _board; uint _rows; uint _cols; public: Board(); } And I have this default constructor: Board::Board() { …
Jewgah
  • 51
  • 8
0
votes
1 answer

vector.push_back not working on template class C++

So I made that code, it get a in file for reading, read it into a temporary variable of a object then push_back it to a vector as container. But... its not working, after the last one, all variables get the information of the last one object read in…
0
votes
0 answers

How to default initialize a vector element?

Let's say I have a class X that does not have any type of constructor unless an implicit default constructor: class X { public: int value; X(void) = default; X(int) = delete; X(const X&) = delete; X(X&&) =…
0
votes
1 answer

C++ vector of class objects and dynamic memory allocation

This is a simplified version of what I want to do, it crushes when push_back is called, specifically when the destructor is called. If I delete the body of the destructor it works, but I want to be sure that it is deleted. Instead of calloc/free I…
SVS
  • 17
  • 5
0
votes
1 answer

C++ Differences Between Pushbacks

Below is code that has different kinds of push backs. By that, I mean push backs with different inputs. An example is accounts.push_back(five). I am not sure why the constructor printouts are different for each kind of push back. Not only are the…
Pistachio
  • 3
  • 2
0
votes
2 answers

I'm getting a long, weird error when I pushback a class object to a initialized class vector

I am new to object oriented programming. I am trying to make a game called Hex. I use a static vector of Hex objects to hold active games (there can be more then one active game). But whenever I try to pushback a new game to Hex vector, I get this…
gokbeykeskin
  • 149
  • 9
0
votes
3 answers

Dealing 5 cards per player using vectors

I am trying to push_back 5 elements from vector deck_of_cards to vector player1_hand and pop_back those elements to reduce the deck_of_cards, then the next 5 elements from vector deck_of_cards for vector player2_hand, etc. I can push_back with the…
0
votes
1 answer

C++ Fizzbuzz Leetcode vector push_back() replacing all previous elements

I am having an issue where the C++ push_back() function is replacing all previous elements in a vector. Code that works is given class Solution { public: vector fizzBuzz(int n) { vector ans; for(int i=1;i<=n;i++) …
Nimitz
  • 3
  • 4
0
votes
1 answer

How to push data to a vector from a file c++

I have some table of data in a '.csv' file.I want to push those data from that file to a vector.I tried push_back.But it hasn't worked for me.I'm actually a beginner to c++.Could someone help me to sort this out? struct contacts { string name; …
0
votes
2 answers

push_back iterator in vector?

I tried to store the number 1,4,7...97 into a vector. if I use std::out << i; the for loop is working. but the push_back gives an error: base of member reference is a function perhaps you meant to call it with no arguments. i googled it and couldn't…
TR3478
  • 25
  • 6
0
votes
1 answer

How .push_back works in C++?

I have a question in my mind. Lets say I have two vectors called vector1 and vector2. vector vector1{1,2}; vector vector2{3,4}; Now I want to create a 2-D vector called vector_2d and assign these two vectors into my new 2-D vector using…
efe373
  • 151
  • 2
  • 11