简体   繁体   中英

Vector Iterator and insert function using typedef, C++

I am trying to implement heap class in c++. I am facing a problem in my insert member function. When I am trying to create an iterator I tried two methods both gave me errors for some reason. Here is my 1st method:

#ifndef Heap_hpp
#define Heap_hpp

#include <stdio.h>
#include <vector>
#include <iterator>
#include <string>
#include <iostream>
typedef int elementType;


    class Heap{
    private:
        std::vector<elementType> myVecrtor;
        int mySize = 1; //The minimum size is 1 since the first element is a dummy.
        
    public:
        Heap();
        void insert(elementType const item);
    };

    //implementation of first method
    void Heap::insert(elementType const item){
        typename std::vector<elementType> :: iterator it = mySize;/* I got the error 
                                                                   in this line*/
        myVecrtor.insert(it, item);//insert after first element(dummy)
        mySize++;
    }

The error:

 No viable conversion from 'int' to 'typename std::vector<int>::iterator' (aka '__wrap_iter<int *>') Candidate constructor not viable: no known conversion from 'int' to 'std::__wrap_iter<int *>::iterator_type' (aka 'int *') for 1st argument; take the address of the argument with &

If I follow the suggestion (adding '&' before item) it gives me this next error in the same line:

 Calling a private constructor of class 'std::__wrap_iter<int *>'

Here is my 2nd method:

void Heap::insert(elementType const item){
    auto it = mySize;//iterator
    myVecrtor.insert(it, item)//here I got the error
    mySize++;
}

The error:

 No matching member function for call to 'insert'

At first I thought it was because the item was not a const but it didn't work either.

In my main I simply created an instance of the class and called this method. I have no idea what is the problem in both methods. Any help would be appreciated.

For starters the keyword typename in this declaration

typename std::vector<elementType> :: iterator it = mySize;

is redundant. Remove it.

std::vector<elementType> :: iterator it = mySize;

There is no constructor that converts an integer to an iterator. So the above declaration does not make a sense.

It seems you are trying to append a new value to the end of the vector. If so then just write

    myVecrtor.push_back(item);
    mySize++;

If you are going to insert a new value in a specified position then write

#include <iterator>

//...

myVecrtor.insert( std::next( std::begin( myVecrtor ), mySize ), item);
mySize++;

Pay attention to that this declaration

std::vector<elementType> myVecrtor;

declares an empty vector without any dummy element. So to add a new element to the vector using an iterator you have to write

myVecrtor.insert( std::begin( myVecrtor ), item);

Iterators are not int s. mySize is an int .

auto it = mySize;//iterator

the comment says this line is nonsense. it is not an iterator.

    typename std::vector<elementType> :: iterator it = mySize;

this line tries to convert an int to an iterator. The compiler complains because you can't do that.

I am not sure what your code is trying to do with mySize so I cannot correct it.

Iterators can be produced by calling myVecrtor.begin() . Such iterators will not be valid after any potentially resizing operation on myVecrtor , such as insert .

A count of the number of elements in a vector is pointless. myVecrtor.size() will maintain that for you. Inserting at the end just requires a call to push_back(int) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM