如何解决使用 vector::begin() 时出错:不存在从 ... " 到 "int" 的合适转换函数
我正在尝试使用选择方法对整数向量进行排序。
我正在使用 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 举报,一经查实,本站将立刻删除。