[c++]对vector<T>容器求交集,并集,去重

 

#include "iostream"
#include "vector"
#include "algorithm" //sort函数、交并补函数
#include "iterator" //求交并补使用到的迭代器

using namespace std;

//打印容器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;
}

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

//判断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::Value qr_result;

    //"猴子尾巴", "猴子尾巴", "松鼠尾巴"
    //"松鼠尾巴", "猴子尾巴", "猴子尾巴"
    vector<string> qr_text;
    string s1 = "松鼠尾巴";
    string s2 = "猴子尾巴";
    string s3 = "猴子尾巴 ";
    string text = "  7ter   09, jdhfd iere*- ddw     jjdjjdj     ";
//    trim_z(text);

    s3 = trim(s3);
//    cout << "text==>>" << text << endl;
    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{"猴子尾巴", "猴子尾巴", "猴子耳朵", "松鼠脸"};
    vector<string> vc2{"猴子尾巴", "猴子耳朵", "松鼠尾巴"};
    vector<string> vec;

    cout << "求v1与v2的交集:";
    vec = vectors_intersection(vc1, vc2);
    print_vector(vec);
    cout << endl;
    cout << "求v1与v2的并集:";
    vec = vectors_set_union(vc1, vc2);
    print_vector(vec);
    return 0;
}

 

posted @ 2019-11-04 17:44  Xu_Lin  阅读(1422)  评论(0编辑  收藏  举报