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});
}
}