简体   繁体   中英

Fail to use iterator to read a vector in the vector of class object

I have created a vector inside vector of class object. The data hierarchy is shown as follow:

ServerList {
       ConsolePerformance: {
               Name:Computer1
               DelayTime:{12, 14, 16, 19}
               ProcessTime:{100, 210, 20, 40}
       },
       {       
               Name:Computer2
               ...
       }
       ...
}

The class object ServerList contains a vector of ConsolePerformance class object.

Each ConsolePerformance class object which have a vector of DelayTime and a vector of ProcessTime.

There are four processes running on a computer, so the DelayTime and ProcessTime would contain four values.

I am going to find out the max/min value inside the vector.

I can read one value inside the vector correctly by the getting method of my class object.

However, I fail to read the value with the use of iterator. Is there any declaration I have set wrongly?

Here is my code:

ServerList.h

class ServerList {
    private:
        vector<ConsolePerformance*> performanceStat;
    public:
        ServerList();
        vector<ConsolePerformance*> & getPerformanceStat() {return performanceStat;}
};

ServerList::ServerList() {
    ConsolePerformance *cp1 = new ConsolePerformance();
    cp1->setName("Computer1");
    performanceStat.push_back(cp1);

    ConsolePerformance *cp2 = new ConsolePerformance();
    cp2->setName("Computer2");
    performanceStat.push_back(cp2);
}

ConsolePerformance.h

class ConsolePerformance {
    private:
        string Name;
        vector<float> delayTime, processTime;
    public:
        string getName() const {return Name;}
        vector<float> getDelayTime() const {return delayTime;}
        vector<float> getProcessTime() const {return processTime;}

        void setName(string _s) {Name = _s;}
        void addDelayTime(float _i) {delayTime.push_back(_i);}
        void addProcessTime(float _i) {processTime.push_back(_i);}
}

Main.cpp

int main()
{
    ServerList LE = ServerList();

    float performanceTime = 12; //repeat for 14, 16, 19
    LE.getPerformanceStat()[0]->addDelayTime(performanceTime);
    performanceTime = 14; 
    LE.getPerformanceStat()[0]->addDelayTime(performanceTime);
    performanceTime = 16; 
    LE.getPerformanceStat()[0]->addDelayTime(performanceTime);
    performanceTime = 19; 
    float performanceTime = 100; //repeat for 210, 20, 40
    LE.getPerformanceStat()[0]->addProcessTime(performanceTime);
    performanceTime = 210; 
    LE.getPerformanceStat()[0]->addProcessTime(performanceTime);
    performanceTime = 20; 
    LE.getPerformanceStat()[0]->addProcessTime(performanceTime);
    performanceTime = 40; 
    LE.getPerformanceStat()[0]->addProcessTime(performanceTime);
        //...

        cout << LE.getPerformanceStat()[0]->getDelayTime()[0]; //show 12 correctly

        //Iterator here return something like 4.80697e-039
        std::vector<float>::iterator itr = LE.getPerformanceStat()[0]->getDelayTime().begin();
        std::vector<float>::iterator itr2 = LE.getPerformanceStat()[0]->getDelayTime().end();
        vector<float>::iterator ptr;
        for(ptr=itr ; ptr!=itr2 ; ptr++)
                cout << *ptr << endl;

        //The following would prompt out Access violation reading location
        //auto result = std::minmax_element (LE.getPerformanceStat()[i]->getDelayTime().begin(),LE.getPerformanceStat()[i]->getDelayTime().end());
}

Problem is with return type of getDelayTime . You return a copy of vector, so calls begin / end refer to two different objects, it leads to UB.

Return vectors by reference:

    const vector<float>& getDelayTime() const {return delayTime;}
    const vector<float>& getProcessTime() const {return processTime;}

These methods are const-qualified, so you need to return reference to const vector.

If you don't want to change interface of your class, create one copy of vector and then on it call begin / end :

std::vector<float> v = LE.getPerformanceStat()[0]->getDelayTime();
auto itr = v.begin();
auto itr2 = v.end();

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