实现对std::vector进行片段截取操作

本文通过C++代码示例,展示了如何使用std::vector容器存储整型数组,并演示了如何截取向量的前四和后四个元素。通过迭代器实现截取操作,代码清晰易懂。

废话少说,直接上代码:

#include <iostream>
#include <vector>
int main() {
    std::vector<int> vector{1,2,3,4,5,6,7,8,9};
    std::cout << "vectoor: ";
    for(auto el : vector) {
        std::cout << el << " ";
    }
    //截取前4个数
    std::vector<int>::const_iterator first1 = vector.begin();
    std::vector<int>::const_iterator last1  = vector.begin() + 4;
    std::vector<int> cut1_vector(first1, last1);
    std::cout << "\ncut1_vector: ";
    for(auto el : cut1_vector) {
        std::cout << el << " ";
    }
    //截取后4个数
    std::vector<int>::const_iterator first2 = vector.end() - 4;
    std::vector<int>::const_iterator last2  = vector.end();
    std::vector<int> cut2_vector(first2, last2);
    std::cout << "\ncut2_vector: ";
    for(auto el : cut2_vector) {
        std::cout << el << " ";
    }
    std::cout << "\n";
}

输出结果为:

vectoor: 1 2 3 4 5 6 7 8 9 
cut1_vector: 1 2 3 4 
cut2_vector: 6 7 8 9 
请告诉我最后的if(iter_max.size() == 1)时啊什么逻辑: void finish_and_compute(std::map<std::string, struct PTQTensorParamenter>& ptq_paramenter, std::vector<std::string> need_quantize_names, std::map<std::string, std::vector<std::vector<float>>>& distribution_map, std::map<std::string, std::vector<bool>> valid_channel_map, std::map<std::string, bool> use_max_map, std::map<std::string, std::vector<float>> intervals_map, int m_bin_number, bool m_merge_channel, std::map<std::string, int>& kl_traget_bin_nums_map) { for (int i = 0; i < int(need_quantize_names.size()); i++) { std::string feature_name = need_quantize_names[i]; std::vector<bool> m_valid_channel = valid_channel_map[feature_name]; bool m_use_max = use_max_map[feature_name]; std::vector<std::vector<float>> m_distribution = distribution_map[feature_name]; std::vector<float> m_intervals = intervals_map[feature_name]; std::vector<float> scale_value(m_distribution.size(), 0.0f); int kl_traget_bin_nums = kl_traget_bin_nums_map[feature_name]; if (m_merge_channel) { if (!m_valid_channel[0]) { ptq_paramenter[feature_name].scale_ = scale_value; } float sum = 0.0f; std::vector<float> distribution = m_distribution[0]; for (int j = 0; j < int(distribution.size()); ++j) { sum += distribution[j]; } if (sum == 0) { VLOGEC(MagikModuleType::MAGIK_PTQ, ErrorCodeType::MAGIK_CODE_CHECK_DIV_BY_ZERO) << "sum is 0"; } for (int j = 0; j < int(distribution.size()); ++j) { distribution[j] /= sum; } int threshold = compute_threshold(distribution, m_bin_number, m_use_max, kl_traget_bin_nums); float scale = 0.0f; if (kl_traget_bin_nums - 1 == 0) { VLOGEC(MagikModuleType::MAGIK_PTQ, ErrorCodeType::MAGIK_CODE_CHECK_DIV_BY_ZERO) << "kl_traget_bin_nums - 1 is 0"; } if (m_intervals[0] != 0.0f) { scale = ((float)threshold + 0.5) / m_intervals[0] / (kl_traget_bin_nums - 1); } else { // when feature map max_value == 0 scale = 0.0f; } auto iter_max = ptq_paramenter[feature_name].iter_max; for (int i = 0; i < int(iter_max.size()); ++i) { iter_max[i] = std::max(iter_max[i], fabsf(ptq_paramenter[feature_name].iter_min[i])); } std::sort(iter_max.begin(), iter_max.end(), std::less<float>()); float sum_max = 0; int valid_num = int(iter_max.size() * 0.7); if (iter_max.size() <= 0) { VLOGEC(MagikModuleType::MAGIK_PTQ, ErrorCodeType::MAGIK_CODE_NOT_MATCHED_DATA) << "Need calibration images num > 0"; } if (iter_max.size() == 1) { valid_num = 1; VLOGW << "better calibration images num > 4"; }
03-20
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值