c++推回第一个向量,并按升序对向量进行排序
c++ pushback first vector and sorting vectors by ascending order
我正在尝试对第一个向量进行排序向量l[0]是x1的点,则其长度总是改变
vector<int> v1;
v1.push_back(l[0]);
vector<int>::iterator Iter = v1.begin();
for (Iter = v1.begin(); Iter != v1.end(); Iter++){
sort(v1.begin(), v1.end());
cout << *Iter << endl;
它没有任何错误,但无法按顺序排序。我该怎么解决?
编辑----------------------------------------------------------------------------
void thresh_callback(int, void*)
{
/// Load source image and convert it to gray
src = imread(img_file, 1);
Mat threshold_output;
Mat threshold_output1;
Mat dst;
vector<vector<Point> > contours;
/// Detect edges using Threshold
Canny(src_hist, threshold_output, thresh, thresh * 3, 3, false);
Sharpen(threshold_output, threshold_output1);
cvtColor(threshold_output1, dst, CV_GRAY2BGR);
vector<Vec4i> lines;
HoughLinesP(threshold_output1, lines, 1, CV_PI / 180, thresh1, min_line, max_gap);
int erosion_size = 1;
Mat element = getStructuringElement(MORPH_ERODE,
Size(2 * erosion_size + 1, 2 * erosion_size + 1),
Point(erosion_size, erosion_size));
/// Apply the erosion operation
dilate(dst, dst, element);
imshow("Source", dst);
for (size_t i = 0; i < lines.size(); i++)
{
Vec4i l = lines[i];
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
int dx = l[2] - l[0];
int dy = l[3] - l[1];
double rad = atan2(dx, dy);
double degree = (rad * 180) / M_PI;
if (degree >= 180) degree -= 180;
if (degree < 15 || degree > 165) {
line(src, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 0, 255), 1, 8);
//printf("%lfn", degree);
//printf("%d, %d, %d, %d, %dn", l[0], l[1], l[2], l[3], i);
vector<int> v1;
v1.push_back(l[0]);
vector<int>::iterator Iter = v1.begin();
sort(v1.begin(), v1.end());
for (Iter = v1.begin(); Iter != v1.end(); Iter++){
cout << *Iter << endl;
}
}
imshow("source", src);
}
}
我用houghline找出x1,x2,y1,y2的点并获取每个x1并向上排序
不确定这是你的问题,但。。。在CCD_ 2循环中定义CCD_ 1。
for (size_t i = 0; i < lines.size(); i++)
{
// ...
vector<int> v1;
v1.push_back(l[0]);
vector<int>::iterator Iter = v1.begin();
sort(v1.begin(), v1.end());
for (Iter = v1.begin(); Iter != v1.end(); Iter++){
cout << *Iter << endl;
}
// ...
}
因此,每次迭代,都要创建一个向量,插入一个元素,对向量进行排序(使用一个元素),打印单个值,并销毁向量。
建议:在for
循环之外定义v1
。
---编辑---
正如艾潮所指出的,排序和打印部分可能在for循环之外更好;类似的东西
vector<int> v1;
for (size_t i = 0; i < lines.size(); i++)
{
// ...
v1.push_back(l[0]);
// ...
}
sort(v1.begin(), v1.end());
for ( vector<int>::const_iterator CIter = v1.cbegin(); CIter != v1.cend(); CIter++) {
cout << *CIter << endl;
}
如果您可以编译C++11或C++14,那么最后一个for
可以是简单的
for ( auto const & i : v1 )
cout << i << endl;
抱歉我英语不好。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 如何用单词排列向量中的每个字符升序?
- 如何按升序将偶数从向量 a 转移到向量 b?
- 按升序对特征向量Xf进行排序
- 如何按C 中的向量对升序和下降顺序进行排序
- 对结构、降序或升序的向量进行排序:C++
- c++推回第一个向量,并按升序对向量进行排序