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.

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,

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().

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:

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;
                }
   }

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.

This article has been dead for over six months. Start a new discussion instead.