簡體   English   中英

C ++正確使用向量迭代器

[英]C++ using vector iterator correctly

我是C ++的新手,並且有很多醫生。
我用以下代碼添加新醫生:

void DoctorAdmin::setDoctor(std::string lastname, std::string forename,
 Person::Sex sex){

    //Create new doctor
    Doctor* doc = new Doctor(lastname, forename, sex);

    //insert at the end of the vector
    doctors.push_back(doc);
}

然后,我想在控制台上顯示他們的信息:

void DoctorAdmin::showDoctors(){

cout << "Doctors:" << endl;
cout << "Name" << "\t\t\t" << "Forename" << "\t\t\t" << "Sex" << endl;

for (vector<Doctor*>::iterator i = doctors.begin(); i != doctors.end(); i++){

    Doctors* doc = doctors.at(i);
    cout << doc->getName() << "\t\t\t" << doc->getForename() << "\t\t\t" 
         << doc->getSex() << endl;
}

這樣做后,我得到兩個錯誤:

E0304   No instance of overloaded function "std::vector<_Ty, _Alloc>::at [mit _Ty=Doctors *, _Alloc=std::allocator<Doctors *>]" matches the argument list.

// and

C2664   "Doctors *const &std::vector<Doctors *,std::allocator<_Ty>>::at(const unsigned int) const" : cannot convert from Argument "std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>" in "const unsigned int" 

如何正確使用向量迭代器來避免這種情況?

迭代器不像索引,而是像指針。

for (vector<Arzt*>::iterator doc = aerzte.begin(); doc != aerzte.end(); doc++)
{
    cout << (*doc)->getName() << "\t\t\t" << (*doc)->getVorname() << "\t\t\t" 
         << (*doc)->getGeschlecht() << endl;
}

似乎您也對何時需要new事物感到困惑。 大多數時候,您不需要new

vector<Arzt> aerzte;

void ArztAdmin::anlegenArzt(std::string name, std::string vorname, Person::Geschlecht geschlecht){
    // Create new doctor at the end of the vector
    aerzte.emplace_back(name, vorname, geschlecht);   
}

您還可以將引用直接綁定為循環變量

for (Arzt & doc : aerzte)
{
    cout << doc.getName() << "\t\t\t" << doc.getVorname() << "\t\t\t" 
         << doc.getGeschlecht() << endl;
}

at函數需要一個索引,但是從語義上或技術上來說, vector<Arzt*>::iterator都不是索引。 迭代器直接指向元素 ,而索引表示容器的開始位置與容器中允許隨機元素訪問的元素之間的距離。

因為迭代器直接指向元素,所以循環中甚至不需要at函數。 *i產生元素:

Arzt* doc = *i;

從C ++ 11開始,可以使用auto以較短的方式編寫此類簡單循環的代碼:

for (auto i = aerzte.begin(); i != aerzte.end(); i++){

編譯器知道i實際上是什么類型,因為它知道begin()返回什么。

更好的是,使用基於范圍的循環:

for (auto doc : aerzte){
    cout << doc->getName() << "\t\t\t" << doc->getVorname() << "\t\t\t" 
         << doc->getGeschlecht() << endl;
}

而且,當我們使用它時,不必使用動態內存分配。 這不是Java或C#; new在C ++中是危險領域,應避免:

#include <vector>
#include <string>
#include <iostream>

struct Arzt
{
    Arzt(std::string const& name, std::string const& vorname) :
        name(name),
        vorname(vorname)
    {
    }

    std::string name;
    std::string vorname;
    // Geschlecht omitted for brevity's sake
};

int main()
{
    std::vector<Arzt> aerzte;

    Arzt doc1("foo", "bar");
    Arzt doc2("foo", "bar");
    Arzt doc3("foo", "bar");

    aerzte.push_back(doc1);
    aerzte.push_back(doc2);
    aerzte.push_back(doc3);

    for (auto const& arzt : aerzte)
    {
        std::cout << arzt.name << ' ' << arzt.vorname << '\n';
    }
}

由於您不再遍歷指針而是遍歷更大的對象,因此const&應該在for循環中使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM