vector<int> 排序-求极值-求和-查找

本文详细介绍了如何使用C++ STL中的vector进行升序、降序排序,查找最大值、最小值,求和及查找特定元素的方法。通过自定义比较函数实现灵活排序,利用<algorithm>和<numeric>头文件中的函数简化操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vector<int> data = { 1, 3, 2, 4 };

1、升序、降序

sort(data.begin(), data.end()); //升

sort(v.begin(), v.end(), less<int>());    //升
sort(v.begin(), v.end(), greater<int>()); //降

bool upsort(const int &a, const int &b)
{
    return a < b;
}
sort(data.begin(), data.end(), upsort);   //升序
bool downsort(const int &a, const int &b)
{
    return a > b;
}
sort(data.begin(), data.end(), downsort); //降序

2、最大值、最小值

auto data_max = data.max_element(data.begin(), data.end());    //返回的是迭代器

auto data_min = data.min_element(data.begin(), data.end());

3、求和   #include <numeric>

int sum = accumulate(data.begin(), data.end(), 0);
int sum = accumulate(data.begin(), data.end(), (int)0);

double sum = accumulate(data.begin(), data.end(), 0.0);           //应避免精度损失
double sum = accumulate(data.begin(), data.end(), (double)0);

4、查找

auto it = find(data.begin(), data.end(), val);   //返回迭代器

 

请告诉我最后的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
这段代码是如何获取到中心线的所在邻域的://先进行高斯滤波 sigma需要大于等于激光线半宽除以sqrt(3) //详情见《An Unbiased Detector of Curvilinear Structures》 cv::GaussianBlur(src_img_float, src_img_float, cv::Size(0, 0), laser_half_width/sqrt(3)); //用高斯一阶导进行滤波 //每行中正响应最大点和负响应最大点构成激光线的边缘 //滤波核的计算方法见《An Unbiased Detector of Curvilinear Structures》 cv::Mat kernel(1, 2 * filter_half_size + 1, CV_32F); float sigma = filter_half_size / 1.0; for (int i = 0; i < kernel.cols; i++) { int x = i - filter_half_size; kernel.at<float>(0, i) = exp(-(pow(x + 0.5, 2) / (2 * sigma * sigma))) / (sqrt(2 * CV_PI) * sigma) - exp(-(pow(x - 0.5, 2) / (2 * sigma * sigma))) / (sqrt(2 * CV_PI) * sigma); } cv::Mat filtered; cv::filter2D(src_img_float, filtered, CV_32F, kernel); for (int h = 0; h < resolution_height; h++) { std::vector<float> data(&filtered.at<float>(h, 0), &filtered.at<float>(h, 0) + resolution_width); cv::Point pMax, pMin; cv::minMaxLoc(data, NULL, NULL, &pMin, &pMax); int width = fabs(pMax.x - pMin.x) + 1; //激光线太细 数据量不够 不计算 if (width < 3) continue; int start = pMax.x < pMin.x ? pMax.x : pMin.x; int end = pMax.x > pMin.x ? pMax.x : pMin.x; //计算[start,end]区间内平均灰度 float avg = cv::sum(std::vector<uchar>(&src_img_float.at<uchar>(h, start), &src_img_float.at<uchar>(h, start) + width))[0] / (float)width; //平均灰度太低 不计算 if (avg < gray_threshold) continue; //对[start,end]内的点进行高斯拟合,方法见 //https://blog.youkuaiyun.com/qq_33487700/article/details/121998149 cv::Mat A(width, 3, CV_64F); cv::Mat b(width, 1, CV_64F); for (int r = 0; r < A.rows; r++) { A.at<double>(r, 0) = 1; int x = start + r; int y = src_img_float.at<uchar>(h, x); A.at<double>(r, 1) = x; A.at<double>(r, 2) = x * x; b.at<double>(r, 0) = log(y); } //cv::Mat x; //cv::solve(A, b, x, cv::DecompTypes::DECOMP_SVD); //和fpga一致 cv::Mat x = (A.t() * A).inv() * A.t() * b; centre_points_internal[h].x = -x.at<double>(1, 0) / (2 * x.at<double>(2, 0)); gray_values_internal[h] = src_img_float.at<uchar>(h, centre_points_internal[h].x); } break; }
最新发布
07-23
``` #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <climits> #include<queue> #include<cstring> #include<tuple> #include<string> #include<iomanip> #define N 105 #define M 305 using namespace std; int head[N], to[N * N], nex[N * N], w[N * N], ti[N * N],cnt; int f[N][N * N],maxn, vis[N][N * N],delt[N][N*N]; void add(int a,int b,int c,int d) { to[++cnt] = b, w[cnt] = c, ti[cnt] = d, nex[cnt] = head[a], head[a] = cnt; } void update(int x, int y, int w) { ++y; for (; y <= 100 * N; y += (y & -y))delt[x][y] = min(delt[x][y], w); } int quary(int x, int y) { y++; int nm = 10000000; for (; y <= 100 * N; y -= (y & -y))nm = min(delt[x][y], nm); return nm; } void SPFA(int s) { fill(&f[0][0], &f[0][0] + N * N * N,INT_MAX); fill(&delt[0][0], &delt[0][0] + N * N * N, INT_MAX); queue<tuple<int,int,int>>line; line.emplace(s,0,0); f[s][0] = 0; update(1, 0, 0); while (!line.empty()) { auto [v,m,t] = line.front(); line.pop(); vis[v][m] = 0; for (int i = head[v]; i; i = nex[i]) { int u = to[i]; int mon = m + w[i]; if (mon > maxn+1)continue; if (quary(u,mon) > f[v][m] + ti[i]) { f[u][mon] = f[v][m] + ti[i]; update(u, mon, f[u][mon]); if (!vis[u][mon]) { vis[u][mon] = 1; line.emplace(u, mon, f[u][mon]); } } } } } int main() { int n, m, s, e,dmax=INT_MAX,ans=0; cin >> n >> m >> s >> e; for (int i = 0; i < m; i++) { int a, b, c, d; cin >> a >> b >> c >> d; add(a, b, c, d); add(b, a, c, d); } maxn = (n-1) * 100; SPFA(s); for (int i = 0; i<maxn+6; i++) { if (f[e][i]==INT_MAX)continue; if (f[e][i] < dmax) { dmax = f[e][i]; ans++; } } cout << ans; }```哪里洋浦问题
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值