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

Error regarding Copy constructor and pushback method of vector

using namespace std; class test { public: int p; test() { cout << "base" << endl; } test(int x) { p = new int;* p = x; cout<<"args" << endl; } test(test & args) { p = new int; …
gabbar
  • 49
  • 1
  • 8
-1
votes
3 answers

Is there a more efficient way for storing vector> ? C++

The real file I read is about 15.000 kBs. Thus I am not sure whether this is the best way of storing such a data structure. Here is the code. string line; ifstream File; vector> v; string filename =…
-1
votes
1 answer

error: no matching member function for call to 'push_back'/ String vectors

The problem is like this: Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2. EX: Input: a1= arp, live, strong. a2 = lively, alive, harp, sharp,…
Maria
  • 71
  • 10
-1
votes
4 answers

How to push_back() C array into std::vector

This doesn't compile: vector v; int p[2] = {1, 2}; v.push_back(p); //< compile error here https://godbolt.org/z/Kabq8Y What's the alternative? I don't want to use std::array. The std::vector declaration on itself compiles. It's the…
tuket
  • 3,232
  • 1
  • 26
  • 41
-1
votes
2 answers

C++ push_back many 1D vectors in a 2D vector

Let's say i have a lot of vectors : vector v1 = ... vector v2 = ... ... vector v1000 = ... Is there a quicker way to populate a 2D vector with those 1D vectors than this : vector> vGlobal…
Viroun
  • 1
-1
votes
1 answer

Why vector.push_back(s) does not work here but vector[i]=s works(mentioned as comment)?

Why vector.push_back(s) does not work here but vector[i]=s works (mentioned as comment)? If I use the push_back function, nothing gets printed. vec[i]=s works perfectly fine, but why is the push_back not working? I expect both the alternatives to…
-1
votes
1 answer

Does push_back change the structure of type stored in its vector?

I have following 2 classes: class Core { public: Core() : mid(0), fin(0) {} Core(std::istream &in) { read(in); } virtual ~Core() {} Str name() const { return n; } virtual double grade() const …
milanHrabos
  • 2,010
  • 3
  • 11
  • 45
-1
votes
3 answers

insert string into vector character by character

I'm trying to insert the characters of the string into a char vector but place the letters in reverse order . can anyone tell me why this doesn't work int main() { string a = "Hello"; vector arr(5); for(int i = 4 ; i == 0 ;…
-1
votes
1 answer

Are these variants of std::vector::push_back constant or linear?

We all know that std::vector::push_back has constant (amortized) complexity. It is constant because we say that the amortization cost is negligible, doubling every time. With reallocation being linear complexity. Let's change our std::vector…
dau_sama
  • 4,247
  • 2
  • 23
  • 30
-1
votes
1 answer

c++ - runtime error: vector push_back()

Please tell me reason why this code have runtime error. I think that addEmployee() function is problem. #include #include #include using namespace std; enum EmployeeLevel {fresh, sophomore, junior, senior}; class…
doubleU
  • 13
  • 2
-1
votes
1 answer

How can I avoid extra copying when constructing a vector to be returned?

C++ newbie here! I wrote a code who is considering a WORLD which made of a vector of CITIES. This world gets updated nbTimeStep times. Here is a rough summary of what my code looks like. For ease of reading, all class names are in all caps. //…
Remi.b
  • 17,389
  • 28
  • 87
  • 168
-1
votes
1 answer

Push_back is replacing current point instead of adding a new one to the vector

I am having trouble appending new points onto the end of my current Vector of points. What it is currently doing is overwriting the existing vector with the new point I am trying to add, leaving the vector with only 1 element. This is the class…
James Mallett
  • 827
  • 4
  • 11
  • 27
-1
votes
1 answer

Inserting in a list changes values c++

I want to create multiple "voxel"-objects and put them in a list. But if I do so two of the values, that are stored by voxel, change. Here is the class definition: class Voxelization{ private: int sf_counter; bool subdiv; int seg_fac; int*…
Sven
  • 81
  • 1
  • 13
-1
votes
1 answer

c++ adding objects to vector destroys earlier objects

I need to add objects of the same class to a vector: #include #include class A { int *array; int size; public: A(int s) { array = new int[size = s]; fprintf(stderr, "Allocated %p\n", (void*)array); …
pVinken
  • 23
  • 4
-1
votes
3 answers

What exactly happens when I do buffer1.push_back(buffer2.front()) in C++?

Help me understand this. Here is the context. I am writing a program in C++. I have 2 buffers (deque). Let's call them buffer1 and buffer2; I have 2 threads: one thread is filling buffer1 with random values. The other one is copying the oldest…
Filipe Santos
  • 61
  • 2
  • 9