Changeset 257 for cpp/common/stl-util.h
- Timestamp:
- 12/03/14 18:52:05 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/stl-util.h
r246 r257 11 11 template<typename T, std::size_t N> void push_back(vector<T>& v, T(&d)[N]) 12 12 { 13 for(unsigned int i=0;i<N;i++)14 v.push_back(d[i]);13 for (unsigned int i = 0; i < N; i++) 14 v.push_back(d[i]); 15 15 } 16 16 17 17 template<typename T> void erase(vector<T>& v, const T& e) 18 18 { 19 typename vector<T>::iterator it=std::find(v.begin(),v.end(),e);20 if (it!=v.end())21 v.erase(it);19 typename vector<T>::iterator it = std::find(v.begin(), v.end(), e); 20 if (it != v.end()) 21 v.erase(it); 22 22 } 23 23 24 24 template<typename T> void deleteVectorElements(vector<T*>& v) 25 25 { 26 for(typename vector<T*>::iterator it=v.begin();it!=v.end();it++)27 delete *it;28 v.clear();26 for (typename vector<T*>::iterator it = v.begin(); it != v.end(); it++) 27 delete *it; 28 v.clear(); 29 29 } 30 30 31 31 template<typename T> int findIndex(vector<T>& v, const T& e) 32 32 { 33 typename vector<T>::iterator it=find(v.begin(),v.end(),e);34 if (it!=v.end())35 return &*it-&v.front();36 return -1;33 typename vector<T>::iterator it = find(v.begin(), v.end(), e); 34 if (it != v.end()) 35 return &*it - &v.front(); 36 return -1; 37 37 } 38 39 40 char* strmove(char *a, char *b); //strcpy that works well for overlapping strings ("Source and destination overlap") 38 41 39 42 string ssprintf(const char* format, ...); … … 45 48 46 49 47 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file =true);48 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file =true);49 bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail=true);50 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail =true);50 bool readCompleteFile(const char* filename, vector<char>& data, bool warn_on_missing_file = true); 51 bool readCompleteFile(const char* filename, string& out, bool warn_on_missing_file = true); 52 bool writeCompleteFile(const char* filename, const std::string& text, bool warn_on_fail = true); 53 bool writeCompleteFile(const char* filename, vector<char>& data, bool warn_on_fail = true); 51 54 52 55 template<class T> class DeletingVector // deletes the elements (pointers) in destructor 53 56 { 54 55 std::vector<T*> vector;56 ~DeletingVector()57 public: 58 std::vector<T*> vector; 59 ~DeletingVector() 57 60 { 58 for(int i=vector.size()-1;i>=0;i--)59 delete vector[i];61 for (int i = vector.size() - 1; i >= 0; i--) 62 delete vector[i]; 60 63 } 61 T* operator[](int i) {return vector[i];}62 int size() {return vector.size();}63 void push_back(T* x) {vector.push_back(x);}64 T* operator[](int i) { return vector[i]; } 65 int size() { return vector.size(); } 66 void push_back(T* x) { vector.push_back(x); } 64 67 }; 65 68
Note: See TracChangeset
for help on using the changeset viewer.