| | |
Changing elements of lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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 "
C++ Syntax (Toggle Plain Text)
using namespace std; struct obs{ int date; int p; int attr_price; int q; double bcode; double uniq_store_id; }; int main(){ string line; ifstream fdata("jumboa_corrected.csv"); obs last_obs; getline(fdata,line); int scanned = sscanf(line.c_str(),"%i,%i,%i, %lf, %lf",&last_obs.q,&last_obs.date,&last_obs.p, &last_obs.bcode,&last_obs.uniq_store_id); list<obs> this_series(1,last_obs); obs this_obs; while(getline(fdata,line)){ ++gi; 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); this_series.push_front(this_obs); ... stuff that fills in a bunch of this_obs into the list if(condition to start unloading){ list<obs*> qtr_ser_obs; for (list<obs>::reverse_iterator it=this_series.rbegin() ; it != this_series.rend(); ++it ) { qtr_ser_obs.push_front(&*it); for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin(); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++){ (*qtr_obs_it)->attr_price= computed_attr_price; } } }
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); " Welcome wiczerd,
wiczerd said,
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().
I think it should be:
wiczerd said,
•
•
•
•
I am, however, not sure if I'm actually modifying the original.
...
In this last line, what am I modifying?
C++ Syntax (Toggle Plain Text)
list<obs*> qtr_ser_obs; for (list<obs>::reverse_iterator it=this_series.rbegin() ; it != this_series.rend(); ++it ) { qtr_ser_obs.push_front(&*it); for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin (); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++) { (*qtr_obs_it)->attr_price= computed_attr_price; } }
I think it should be:
C++ Syntax (Toggle Plain Text)
for (list<obs>::reverse_iterator it=this_series.rbegin() ; it != this_series.rend(); ++it ) { list<obs*> qtr_ser_obs; qtr_ser_obs.push_front(&*it); for (list<obs*>::reverse_iterator qtr_obs_it =qtr_ser_obs.rbegin (); qtr_obs_it !=qtr_ser_obs.rend();qtr_obs_it++) { (*qtr_obs_it)->attr_price= computed_attr_price; } }
Last edited by adatapost; Jul 4th, 2009 at 4:51 am.
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Jul 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- List Problem (Python)
- Arrays of linked lists - help! (C)
- Overide Textinput settings (JavaScript / DHTML / AJAX)
- scheme newbie here!!! (Computer Science)
- Bumbling highlights square. (DaniWeb Community Feedback)
- Insertion Sort (C++)
- New programming language: Who can figure it out? (Computer Science)
- Source Code that don't work? (Java)
- tuple or list? (Python)
Other Threads in the C++ Forum
- Previous Thread: Need help on homwork assignment.
- Next Thread: a c++ solution
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






