Structure of vectors C++

2019-06-09 09:20发布

问题:

is there a way to clear a structure of vectors at a time using a single statement ? i.e. struct AStruct { vector StringList; vector DistanceList; }A;

i want both the vectors using a single statement.

回答1:

Sure:

AStruct a;
// stuff
a = AStruct();  // clear it

However, I would probably give myself a function:

struct AStruct { 
   vector <string> StringList; 
   vector <string> DistanceList; }
   void clear() {
       StringList.clear();
       DistanceList.clear();
   }
};

You can then say:

AStruct a;
// stuff
a.clear();  // clear it

which is perhaps easier to understand.



标签: vector struct