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

garbage values for vector push_back

I'm trying to assign an array's values to a vector. It seems to be working fine for one vector, but when I do it for a second, I'm getting back garbage values. I cout the number and I know it's correct, but it's not assigning correctly. I don't…
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
1
vote
1 answer

vector.push_back fails allocating memory

Im new to programming and can't solve the following error. The dynamic allocated memory seems to be faulty. I already tried to google for people with a similar problem. "Windows has triggered a breakpoint" at _CrtIsValidHeapPointer Call…
Alex
  • 13
  • 1
  • 5
1
vote
2 answers

pushing back an boost::ptr_vector<...>::iterator in another boost::ptr_vector?

I have the following code (just typed it in here, might have typos or stuff): typedef boost::ptr_vector tvec; tvec v; // ... fill v ... tvec vsnap; for(tvec::iterator it = v.begin(); it != v.end(); ++it) { if((*v).anyCondition) …
Ethan Nash
  • 11
  • 2
1
vote
2 answers

Does std::vector size()-1 ALWAYS give the index of the last element?

Say I have a std::vector named myVec. I don't know the exact size it will reach, so i use only myVec.push_back(...). Can I rely on the fact that after I have pushed_back, size()-1 will give me the index of that last element? Thanks.
G.Rassovsky
  • 774
  • 1
  • 10
  • 23
1
vote
1 answer

C++ map > segmentation fault

I'm trying to read in a directed graph adjacency list from a file. I am storing it in a map of each node to a vector of the nodes it is connected with. Below is an example line of input for the nodes connected to node 1. 1 37 79 164 155 32 87 …
Aakash Jain
  • 1,963
  • 17
  • 29
1
vote
4 answers

pushing back a class object into a vector

I have a vector that takes in a class object but when I try to push back the object I have created into the vector I am getting these problems and don't know how to getnaround it. Can anyone help me please ? void populate( std::vector vNav,…
1
vote
3 answers

Vector (push_back); g++ -O2; Segmentation fault

I'm having problem with vector, (in the usage of push_back) but it only appears when using additional g++ flag -O2 (I need it). #include #include typedef std::vector node; typedef std::vector graph; int main() { …
user85423
  • 21
  • 3
1
vote
1 answer

Push_back on a 2D vector through for loop

I am trying to learn C++ and came across this problem statement CHALLENGE DESCRIPTION: Our marketing department has just negotiated a deal with several local merchants that will allow us to offer exclusive discounts on various products to our top…
Dexobox
  • 125
  • 1
  • 9
1
vote
1 answer

Seg Fault resulting from push_back call on vector (threads linux)

So what I'm trying to do is write a program that creates a series of child threads that take the arguments using the pthread_create method and uses the parameter passed in to do more manipulation and so on. The parameter I'm trying to pass in is a…
1
vote
1 answer

Is there a push back time limit for vector

For c++ STL we can insert new value by using "push_back", is there a push_back times limit if we never pop_back? In other words, what is the maximum size of vector? Because when declare vector, we don't need to specify the size of vector, so I am…
hellocoding
  • 221
  • 4
  • 13
1
vote
2 answers

Crash from a push_back() in a std vector

This program works as expected: #include #include #include using namespace std; struct Thumbnail { string tag; string fileName; }; int main() { { Thumbnail newThumbnail; …
Pietro
  • 12,086
  • 26
  • 100
  • 193
1
vote
1 answer

insert vector element from vector's element member method destroys *this

Can someone explain me why this C++ code it's behaving this way? And .. how can i avoid this 'vector' problems in the future. #include #include using namespace std; struct my_str { int x; my_str() { x = 0; } …
Alex Velea
  • 29
  • 2
1
vote
3 answers

Causing push_back in vector to segmentaion fault on what seems to be simple operation

I'm working on a program for Project Euler to add all the digits of 2^1000. So far I've been able to track the program segmentation faults when it reaches around 5 digits and tries to push a one onto the vector at line 61 in the function carry(). …
Sparky
  • 2,694
  • 3
  • 21
  • 31
1
vote
2 answers

Weird error with vectors in C++ when trying to push_back an instance

I'm trying to do a push_back but Code::Blocks sends me a weird error. I have debugged this code many many times but I can't identify the error. The method LoadFromConsole has the line with the error. Specifically the error occurs when I go through…
Ranguro
  • 27
  • 4
1
vote
2 answers

Effects of vector pushback on element address

I have a vector of class objects. A function randomly choses two elements and return their addresses. Now using these two elements, I want to generate two new objects of the same class and add them to the vector by using push_back. Here is the…
jazzybazz
  • 1,807
  • 4
  • 17
  • 21