Changing elements of lists

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

Join Date: Jul 2009
Posts: 2
Reputation: wiczerd is an unknown quantity at this point 
Solved Threads: 0
wiczerd wiczerd is offline Offline
Newbie Poster

Changing elements of lists

 
0
  #1
Jul 4th, 2009
I am reading lines from a CSV and don't know what size it'll be, so I place read lines into a list of a structure "obs". Then I want to iterate through this list, put it into another list and then modify contents of the original list so that I can print that later. I am, however, not sure if I'm actually modifying the original.

  1. using namespace std;
  2. struct obs{
  3. int date;
  4. int p;
  5. int attr_price;
  6. int q;
  7. double bcode;
  8. double uniq_store_id;
  9. };
  10.  
  11. int main(){
  12. string line;
  13. ifstream fdata("jumboa_corrected.csv");
  14. obs last_obs;
  15. getline(fdata,line);
  16. int scanned = sscanf(line.c_str(),"%i,%i,%i, %lf, %lf",&last_obs.q,&last_obs.date,&last_obs.p,
  17. &last_obs.bcode,&last_obs.uniq_store_id);
  18. list<obs> this_series(1,last_obs);
  19. obs this_obs;
  20. while(getline(fdata,line)){
  21. ++gi;
  22. sscanf(line.c_str(),"%i, %i,%i, %lf, %lf",&this_obs.q,&this_obs.date,&this_obs.p,&this_obs.bcode,&this_obs.uniq_store_id);
  23. this_series.push_front(this_obs);
  24.  
  25. ... stuff that fills in a bunch of this_obs into the list
  26. if(condition to start unloading){
  27. list<obs*> qtr_ser_obs;
  28. for (list<obs>::reverse_iterator it=this_series.rbegin() ; it != this_series.rend(); ++it )
  29. {
  30. qtr_ser_obs.push_front(&*it);
  31. for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin(); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++){
  32. (*qtr_obs_it)->attr_price= computed_attr_price;
  33. }
  34. }
  35. }

In this last line, what am I modifying?
Any help would be greatly appreciated. I guess I'm really not sure what is being placed with the line " qtr_ser_obs.push_front(&*it); "
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Changing elements of lists

 
0
  #2
Jul 4th, 2009
Welcome wiczerd,

wiczerd said,
I am, however, not sure if I'm actually modifying the original.
...
In this last line, what am I modifying?
Print the list after modification. However, there is no reason to add list item into another list to modify and see; qtr_ser_obs list will add more and more list items and the inner loop is started from the very first element - qtr_ser_obs.rbegin().

  1. list<obs*> qtr_ser_obs;
  2. for (list<obs>::reverse_iterator it=this_series.rbegin() ; it !=
  3. this_series.rend(); ++it )
  4. {
  5. qtr_ser_obs.push_front(&*it);
  6. for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin
  7. (); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++)
  8. {
  9. (*qtr_obs_it)->attr_price= computed_attr_price;
  10. }
  11. }

I think it should be:
  1. for (list<obs>::reverse_iterator it=this_series.rbegin() ; it !=
  2. this_series.rend(); ++it )
  3. {
  4. list<obs*> qtr_ser_obs;
  5. qtr_ser_obs.push_front(&*it);
  6. for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin
  7. (); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++)
  8. {
  9. (*qtr_obs_it)->attr_price= computed_attr_price;
  10. }
  11. }
Last edited by adatapost; Jul 4th, 2009 at 4:51 am.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: wiczerd is an unknown quantity at this point 
Solved Threads: 0
wiczerd wiczerd is offline Offline
Newbie Poster

Re: Changing elements of lists

 
0
  #3
Jul 4th, 2009
Thanks very much. The suggestion to print my list was very helpful, and it seems that I am modifying the original objects.
My syntax got a bit screwed up because I tried cutting and pasting from a much larger program, but your catch was good.
The reason I am moving things from one list to the other is because I need to do calculations on a moving window of the data in the larger list. So I was reading everything into the larger list, and then putting snippets into the smaller one and hoping to modify the initial obs by referencing them through the subset list.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC