Question

Hi, I have C++ programming problem here:

Problem:

Please modify your string type vector in for push_back() function as below:

void push_back(string str)
{
// increase vector size by one
// initialize the new element with str
}

In addition, the standard library vector doesn't provide push_front(). Implement
push_front() for your vector. Test your code with the main function below.

int main()
{
   vector v1(3);
   cout<<"v1: ";
   v1.print(); // this should display -, -, -
   for (int i=0; i    string s = NumberToString(i);
   v1[i]= s;
   }
   v1.push_back(“back”);
   v1.push_front(“front”);
   v1.print(); // this should display front, 1, 2, 3, back
}

Solution:

Actually I have tried. Here is my program (containing class.h, class.cpp, and main.cpp file) :

Class definition file (labten.h) Class implementation file (labten.cpp) Client file (main.cpp) 2 3 4 #ifndef LABTEN_H #define

I have tried to store (line 37-42 at labten.cpp file), but it's not printing.

Therefore, can you please help me to fix it?

Note: if you need, you can get the three files source codes from my google drive folder here:

https://drive.google.com/open?id=1p1EC-lL0-lQU2icMfUvzI9izoR7lU5NK

Thank you.

Best Regards

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

Here is the c++ code for above problem

#include<iostream>
#include<String>
#include<vector>
#include<sstream>
using namespace std;

class Vector
{
   public:
       vector<string> elements;
       Vector(int size)
       {
           elements.reserve(size);
       }
       void push_back(string);
       void push_front(string);
       void print();
       void insert(int,string);
};

void Vector::push_back(string element)
{
elements.push_back(element);  
}
void Vector::insert(int index,string element)
{
elements.insert(elements.begin()+index,element);
}
void Vector::push_front(string element)
{
elements.insert(elements.begin(),element);
}

void Vector::print()
{
   for(unsigned int i = 0;i<elements.size();++i)
   {
       cout<<elements[i]<<",";
   }
}

int main()
{

   Vector v1(3);
   for(int i=0;i<10;++i)
   {
       stringstream ss;
       ss<<i;
       string s= ss.str();
       v1.insert(i,s);
   }
   v1.push_front("back");
   v1.push_back("front");
   v1.print();
   return 0;
}

Here I have implemented three methods

1. push_back() to push string element in the back of the vector

2. push_front() to push string element in the front of the vector using inbuilt vector function insert

3. insert() this methods inserts a value at any specified location in the vector

The class contains one vector<string> internally to manage all the elements and sstream is used to convert numbers to string.

Add a comment
Know the answer?
Add Answer to:
Hi, I have C++ programming problem here: Problem: Please modify your string type vector in for...
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
  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • Please help me modify the Stash3.cpp and Stash3.h files below to utilize default arguments in the...

    Please help me modify the Stash3.cpp and Stash3.h files below to utilize default arguments in the constructor. Please test the constructor by creating two different versions of a Stash object. Please see the source code below: //Stash3.cpp //: C07:Stash3.cpp {O} // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Function overloading #include "Stash3.h" #include "../require.h" #include <iostream> #include <cassert> using namespace std; const int increment = 100;...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the...

    C++ Programming Hi! Sorry for all the material attached. I simply need help in writing the Facility.cpp file and the other files are included in case they're needed for understanding. I was able to complete the mayday.cpp file but am stuck on Facility. The following link contains a tar file with the files provided by the professor. Thank you so much in advanced! http://web.cs.ucdavis.edu/~fgygi/ecs40/homework/hw4/ Closer.h: #ifndef CLOSER_H #define CLOSER_H #include <string> #include "gcdistance.h" struct Closer { const double latitude, longitude;...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • 02. Log N Vector Due Sunday by 11:59pm Points 149 O(log(N)) Vector std::vector is pretty cool...

    02. Log N Vector Due Sunday by 11:59pm Points 149 O(log(N)) Vector std::vector is pretty cool but it has one big problem: every once in a while, push_back has to create a whole new array and copy a bunch of elements which is O(n)! Luckily, we can do better, in terms of Big-O. The goal of this homework is to write a class that behaves like a vector but without the O(n) push_back. Whenever we run out of space, we'll...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

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