c++ - better way to shrink copy/paste codes -


currently have trunk of code: vector of structs v[mystruct]. size of 3. named these structs mystruct_a, mystruct_b, , mystruct_c. each piece of code same, name suffix.

somemap somemap_a; (auto& pair : mystruct_a.hashmap) {   somestruct somestruct = foo(pair);   somemap_a[somestruct.key] = somestruct.value;   anotherstruct_a[somestruct.namex] = somestruct.bundle; } somevector.push_back[somemap_a]; 

all of 3 a,b,c need above things. question here if using function, don't know how separate , specify names. need several vectors of data based on of 3 mystructs' outputs.

you can macro. example, here's simplified version:

#define process(i, suffix)  some_vect_ ## suffix.push_back(i);  \                             some_set_ ## suffix.insert(i);   int main() {      std::vector<int> some_vect_a, some_vect_b, some_vect_c;     std::set<int> some_set_a, some_set_b, some_set_c;      (int = 0; < 100; i++) {         int val = rand();         process(val, a); // adds `val` `some_vect_a` , `some_set_a`         process(val, b); // adds `val` `some_vect_b` , `some_set_b`         process(val, c); // adds `val` `some_vect_c` , `some_set_c`     } } 

this particular demo code pretty pointless (it produces identical results in 3 sets , 3 vectors), show how manipulation need.


Comments