Question
Could someone help me with adding code to the existing code? It doesn't have to work completely but I just want to get a start. If it you get it to work then great. If not then I will add or fix it if it doesn't work completely. I don't think we are supposed to make two different codes. Not sure though, but I'm assuming we add both merge and splice list data type's into the code below.
*******************************************************************************************************
Here is the question:
Add the following operation to the implementation of the list data type.
Merge(list)--assume that the list have been sorted and merge the two list to make one.
Splice(iterator, list)-- splice the list at the indicated location removing all values form the argument list.
//the given code
#include <string>
#include <list>
#include <iostream>
using namespace std;
int main()
{
list<string> names;
names.push_back("Tom");
names.push_back("Dick");
names.push_back("Harry");
names.push_back("Juliet");
// Add a value in fourth place
list<string>::iterator pos;
pos = names.begin();
pos++;
pos++;
pos++;
names.insert(pos, "Romeo");
// Remove the value in second place
pos = names.begin();
pos++;
names.erase(pos);
// Print all values
for (pos = names.begin(); pos != names.end(); pos++)
cout << *pos << " ";
return 0;
}
Explanation / Answer
hope this will help u out and it have the program u want too * an empty list is created using, e.g., list strList; * A list may be initialized with a certain number of elements. By default, if the initialization value is not explicitly mentioned, the defaultvalue or default constructor for the actual datatype is used. For example: list hello(5, string("Hello")), // initialize to 5 Hello's zilch(10); // initialize to 10 empty strings * A list may be initialized using a two iterators, e.g., to initialize a list with elements 5 until 10 (including the last one) of a vector the following construction may be used: extern vector svector; list slist(&svector[5], &svector[11]); Note that a list may be defined without size: list ivect; This defines an empty list, without any element at all. So, a statement like *ivect.begin() = 18; would in this case be an error, as there isn't any element as yet. In this case, the preferred idiom is: ivect.push_back(18); Other memberfunctions, some of which were also available in vector, are: * back(), returning the last element of the list. * clear(), * front(), returning the first element of the list. * empty(), * elements may be erased: o erase() and clear() both erase all elements, o erase(pos) erases all elements starting at the position pointed to by iterator pos, o erase(begin, end) erases elements indicated by the iterator range [begin, end). * elements may be inserted at a certain position pointed to by the iterator pos: o insert(pos, source) inserts source at the position pointed to by pos, o insert(pos, begin, end) inserts the elements in the iterator range [begin, end) at the position pointed to by pos. o insert(pos, n, argument) inserts n elements having value argument at the position pointed to by pos. The data type of argument must be equal to the data type of the the elements of the list. * resize(n) and resize(n, argument) may be used to resize the list to a size of n. If the list is expanded, the extra elements are initialized by the default value of the used datatype, or by the explicitly provided value argument. * size(), * swap(argument), swaps two lists. Also available are: * void push_front(source) to enter a new element at the head of the list. * void push_back(source) to enter a new element at the end of the list. * void pop_front() may be used to remove the first element from the list. This element is not returned by this memberfunction. * void pop_back() may be used to remove the last element from the list. This element is not returned by this memberfunction. * remove(source): This memberfunction removes all occurrences of source from the list: the two strings Hello are removed from the list object in the following example: #include #include #include int main() { list object; object.push_back(string("Hello")); object.push_back(string("World")); object.push_back(string("Hello")); object.push_back(string("World")); object.remove(string("Hello")); while (object.size()) { cout