Question: write C++ Code SSample Code // Created by Y. Wu, Jan 31, 2023 // Merge lists (of any STL container type) into a single sorted
write C++ Code
SSample Code
// Created by Y. Wu, Jan 31, 2023
// Merge lists (of any STL container type) into a single sorted list (of any STL container type)
// Duplicates allowed unless the output doesn't take duplicates (e.g., a map)
// Note: you should make your code as flexible as possible: you should allow all kinds of outputs: vector, set, etc
// Make your code short: at most 10 lines of code (LOC)
// now defiine ECMergeContainers ...
Test Case Code
// Test merge containers. To compile: c++ ECMergeContainersTest.cpp -o test
#include "ECMergeContainers.cpp"
#include
#include
#include
using namespace std;
template
void ASSERT_EQ(T x, T y)
{
if( x == y )
{
cout
}
else
{
cout
}
}
void Test1( )
{
vector
vector
lis1.push_back(1);
lis1.push_back(3);
lis1.push_back(2);
vector
lis2.push_back(3);
lis2.push_back(1);
listListNums.push_back(lis1);
listListNums.push_back(lis2);
vector
ECMergeContainers(listListNums, lisOut);
ASSERT_EQ((int)lisOut.size(), 5);
ASSERT_EQ(lisOut[0], 1);
ASSERT_EQ(lisOut[1], 1);
ASSERT_EQ(lisOut[2], 2);
ASSERT_EQ(lisOut[3], 3);
ASSERT_EQ(lisOut[4], 3);
}
void Test2()
{
set
set
lis1.insert(1);
lis1.insert(3);
lis1.insert(2);
set
lis2.insert(3);
lis2.insert(1);
listListNums.insert(lis1);
listListNums.insert(lis2);
vector
ECMergeContainers(listListNums, lisOut);
ASSERT_EQ((int)lisOut.size(), 5);
ASSERT_EQ(lisOut[0], 1);
ASSERT_EQ(lisOut[1], 1);
ASSERT_EQ(lisOut[2], 2);
ASSERT_EQ(lisOut[3], 3);
ASSERT_EQ(lisOut[4], 3);
}
int main()
{
Test1();
Test2();
}
1 Merging containers You are to implement a function called ECMergeContainers, which takes two parameters: (i) a list (container) of some lists (containers), and (ii) a container for output. Here, container is a STL container e.g., vector, set, etc. ECMergeContainers will merge all the lists of containers into a single container (which should be sorted). 1. Your code must be as flexible as possible. That is, you should allow different kinds of STL containers to be the input and output. 2. Your code must be concise: use no more than 10 lines of code inside the function! 3. For output, some containers allow duplicates and you should keep the duplicate. If the output container doesn't allow duplicate (like set), then it is OK to discard duplicate
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
