Insert object at index of vector c

0 votes

I have to add a new item to the existing object vector. 

Although I am aware that an iterator must be used, I am not entirely sure how it operates.

I need to insert a new item by name in the precise index that I found after some searching into an alphabetically sorted vector. 

I have this, then.

vector<Person>people;

int index =54;
Person temp;

people.push_back(temp);//insert at end of vector
people.insert(index, temp);//doesnt work for int

Can somebody advise me on how to use an iterator correctly to shift all subsequent objects by one index and put my item at the 54th element of the vector?

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
468 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In C++

+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
237 views
0 votes
1 answer

What is dynamic initialization of object in c++?

Dynamic initialization occurs when the initialization value is unknown at compile time.  To initialise the variable, it is calculated at runtime. Example, int factorial(int n) { ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,562 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
974 views
0 votes
1 answer

C++ "Object" class

No, there is no generic base class&nb ...READ MORE

answered Jun 2, 2022 in C++ by Damon
• 4,960 points
315 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
470 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
332 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
11,364 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
344 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
256 views
0 votes
1 answer

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,144 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP