Question

Would someone help me custom or implementing methods Iterator insert(Iterator position, const int &data) and Iterator...

Would someone help me custom or implementing methods Iterator insert(Iterator position, const int &data) and Iterator erase(Iterator position) in class vector c++. Thank you very much.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Inserting into the vector

#include <iostream>
#include <vector>

using namespace std;

int main()
{
   vector<int> sample_vector(3,100);
   vector<int>::iterator it;
  
   cout << "Initially sample_vector contains:";
for (it=sample_vector.begin(); it<sample_vector.end(); it++)
cout << ' ' << *it;
cout <<endl;
  
   it = sample_vector.begin();
   it = sample_vector.insert ( it , 200 ); //insert at the 1st position
  
   sample_vector.insert(it+1,400); //inserting at the second position
  
cout << "After insertion sample_vector contains:";
for (it=sample_vector.begin(); it<sample_vector.end(); it++)
cout << ' ' << *it;
cout <<endl;
  
return 0;
}

OUTPUT:

//Erasing from the vector

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
vector<int> sample_vector;

// setting values (from 1 to 10) inside the vector
for (int i=1; i<=10; i++)
   sample_vector.push_back(i);
cout << "Initially sample_vector contains:";
for (unsigned i=0; i<sample_vector.size(); ++i)
cout << ' ' << sample_vector[i];
cout <<endl;

// erasing the 4th element
sample_vector.erase (sample_vector.begin()+3);
  
cout << "After erasing sample_vector contains:";
for (unsigned i=0; i<sample_vector.size(); ++i)
cout << ' ' << sample_vector[i];
cout <<endl;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Would someone help me custom or implementing methods Iterator insert(Iterator position, const int &data) and Iterator...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. void raw_push_front(const Object &x) { // insert x at the head of the list *without*...

    1. void raw_push_front(const Object &x) { // insert x at the head of the list *without* using the iterator classes // Place your code here. } 2. void raw_push_back(const Object &x) { // insert x at the tail of the list *without* using the iterator classes // Place your code here. } #ifndef LIST_H #define LIST_H #include <algorithm> using namespace std; template<typename Object> class List { private: // The basic doubly linked list node. // Nested inside of List, can...

  • //codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement...

    //codes in DynamicString.cpp #include "DynamicString.h" int DynamicString::myStringLen(const char* str){ //TODO::Implement me return -1; } DynamicString::DynamicString(){ //TODO::1::Implement me } DynamicString::DynamicString(const char* str){ //TODO::1::Implement me } int DynamicString::len() const{ //TODO::1::Implement me return -1; } const char* DynamicString::c_str() const{ //TODO::1::Implement me return nullptr; } char& DynamicString::char_at(int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::char_at(int position) const{ //TODO::1::Implement me return 'a'; } char& DynamicString::operator[](int position){ //TODO::1::Implement me char* a = new char('a'); return *a; } char DynamicString::operator[](int position) const{...

  • Mathematic Methods: Please show all working to help me understand and thank you very much for...

    Mathematic Methods: Please show all working to help me understand and thank you very much for any help. Calculate where c is a constant vector

  • Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of...

    Language: C++ Complete this function 1.Object &raw_front() { // Return the element at the front of the list *without* using the iterator classes // (You may assume the list is not empty) // Place your code here. Code: #ifndef LIST_H #define LIST_H #include using namespace std; template class List { private: // The basic doubly linked list node. // Nested inside of List, can be public // because the Node is itself private struct Node { Object data; Node *prev;...

  • Can someone please help me with this data structure class exercise? Will rate b. Given the...

    Can someone please help me with this data structure class exercise? Will rate b. Given the following sorted singly linked list 5 header 1 shown above to trace the a) Use the implemented sorted linked list and the sorted linked list L following statements. Draw the status of the new list after each statement. List L2(L1) L2.insert(5); L2 insert (7); L2. insert(4); cout<cL2; し2.erase(4); し2.erase(7); cout«<L2;

  • JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator;...

    JAVA Have not gotten the public Iterator<Item> iterator() . Can someone explain it please? import java.util.Iterator; /* * GroupsQueue class supporting addition and removal of items * with respect to a given number of priorities and with * respect to the FIFO (first-in first-out) order for items * with the same priority. * * An example, where GroupsQueue would be useful is the airline * boarding process: every passenger gets assigned a priority, * usually a number, e.g., group 1,...

  • could someone help me make a linked list in javascript, that has methods of inserting, updating...

    could someone help me make a linked list in javascript, that has methods of inserting, updating element by index, deleting by index, obtaining all the elements, obtaining elements by index, I would be very grateful to you to help me :)

  • Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support...

    Array with Iterator. Java style Implement an array data structure as a class JstyArray<E> to support the Iterable interface such that the following code works: JstyArray<Integer> data; data = new JstyArray<Integer>(10); for (int i = 0; i < 10; ++i) { data.set(i, new Integer(i) ); } int sum = 0; for ( int v : data ) { if (v == null) continue; // empty cell sum += v; } The iterator provided by this class follows the behaviour of...

  • This is a C++ program about link list, please help me complete the codes, thanks!(the .h...

    This is a C++ program about link list, please help me complete the codes, thanks!(the .h file has been provided) /* * List.h * * Class Description: List data collection ADT. * Class Invariant: Data collection with the following characteristics: * - Each element is unique (no duplicates). * - (What other characteristic does our List have?) * * * */ #pragma once // You can add #include statements if you wish. #include <string> #include "Patient.h" using namespace std; class...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT