C++ list.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training

C++ list.

 
0
  #1
Oct 26th, 2009
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 :
  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. }
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 393
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 40
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Whiz
 
0
  #2
Oct 26th, 2009
Is merging the 2 lists into a 3rd list an option for you?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training
 
0
  #3
Oct 26th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,336
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 166
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
Oct 26th, 2009
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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training
 
0
  #5
Oct 26th, 2009
Originally Posted by firstPerson View Post
Something like this : ?

1) Create a result list
  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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 57
Reputation: complexcodes is an unknown quantity at this point 
Solved Threads: 1
complexcodes complexcodes is offline Offline
Junior Poster in Training
 
0
  #6
Oct 26th, 2009
anyone?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,336
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 166
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
Oct 26th, 2009
>>But I have no idea how can I integrate one list onto another.

What requirements comes to mind when you think this?
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC