dimanche 8 mai 2022

error: no matching member function for call to 'erase'?

I was working with vector and trying to work with erase function from vector library.

Here is my code

#include <iostream>
#include <vector>

int leftover(std::vector<int> aList)
{
    // size = 1 will be termination point
    if (aList.size() == 1)
    {
        return aList.at(0);
    }
    else    
    {
        aList.push_back(aList.at(0));
        aList.erase(aList.at(0));
        aList.erase(aList.at(1));
        return leftover(aList);
    }
}


int main()
{
    int x;

    std::cout << "Enter a number: ";
    std::cin >> x;
    
    std::vector<int> intVector;
    for (int i = 0 ; i < x; i++) {
        intVector.push_back(i+1);
    }

    for (const int& i : intVector)
    {
        std::cout << i << " ";
    }

    std::cout << "\n";

    //int leftwith = leftover(intVector);

    //std::cout << leftwith << "\n";

    return 0;
}

Anything with remove the element at particular position would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire