//// Created by leoxae on 2019-05-08.//#ifndef OPENCVDEMO_UTILS_H#define OPENCVDEMO_UTILS_H#include #include #include #include #include #include #include "../globals.h"#include "opencv2/opencv.hpp"#define Max(a, b) (a > b ? a : b)#define Min(a, b) (a < b ? a : b)using namespace std ;using namespace cv ;class TempHelper {public: static long getTickCount() { auto now = std::chrono::steady_clock::now(); std::chrono::milliseconds ms(0); ms=std::chrono::duration_cast(now.time_since_epoch()); return ms.count(); } /** * 批量删除vector中的元素 * @tparam T 泛型 * @param src 数据源 * @param rms 待删除的数据 * @return */ template static auto removeAll(std::vector &src, std::vector &rms); /** * 删除vector中单个元素 * @tparam T * @param src * @param a * @return */ template static auto removeObj(std::vector &src, T &a){ typename std::vector::iterator d = remove(src.begin(), src.end(), a); src.erase(d, src.end()); return src; } /** * 直接把对象tostring * @tparam T * @param t * @return */ template static auto toString(const T& t) { std::ostringstream oss; //创建一个格式化输出流 oss< return oss.str(); } /** * 批量添加元素 * @tparam T * @param src * @param need * @return */ template static auto addAll(std::vector &src, std::vector &need){ for (auto it = need.begin(); it != need.end(); ++it) { src.emplace_back(*it); } return src; } /** * 取特定个数元素 * @tparam T * @param src * @param size * @return */ template static auto subVector(std::vector &src, const int &size){ std::vector vt; int idx = 0; for(auto itx = src.begin(); itx != src.end(); ++itx){ if (idx == size) { break; } vt.emplace_back(*itx); idx ++; } return vt; } template static auto split_and_toString(std::vector src, std::string split){ std::string data ; for (auto itx = src.begin(); itx != src.end(); ++itx) { data += (*itx) + split; } return data.substr(0, data.length() - 1); } /**** * 排序 * @tparam A * @tparam B * @param p * @return */ template static std::pair flip_pair(const std::pair &p) { return std::pair(p.second, p.first); } template static std::multimap flip_map(const std::map &src) { std::multimap dst; std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), flip_pair); return dst; } /** * 数组长度 * @tparam T * @param ary * @return */ template static int getAryLen(T &ary) { return sizeof(ary) / sizeof(ary[0]); }};templateauto TempHelper::removeAll(std::vector &src, std::vector &rms) { for (auto it = rms.begin(); it != rms.end(); ++it) { typename std::vector::iterator del = remove(src.begin(), src.end(), (*it)); src.erase(del, src.end()); } return src;}////////////////////////templateint length(T& arr){ //cout << sizeof(arr[0]) << endl; //cout << sizeof(arr) << endl; return sizeof(arr) / sizeof(arr[0]);} /** * 对数组取平均 * * @param ary * @return */ static double avg(double ary[]) { int size = length(ary); double total = 0; for (int i = 0; i < size; i++) { total += ary[i]; } return total / size; }//char getCardName(char content) {// char regex = "([\u4e00-\u9fa5]+)";// Pattern p = Pattern.compile(regex);// Matcher m = p.matcher(content);// while (m.find()) {// return m.group(0);// }// return "未找到卡片名称";//}// std::vector DmatchtoArray() {// int num = int total();// DMatch a[] = new DMatch[num];// if (num == 0) {// return a;// } else {// float buff[] = new float[num * 4];// this.get(0, 0, buff);//// for(int i = 0; i < num; ++i) {// a[i] = new DMatch((int)buff[4 * i + 0], (int)buff[4 * i + 1], (int)buff[4 * i + 2], buff[4 * i + 3]);// }//// return a;// }//}#endif //OPENCVDEMO_UTILS_H Talk is cheap. Show me the code