Question: C++ ToString Objective:Learn basic use of templates. In this exercise you will need to implement different overloaded versions of functionToStringthat returns the contents of an

C++

ToString

Objective:Learn basic use of templates.

In this exercise you will need to implement different overloaded versions of functionToStringthat returns the contents of an container as a string. Any type of sequential container should be compatible, i.e. you need to use the container through template.

  • One version of the function gets a container type as a parameter, in which case it will return the contents of the whole container in a string.
  • Another version of the function gets beginning and ending iterators, in which case the range between iterators is printed.
  • In addition, if given a string, theToStringshould just return the string inside double quotes: "somestring".
  • When a string is given as two iterators (e.g., ToString(str.begin(), str.end()), it will be printed as sequence of characters: { f, o, o }

Apart from the single string case, the function should return the container items as comma separated list inside brackets. For example, in the case of string container elements, it would return something like the following (spaces are significant):

{ foo, bar, baz }

main.cpp

#include "to_string.hpp"

#include

#include

int main() {

std::string s = "foobar";

std::cout (s)

std::cout

std::set v;

v.insert(1.23);

v.insert(4.56);

v.insert(7.89);

std::cout

std::cout

}

Write fill to_string.hpp

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!