【数据库】C++实现集合简单操作(集合间交、并、差和元素的增删)

简述

此为笔者数据结构作业,代码可能存在纰漏,望谅解。


源代码

#include <bits/stdc++.h>
#define int long long
#define endl "\n"

using namespace std;

const int maxlen = 10010;

class Set{
public:
    Set(){};                            // 构造函数
    Set(int *a1, int *a2);              // 构造函数,重载用来接收数组
    void print();                       // 打印集合元素
    int length()const;                  // 返回集合元素个数
    void add(int a);                    // 新增元素 a
    void del(int a);                    // 删除元素 a
    void clean();                       // 清空集合
    int operator[](int i)const;         // 重组 [], 直接访问 A[i]
    void operator=(const Set &s);       // 重组 = , 传值
    Set operator-(const Set& s1)const;  // 重组 - , 用于差集和交集的运算
    Set operator+(const Set& s1)const;  // 重组 + , 用于并集
    Set intersect(const Set &s);        // 交集
private:
    vector<int> arr;                    // 用于存储集合元素的动态数组
};

Set::Set(int *a1, int *a2){
    int len = a2 - a1;
    for(int i = 0; i <= len; i++) add(*(a1 + i));
}

void Set::print(){
    for(int i = 0; i < arr.size(); i++) cout << arr[i] << " ";
    cout << endl;
}

int Set::length()const{
    return arr.size();
}

void Set::add(int a){
    for(int i = 0; i < arr.size(); i++){
        if(arr[i] == a) return;
    }
    arr.push_back(a);
}

void Set::del(int a){
    for(int i = 0; i < arr.size(); i++){
        if(arr[i] == a){
            arr.erase(arr.begin() + i);
            break;
        }
    }
}

void Set::clean(){
    arr.clear();
}

int Set::operator[](int i)const{
    if(i > arr.size()){
        cout << "索引超过最大值!将返回第0个元素" << endl;
        return arr[0];
    }else{
        return arr[i];
    }
}

// 赋值
void Set::operator=(const Set &s){
    arr.clear();
    for(int i = 0; i < s.length(); i++){
        arr.push_back(s[i]);
    }
}

// 交集
Set Set::intersect(const Set &s){
    Set s1, s2;
    for(int i = 0; i < arr.size(); i++){
        s1.add(arr[i]);
    }
    s2 = s1;
    s1 = s1 - s;
    s2 = s2 - s1;
    return s2;
}

// 并集
Set Set::operator+(const Set& s1)const{
    Set s;
    for(int i = 0; i < arr.size(); i++){
        s.add(arr[i]);
    }
    for(int i = 0; i < s1.length(); i++){
        s.add(s1[i]);
    }
    return s;
}

// 差集
Set Set::operator-(const Set& s1)const{
    Set s;
    for(int i = 0; i < arr.size(); i++){
        s.add(arr[i]);
    }
    for(int i = 0; i < s1.length(); i++){
        s.del(s1[i]);
    }
    return s;
}

signed main(){

    // 为方便操作,以下操作将从数组 a 取数
    int a[10] = {1, 2, 3, 4, 5, 6, 7, 6, 58, 44};

    // 取 a(0, 8)
    Set s(a, a + 8);
    s.print();
    cout << s.length() << endl;

    // 取 a(4, 6)
    Set s1(a + 4, a + 6);
    s1.print();
    cout << s1.length() << endl;

    // 测试差集 (s - s1), 注意为检查运算准确性, s2 在运算前加了元素 128
    Set s2;
    s2 = s - s1;
    s2.add(128);
    s2.print();
    cout << s2.length() << endl;

    // 测试交集 (s3 交 s2)
    Set s3;
    s3 = s.intersect(s2);
    s3.print();
    cout << s3.length() << endl;

    // 取 a(6, 9)
    Set s4(a + 6, a + 9);
    s4.print();
    cout << s4.length() << endl;

    // 测试并集 (s1 + s4)
    Set s5;
    s5 = s1 + s4;
    s5.print();
    cout << s5.length() << endl;
    return 0;
}

运行结果:

1 2 3 4 5 6 7 58
8
5 6 7
3
1 2 3 4 58 128
6
1 2 3 4 58
5
7 6 58 44
4
5 6 7 58 44
5
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB C++ driver支持同步和异步操作。下面分别给出同步和异步增删改查接口的C++代码示例: 同步操作: ```c++ #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/options/find.hpp> #include <mongocxx/stdx.hpp> #include <bsoncxx/json.hpp> mongocxx::instance instance{}; // 实例化驱动 mongocxx::client client{mongocxx::uri{}}; // 连接MongoDB数据库 mongocxx::database db = client["test"]; // 选择数据库 mongocxx::collection coll = db["test_collection"]; // 选择集合 bsoncxx::builder::stream::document document{}; document << "name" << "John Doe" << "age" << 42; coll.insert_one(document.view()); // 插入一条文档 auto builder = bsoncxx::builder::stream::document{}; auto query = builder << "name" << "John Doe" << bsoncxx::builder::stream::finalize; auto update = builder << "$set" << bsoncxx::builder::stream::open_document << "age" << 43 << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize; coll.update_one(query.view(), update.view()); // 更新一条文档 auto delete_result = coll.delete_one(query.view()); // 删除一条文档 ``` 异步操作: ```c++ #include <mongocxx/client.hpp> #include <mongocxx/instance.hpp> #include <mongocxx/pool.hpp> #include <mongocxx/options/find.hpp> #include <mongocxx/stdx.hpp> #include <bsoncxx/json.hpp> mongocxx::instance instance{}; // 实例化驱动 mongocxx::pool pool{mongocxx::uri{}}; // 连接池 mongocxx::database db = pool.acquire().database("test"); // 从连接池中获取连接并选择数据库 mongocxx::collection coll = db["test_collection"]; // 选择集合 auto insert_result = coll.insert_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << "age" << 42 << bsoncxx::builder::stream::finalize, mongocxx::options::insert{}); // 插入一条文档 auto update_result = coll.update_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << bsoncxx::builder::stream::finalize, bsoncxx::builder::stream::document{} << "$set" << bsoncxx::builder::stream::open_document << "age" << 43 << bsoncxx::builder::stream::close_document << bsoncxx::builder::stream::finalize, mongocxx::options::update{}); // 更新一条文档 auto delete_result = coll.delete_one( bsoncxx::builder::stream::document{} << "name" << "John Doe" << bsoncxx::builder::stream::finalize, mongocxx::options::delete{}); // 删除一条文档 ``` 需要注意的是,异步操作需要使用连接池,而同步操作不需要。此外,异步操作需要使用回调函数处理结果,但这里没有给出回调函数的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值