c++ - Changing an element within a vector of structures -


i'm iterating through set of elements within vector of structures , want change element in 1 of structures. when write element change value, update isn't retained. here have:

first, in header file:

std::vector<sched::scheduledevent_t> v_schedevents; typedef std::vector<sched::scheduledevent_t>::const_iterator event_iter; 

then later in .cpp module...

for (event_iter = v_schedevents.begin(); != v_schedevents.end(); ++i) {     scheduledevent_t event = *i;      if(event.member == true) {         event.member = false;     } } 

the value of event.member given structure in vector isn't staying false. when returning loop, conditional statement run again.

could have typedef iterator?

two problems here.

1) you're making copy:

scheduledevent_t event = *i; 

event copy of element in vector. modifing event won't affect it.

2) using const_iterator allows reading value, not changing it.

use iterator instead

typedef std::vector<sched::scheduledevent_t>::iterator event_iter; 

and use directly:

if (i->member) { //  == true useless     i->member = false; } 

or for-range loop if have access c++11 or more recent:

for (auto & event : v_schedevents) {     if (event.member) {         event.member = false;     } } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -