C++ c++;:“原因”;没有重载函数的实例“;

C++ c++;:“原因”;没有重载函数的实例“;,c++,error-handling,C++,Error Handling,我正在尝试使用“插入排序”方法对数组向量进行排序。 但我面临的错误显示在底部,涉及这两行: vec.insert(j , vec[i]); vec.erase(i+1); 完整代码: #include<iostream> #include<vector> /* I could do it recursively */ void ins_sort(std::vector<int> vec ); int main(){ std::vector<i

我正在尝试使用“插入排序”方法对数组向量进行排序。 但我面临的错误显示在底部,涉及这两行:

vec.insert(j , vec[i]);

vec.erase(i+1);
完整代码:


#include<iostream>
#include<vector>

/*
I could do it recursively
*/
void ins_sort(std::vector<int> vec );

int main(){

std::vector<int> vec = {2 , 8 , 5 , 3 , 9 , 4};

ins_sort(vec);

}

void ins_sort(std::vector<int> vec ){

    for (int i = 1 ; i < vec.size() ; i++){

        if(vec[i] < vec[i-1]){ //look for a index which value is lower than vec[i], then move vec i to the index after that

            for (int j = 0 ; j <vec.size() ; j++){

                if (vec[i] < vec[j] ){

                    vec.insert(j , vec[i]);//inserting vec[i] into the right position

                    vec.erase(i+1); //erasing vec[i] which now is vec[i+1] after insertion

                    break; //ending the inner loop after finding the first greater value
                }
            }

        }
    }
}

#包括
#包括
/*
我可以递归地做
*/
无效ins_排序(标准::向量向量向量);
int main(){
向量向量向量={2,8,5,3,9,4};
ins_排序(vec);
}
无效INSU排序(标准::向量向量){
对于(int i=1;i对于(int j=0;j一个快速而肮脏的解决方法:

vec.insert(vec.begin() + j , vec[i]);//inserting vec[i] into the right position
vec.erase(vec.begin() + i + 1); //erasing vec[i] which now is vec[i+1] after insertion

另外,请从您应该了解的
ins\u sort
函数返回排序向量iterators@MatG请解释更多
int-ins\u排序(std::vector-vec){
请注意,您正在按值传递
vec
,这意味着调用函数将看不到此函数内的任何更改。insert将迭代器作为第一个参数,而不是索引:此链接底部的示例代码可以帮助您解决此问题。为什么需要添加vec.begin()到索引的开始?我知道它是指向向量容器的第一个元素的迭代器。但是为什么“I”和“j”不是足够了吗?这是因为向量是动态的吗?更新:这个链接有帮助:嗨,@Max。最好避免使用
insert
delete
方法,因为它们可能导致向量的重新分配。也许最好是顺序复制元素以保持向量的完整性。
vec.insert(vec.begin() + j , vec[i]);//inserting vec[i] into the right position
vec.erase(vec.begin() + i + 1); //erasing vec[i] which now is vec[i+1] after insertion