Jak napisać (lub przeciążyć) iterator dla wektora? (Opis)

0

Witam.

Mam sobie klasę File, która zawiera dwie zmienne, dwa gettery oraz konstruktor. Mam vector z tą klasą. Wygląda to tak:

File.h (tylko i wyłącznie, kodu jest na tyle mało, że IMO nie było sensu tworzyć pliku cpp)

#ifndef FILE_H
#define FILE_H

#include <string>

class File{
    public:
        explicit File(std::string _path, std::string NameInProgram) :
        path(_path), name(NameInProgram) {}
        ~File() = default;
        std::string GetName() {return this->name;}
        std::string GetPath() {return this->path;}
    private:
        std::string path;
        std::string name;
};

#endif

main.cpp

...

int main(int argc, char* argv[]){
    ArgP::ArgumentParser AP(argc, argv);
    std::vector<File> Files;

/** Commands:

file list (1)
file help (1)
file create path (2)
file load path ObjName (3)
file copy path path / obj obj / obj path / path obj (3)
file move path path / obj path
file show path / obj
file obj / path add TEXT
file delete path / obj (2)
file attr add ATTRIBUTE (3)
file attr del ATTRIBUTE (3)
file free obj (2)
file exit (1)

Czy zostawianie takich komentarzy w kodzie jest dobrą praktyką?
Póki co mam to do przypomnienia, parę opcji dojdzie.
**/

  .. tutaj było trochę ifów i else ifów, a aż wreszcie to:
    else if(AP.ArgsCount==2 && AP[1]=="list"){
        std::vector<Files>::iterator i;
        for(i = Files.begin(); i!=Files.end(); ++i){
            std::cout<<counter<<".  "<<i->GetName()<<"   "<<i->GetPath()<<"\n";
        }
    }
}


I chodzi o to, że dostaję błędy. Pomyślałem, że trzeba zaimplementować własny iterator, tak jak implementuje się własne operatory. Dodatkowo słyszałem kiedyś o potrzebie implementacji iteratorów we własnych klasach.

Nie oczekuję gotowego rozwiązania tylko naprowadzenia lub materiału jak mogę to zrobić.

Z góry dziękuję za pomoc.

Pozdrawiam.

2

Files to wektor File, nie potrzebujesz nic samemu implementować.

for(File const& f : Files)
    cout << f.GetName() << ",  " << f.GetPath() << '\n';

1 użytkowników online, w tym zalogowanych: 0, gości: 1