Menu
  • HOME
  • TAGS

Push Back in Map

c++,vector,containers,push-back

If you want to push_back into the vector returned by map[N], just use: //assuming std::map<int, std::vector<std::string>> my_map; int N; std::string my_string; my_map[N].push_back(my_string); ...

push_back() in vector of a Point doesn't work [closed]

c++,vector,push-back

You don't show all the code, but I think the issue is that you use push_back to add entries to the end of the vector, but you instantiated the vector with size nombrePoint, which will cause it to create nombrePoint values that are default initialized. You then push another nombrePoint...

pushing back a class object into a vector

c++,object,vector,push-back,c2664

The vector is expecting a pointer to an object whereas the vPB you are pushing back is not a pointer to an object but rather the object itself. The minimum you can do to fix this is: NV* vPB = new NV(); vPB->name = namE; ... and similarly for all...

Is there a way to push_back multiple variables into a vector in a for loop?

c++,vector,push-back

With a compiler that supports the C++11 standard you can use emplace_back, like this: vector <Book> books; books.emplace_back("a", "Jim John", 1000); books.emplace_back("b", "Jim John", 1001); books.emplace_back("c", "Billy Bill", 1002); books.emplace_back("d", "Greg Lumburge", 1003); books.emplace_back("e", "Dallas Orange", 1004); books.emplace_back("f", "Old McDonald", 1005); But easier still, with C++11, is to just list...

vector::push_back ( A * ) creating leaks?

c++,vector,push-back

If you do not have virtual destructor in your Node class, deleting your Directory objects through base class pointer is undefined behaviour. Your Directory class' s destructor won't be called, also the destructor of the contained vector. This might cause the memory leak.

R-value Reference push_back Function

c++11,move,encapsulation,rvalue-reference,push-back

Yes, you need std::move here, but you could also use emplace_back here: template <typename T> void Queue<T>::push( T && val ) { c.emplace_back( std::move(val) ); } ...

C++ vector.push_back adds objects twice when adding an object once *Solved*

c++,vector,push-back

There is nothing in the code shown that would make objects be inserted twice, so it comes down to some old-fashioned debugging skills. For a start, change the loader function to be something like: bool Level1::LoadContent() { std::cout << "Enter LoadContent with size " << SpriteList2.size() << '\n'; Sprite1 Sprite_1({0.0f,...

How to use push back method for a C++ vector

c++,vector,push-back

There are a few problems with your code: you are pre-allocating the size() of each std::vector, which means you are pre-filling them with blank values. Then you are reading into those existing elements (which is fine) and also push_back()ing what you have already stored (which is wrong). So, either stop...

C++ — Adding elements to private member field std::vector [duplicate]

c++,vector,size,field,push-back

std::vector<T> getMyVec() { return myVec; } should be std::vector<T>& getMyVec() { return myVec; } else you return a copy of your member....

garbage values for vector push_back

c++,vector,garbage,push-back

In the second case, you are indexing from firstHalfSize. You need to cout the values starting from index 0. For example: cout << "v: " << secondHalf[i-firstHalfSize] << endl; ...

Error with Push_back function for a LinkedList?

c++,linked-list,syntax-error,push-back

When the list is empty, push only a single item and return if(head == nullptr) // using nullptr instead of NULL { tail = head = new IntNode(ne); head->next = nullptr; return; } Because in case of 1st time push, the item would be inserted twice....

C++ vector only save the last push_back value

c++,memory-management,vector,push-back

I invite you over to my house, giving you my address which you hastily note down on paper. You come over and see that my house has green walls. The next day, you copy the address into your phone's address book for safekeeping. A few months later, I repaint...

structure vector push_back C++

c++,vector,struct,push-back

First, you need to change TeamS to have a list of strings for team members, not just one string. struct TeamS { int ID_NUM; std::vector<string> team_members; }; In the function to initialize teamV: void Initialize(vector <TeamS> & TeamV, const int Id[], const string m[][MEMBERS], int arraysize) { cout <<...

String not getting stored in vector?

c++,vector,push-back

"why is this happening and how do I make it so that the entire strings (including the spaces) get stored?" To get more than a single word from the input you should use std::getline(cin,n); instead of std::cin >> n; White spaces are used as delimiters by default, so each...

Can't deque.push_back() 10 million+ deques

c++,deque,push-back

This looks like address space exhaustion. If you are compiling for a 32-bit target, you will generally be limited to 2 GiB of user-mode accessible address space per process, or maybe 3 GiB on some platforms. (The remainder is reserved for kernel-mode mappings shared between processes) If you are running...

Qt containers with object

qt,containers,copy-constructor,push-back

In the case of QObject derived objects, you have to use dynamic allocation and just put pointers in the container, because such objects have unique identities and as such are prohibited from copying. In that case only the pointer is copied around, which is just an integer, the copying of...

I can't seem to get v.push.back() to work with strings

c++,string,push-back

Your first while loop reads until end-of-file... after end of file your stream state has EOF bit set, which is why that loop exits. It's also why the next attempt to cin >> Uword exits without doing anything. If you'd written something like... if (!(cin >> UWord)) { std::cerr <<...

Increase capacity for vector

c++,vector,push-back,capacity

Why do you have to create a new Vector<T>? Just create a new array: template <class T> void Vector <T> :: push_back(const T & number) { if(numItems == capacity) { capacity *= 2; T* newData = new T[capacity]; std::copy(data, data + numItems, newData); delete[] data; data = newData; } data[numItems++]...