[C ++] on the intersection of vector <T> of the container, and set, deduplication

 

#include " the iostream " 
#include " Vector " 
#include " algorithm "  // Sort functions, and cross-complementary function 
#include " Iterator "  // intersection and used to complement the iterator 

the using  namespace STD; 

// Prints the container Vector 
void print_vector (Vector < String > V) {
     IF (v.size ()> 0 ) { 
        COUT << " { " ;
         for ( int I = 0 ; I <int(v.size()); i++) {
            cout << v[i] << ",";
        }
        cout << "\b}";
    } else {
        cout << "{}";
    }
}

//容器vector中元素的去重
vector<string> unique_element_in_vector(vector<string> v) {
    vector<string>::iterator vector_iterator;
    sort(v.begin(), v.end());
    vector_iterator = unique(v.begin(), v.end());
    if (vector_iterator != v.end()) {
        v.erase(vector_iterator, v.end());
    }
    return v;
}

//两个vector求交集
vector<string> vectors_intersection(vector<string> v1, vector<string> v2) {
    vector<string> v;
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v));//求交集
    return v;
}

//And two current demand vector 
vector < String > vectors_set_union (vector < String > V1, vector < String > V2) { 
    vector < String > V; 
    Sort (v1.begin (), v1.end ()); 
    Sort (V2. the begin (), v2.end ()); 
    set_union (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (V)); // intersection of 
    return V; 
} 

// determines whether there is an element of the vector 
BOOL is_element_in_vector (vector < String > V, String element) { 
    vector < String > :: Iterator IT; 
    IT= find(v.begin(), v.end(), element);
    if (it != v.end()) {
        return true;
    } else {
        return false;
    }
}


int trim_z(std::string &s) {
    if (s.empty()) {
        return 0;
    }
    s.erase(0, s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);

    return 1;
}

String TRIM ( String & S) {
     String STR;
     IF (! {s.empty ()) 
        s.erase ( 0 , s.find_first_not_of ( "  " )); 
        s.erase (s.find_last_not_of ( "  " ) + . 1 ) ; 
    } 
    STR = S;
     return STR; 
} 

void Test () { 
    Json the Value :: qr_result; 

    // "monkey tail", "monkey tail", "squirrel tail"
     // "squirrel tail", "monkey tail", " monkey tail " 
    the Vector < String > qr_text;
    string= S1 " squirrel tail " ;
     String S2 = " monkey tail " ;
     String S3 = " monkey tail " ;
     String text = "   7ter 09, jdhfd IERE * - DDW jjdjjdj      " ;
 //     trim_z (text); 

    S3 = TRIM (S3 );
 //     COUT << "text == >>" << endl << text; 
    COUT << " S3 == >> " << S3 << endl; 

    qr_text.emplace_back (S1); 
    qr_text.emplace_back (S2 );
    qr_text.emplace_back(s3);

    const int s = qr_text.size();
    int tempTimes = 0;
    Json::Value items;
    string name;
    for (int j = 0; j < s; ++j) {
        string item = qr_text[j];
        if (item != "二维码识别失败") {
            int times = count(qr_text.begin(), qr_text.end(), item);
            cout << "times==>" << times << endl;
            if (times > tempTimes) {
                tempTimes = times;
                name = item;
                tempTimes++;
            }
        }
        items.append(item);
    }
    //封装返回的json信息
    qr_result["name"] = name;
    qr_result["nname"] = items;
    qr_result["score"] = items.size();
    qr_result["function"] = "QRcoderecognition" ; 
    COUT << " JSON == >> " << qr_result.toStyledString () << endl; 
} 


int main () {
 //     jiaoji ();
 //     Test (); 
    Vector < String > VC1 { " monkey tail " , " monkey tail " , " monkey ear " , " squirrel face " }; 
    Vector < String > {VC2 " monkey tail " , " monkey ear ", " Squirrel tail "}; 
    Vector < String > VEC; 

    COUT << " seek v1 and v2 intersection: " ; 
    VEC = vectors_intersection (VC1, VC2); 
    print_vector (VEC); 
    COUT << endl; 
    COUT << " seek and v1 and v2 set: " ; 
    VEC = vectors_set_union (VC1, VC2); 
    print_vector (VEC); 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/lx17746071609/p/11766558.html