Home > Back-end >  Sort Algorithm creates error message when changing objects in vector
Sort Algorithm creates error message when changing objects in vector

Time:05-18

#include<vector>;
using namespace std;

int main() {
vector<int>Liste;                       
Liste = { 5,2,3,6,3,4,7 };              
int n = Liste.size();                   
int i, j, k_1, k_2 ;                    
int m_1, m_2 ;                          
for (i; i = 0; i = n - 1) {             
    k_1 = i;                            
    m_1 = Liste[i];                     
    for (j; j = i   1; j = n) {         
        if (Liste[j] < m_1) {           
            k_2 = j;                    
            m_2 = Liste[j];             
        }
    Liste.insert(k_1, m_2);             
    Liste.insert(k_2   1, m_1);         
    Liste.erase(k_1 1);                 
    Liste.erase(k_2   2);               
    }
}
cout << Liste << endl;
return 0;
}

When running the code an error occures in line 19,20,21,22: Keine Instanz von Überladene Funktion "std::vector<_Ty, _Alloc>::insert [mit _Ty=int, _Alloc=std::allocator]" stimmt mit der Argumentliste überein

Since i am new to coding i am not sure why this error occures.

CodePudding user response:

insert and erase methods of std::vector take an iterator as first parameter, not a simple integer, this is roughly the sens of the error returned by your compiler. It does not find any version of insert that takes an int as first parameter.

https://www.cplusplus.com/reference/vector/vector/insert/

Anyway you can use this method to achieve what you want to do:

Liste.insert(Liste.begin()   k_1, m_2);             
Liste.insert(Liste.begin()   (k_2   1), m_1);         
Liste.erase(Liste.begin()   (k_1   1));                 
Liste.erase(Liste.begin()   (k_2   2))  
  • Related