C++ list

Hi all,

I am playing around with list and I figure out how to merge two lists and all. But I have no idea how can I integrate one list onto another. For example A = {1,2,3,4,10,11} and B = { 5,6,7} Then my integrated list should be
{1,5,2,6,3,7,4,10,11}. I would have figured out of two lists are of the same length but they aren't. Two lists can be varied in terms of size.
I am writing a free function so here is my approach :

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <list>
using namespace std;

template <class T>
T integrateList(const& list<T> i, const& list<T> m)
{
  list<T>::const_iterator iter;
  what exactly do I need to do here?I know how I Can merge the two list?
  But how can I integrate or collate them?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
  list<int> listA;
  list<int> listB;
  list<int> newList;

  listA.push_back(1);
  listA.push_back(2);
  listA.push_back(3);
  listA.push_back(4);
  listA.push_back(10);
  listA.push_back(11)

 
  listB.push_back(5);
  listB.push_back(6);
  listB.push_back(7);

  newList = integrateList(listA, listB);
  cout<<newList <<endl;
  return 0;
} 


Thank you for any help,
And what is a question?
Instead of an integrateList, why not have an appendList and append a to newList then b to newList, then sort newList?
If you don't want duplicates either, once you have sorted the list, apply the unique() STL algorithm to it.
http://www.cplusplus.com/reference/algorithm/unique/
Also, if this is just an exercise to learn an STL container, consider using the std::set<> template instead,
which will give you both the uniqueness and the ordering of the elements you desire.
Sorry,jsmith. But I didn't get you.
std::set<> is a container that does not allow you to insert duplicates, and by default when you walk
the container begin() to end() will return the contained values already sorted, lowest to highest (by default).
Topic archived. No new replies allowed.