正在加载图片...
Design a generic algorithm(cont.) J First solution, using pointer to function vector<int> filter ver l(const vector<int> &vec, int threshold, bool( pred)(int, int) vector<int> nvec for (int iX=0; iX< vec size(); iX++) The for loop is related if( pred(vec[IXI threshold) nvec.push back(vecIIXD to sequential addressing return nvec bool less than(int vl, int v2 f return v1 <v2? true: false; j bool greater than(int vI, int v2) return v1>v2? true false vector<int> It 10=filter verl(ivec, 10, less than) //callingDesign a generic algorithm (cont.) ◼ First solution: using pointer to function vector<int> filter_ver1(const vector<int> &vec, int threshold, bool (*pred)(int, int)) { vector<int> nvec; for (int iX = 0; iX < vec.size(); iX++) if ( pred(vec[iX], threshold) ) nvec.push_back(vec[iX]); return nvec; } bool less_than(int v1, int v2) { return v1 < v2 ? true : false; } bool greater_than(int v1, int v2) { return v1 > v2 ? true : false; } vector<int> lt_10 = filter_ver1(ivec, 10, less_than); //calling  The for loop is related to sequential addressing
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有