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
1
vote
3 answers

Typecasting for vector pushback operation in C++

I have two vectors std::vector outputStack, operatorStack; At some point, I need to pop some elements out of one stack and push it into another stack. while(operatorStack.back().compare(L_BRACKET)) { …
user592748
  • 1,194
  • 3
  • 21
  • 45
1
vote
1 answer

why vector push_back> not work in a for loop for my defined class

I want use std::vector.push_bask to store 20 numbers of shared_ptr in a for loop. But from the output: it seems it only pushed one. And vector::push_back> 20 times worked as designed. Could some body help me out? class IObserver{ public: int…
1
vote
1 answer

when i get an index of a vector in c++, it always returns last value that is push backed

I create a global vector. After that I add values with push_back function. But, when I want to get an arbitrary index of vector, it always returns the last value that was pushed. The code is the following: struct Choromosome{ float fitness; …
user1914367
  • 171
  • 1
  • 2
  • 12
1
vote
1 answer

push_back a struct into a vector in a struct

I have the following structs: enum messageType {New = 0, Old = 1, No_Message = 2}; typedef struct { enum messageType type; unsigned int senderID; char message[100]; } StoredMessageData; struct StoredMessage { unsigned int…
Gossamer Shadow
  • 387
  • 1
  • 5
  • 16
1
vote
2 answers

Push_back a variable to vector

Just started learning STL and here is the first problem: vector vec1; for(int i = 1; i <= 100; i++) { vec1.push_back(i); cout << vec1[i] << endl; } As you may see i want to push back variable i to vector vec1 but output…
1
vote
2 answers

C++ Input data to vector that is of a user-defined type?

I have to create a banking system program which adds, deletes, looks up, saves to file etc. For this I am required to use a Vector matching the private data member below and it is suggested to use the push_back() function to add new accounts. The…
user1550409
1
vote
2 answers

Vector push_back overwrites all entries

I want to use the push_back function to fill my vector with lines from a text file. But it overwrites all entries with the last line. Here is the source code: int main() { std::vector lines; FILE* file; file =…
Homer
  • 113
  • 1
  • 2
  • 6
1
vote
1 answer

vector push_back not working in class object

I am making an hierarchy of sorts and am having trouble adding an element to a vector. I simplified the code and still cannot add an element to the vector as expected. The hierarchy looks like: Pdb > Chain > strings Pdb and Chain are class names and…
NCAggie
  • 97
  • 2
  • 6
1
vote
4 answers

manipulation of Vectors created with new

Can anyone help with this... vector *vVec = new vector; vVec .reserve(frankReservedSpace); start = std::clock(); for(int f=0; f
Columbo
  • 2,896
  • 7
  • 44
  • 54
1
vote
3 answers

Array operation, adding element at the end, pushing the other elements back

I'm planning to do a small program that'll display a graph which will be updated a few times per second (maybe 100/200ms or so). The purpose is to plot over 1000 different values in the graph, somewhat like an XY plot. When the array contains 1000…
user1240989
  • 619
  • 1
  • 6
  • 5
1
vote
2 answers

push_back issue std::vector

OSX 10.7, XCode 4. I have a small problem: I need to push back a 2D vector> in to a 3D Vector, after a process of selection. It throws the EXC_BAD_ACCESS error in the push_back function in the stl::vector template. It seems like it…
Rhys Davies
  • 69
  • 1
  • 9
0
votes
1 answer

String::push_back() does not push back

I am currently writing a infix to postfix converter. It works pretty well except I have problems adding the rest of the Stack to the postfix. The stack is a vector and the postfix is a String. After I'm done reading everything I try the…
Teraku
  • 13
  • 4
0
votes
2 answers

Adding a object having a string member to a vector

class A { public: string name; float length; float weight; bool isEnabled; }; When I'm doing push_back() on the vector of this class, it works for the first time, but doesn't on the subsequent push_back() calls. Could it…
0
votes
1 answer

(C++) Do different functions need assignment while others do not?

Can someone clarify/correct me on why certain functions such as push_back() do not need an assignment to output the new string, while a function such as substr() needs to be assigned to a string in order for the output to be the new subs-string? For…
user22099685
0
votes
2 answers

What could be causing the 'expected expression' error when pushing a std::pair to a vector in my graph code using VSCode?

#include #include #include #define ii pair using namespace std; // DSK -> DSC int n; vector edge; string s, num; int main() { cin >> n; cin.ignore(); for(int i = 1; i <= n; i++) { …