944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 319
  • C++ RSS
Oct 26th, 2009
0

C++ list.

Expand Post »
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}. 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 :
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. template <class T>
  6. T integrateList(const& list<T> i, const& list<T> m)
  7. {
  8. list<T>::const_iterator iter;
  9. what exactly do I need to do here?I know how I Can merge the two list?
  10. But how can I integrate or collate them?
  11.  
  12. }
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. list<int> listA;
  4. list<int> listB;
  5. list<int> newList;
  6.  
  7. listA.push_back(1);
  8. listA.push_back(2);
  9. listA.push_back(3);
  10. listA.push_back(4);
  11. listA.push_back(10);
  12. listA.push_back(11)
  13.  
  14.  
  15. listB.push_back(5);
  16. listB.push_back(6);
  17. listB.push_back(7);
  18.  
  19. newList = integrateList(listA, listB);
  20. cout<<newList <<endl;
  21. return 0;
  22. }

Thank you for any help.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ list.
Is merging the 2 lists into a 3rd list an option for you?
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 26th, 2009
0
Re: C++ list.
How can you merge two lists of different size? Is it even possible? I thought you can only merge two lists which are of the same size.
After merging two lists, the original lists will be empty..no? If so, that is not my options for sure!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ list.
Something like this : ?

1) Create a result list
2) Merge list A and list B
3) copy result list to list A, while making sure that list A has enough add at each insertion.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 26th, 2009
0
Re: C++ list.
Something like this : ?

1) Create a result list
C++ Syntax (Toggle Plain Text)
  1. T inteGrateList(const list<T> i, const list<T> m)
  2. {
  3. list<T>::const_iterator p;
  4. list<T> resultingList;
  5. i.merge(m) // merge list m in i; now m is empty
  6.  
  7.  
  8. }
2) Merge list A and list B
3) copy result list to list A, while making sure that list A has enough add at each insertion.
This will basically merge the two lists but I wont get the output the way I desire. I think it will join the tail of one list with the head of another list!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ list.
anyone?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
complexcodes is offline Offline
57 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ list.
>>But I have no idea how can I integrate one list onto another.

What requirements comes to mind when you think this?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Extra Paramaters
Next Thread in C++ Forum Timeline: I don't know how to make this float function work correctly.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC