问题描述
我正在尝试使用选择方法对整数向量进行排序。
我正在使用 vector::begin()
迭代向量的元素,但是,迭代中出现了一些错误,导致如下错误:
这里有什么问题?
#include<iostream>
#include<vector>
void selection_sort(std::vector<int> vec);
int main(){
std::vector<int> vec = {2,8,5,3,9,4,1};
selection_sort(vec);
}
void selection_sort(std::vector<int> vec){
for(int j = vec.begin() ; j < vec.size() ; j++){ //searching from the first vec index to the last index of vec
int min = j; //current min always is assigned to the first unsorted element
for (int i = vec.begin() + j ; i < vec.size() ; i++){ //starting searching in the ubsorted numbers
if (vec[i] < vec[min]){ //if one of unsorted elements is smaller than the first unsorted element,that will be the min
min = i; //min between unsorted numbers
}
std::swap(vec[min],vec[j]); //swaping th efinal min of unsorted elements with the firstr unsorted element
}
}
for (int i = vec.begin() ; i < vec.size() ; i++){
std::cout<<vec[i];
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)