std::vector遍历元素和std::map 遍历元素


std::vector<std::string> dets = {"apple", "banana", "orange"};

// 使用 dets.size() 获取容器大小
for (size_t i = 0; i < dets.size(); ++i) {
    std::string element = dets[i];
    // 对每个元素执行操作
    std::cout << element << std::endl;
}

// 使用 dets.begin() 获取迭代器
for (auto it = dets.begin(); it != dets.end(); ++it) {
    std::string element = *it;
    // 对每个元素执行操作
    std::cout << element << std::endl;
}



在第一个循环中,我们使用 dets.size() 来确定循环的次数,并使用索引来访问容器中的元素。这种方式适用于随机访问容器(如 std::vector)。

在第二个循环中,我们使用 dets.begin() 获取迭代器,然后使用迭代器来遍历容器中的元素。这种方式适用于所有容器类型。

总之,dets.size() 用于获取容器的大小,dets.begin()
用于获取容器的起始迭代器。你可以根据具体的需求和容器类型选择适合的遍历方式。


std::map<std::string, Detect> detectMap;
bbox = cv::Rect(366, 209, 101, 91); //xywh
detectMap.insert({"cat",{"cat", 0.702, bbox}})

std::vector<std::string> keys;
bool end_flag=true;  
// it->first  表示key   it->second 表示 value
for (auto it = detectMap.begin(); it != detectMap.end(); ++it) {
  if (it->first.find("面") != std::string::npos ){ // 找到了有面这个词, 不用结束了
      end_flag=false;  // 找
  }

  keys.push_back(it->first);  // 将键添加到 keys 向量中
}
// 字典增加元素相同更新,没有增加
void add_det_map(std::map<std::string, Detect> &detectMap, std::string label, float conf, cv::Rect bbox)
{
    auto it = detectMap.find(label);
    if (it !=detectMap.end()){
        if (conf> it->second.prob)
    }
    // 查找是否已存在相同标签的元素
    auto it = detectMap.find(label);
    if (it != detectMap.end()) {   // 如果不等于 end(),表示在 detectMap 中找到了与 label 匹配的元素
        // 如果已存在,比较置信度并更新
        if (conf > it->second.prob) {  //  it->second 表示迭代器指向  表示 key:value  中的value
            it->second.prob = conf;
            it->second.rect = bbox;
        }
    } else {
        // 如果不存在,添加新元素
        detectMap.insert({label, {label, conf, bbox}});
    }
}


// 向量增加元素相同更新,没有增加
void add_det(std::vector<Detect>& dets, std::string label, float conf, cv::Rect bbox)
// 输入引用,定义的变量dets,  如果不存在这个这个label结果,直接添加,如果存在这个结果看置信度,更大就修改为最新的
{
    bool found = false;
    for (auto& det : dets) {
        if (det.name == label) {
            if (conf > det.prob) {
                det.prob = conf;
                det.rect = bbox;
            }
            found = true;
            break;
        }
    }

    if (!found) {
        dets.push_back({label, conf, bbox});
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值