samedi 20 novembre 2021

Return true statement is not exiting function instead its gong to first line of function | C++

I am working on remove-functionality to delete a contact from multimap wherein the function I am passing 'aKey' parameter as the key of the multimap, before erasing multimap I am removing its associated data, in my case, a vector. I am clearing related data in the vector and using new vector size I am reinserting values in multimap then I passed return true statement, however, I am facing an issue where bool returns true is not exiting function instead of flow of control is going back to the first line of function. This is my code below for the function, I am not able to figure out why the return true statement not working, I tried writing outside if-statement as well.

    bool ContactList::remove( const string& aKey ){

    //if result found more than 1, dont delete
    if(listOfContactsMap.count(aKey)>1){
        return false;
    } else {

        //get iterator at Contact list
        //pair<multimap<string, unsigned>:: iterator> itr = listOfContactsMap.find(aKey);
  
        auto itr = listOfContactsMap.find(aKey);

        //remove assciated vectors::start
        
            //method 1
            //vecOfContactObjects.erase(vecOfContactObjects.begin() + itr->second);
     
            //method 2:: replacing curr ele with last ele
            unsigned last_index_vec = vecOfContactObjects.size()-1;
 
            vecOfContactObjects.at(itr->second) = vecOfContactObjects.at(last_index_vec);
           
            vecOfContactObjects.pop_back();
            
            listOfContactsMap.clear();
            
            if(vecOfContactObjects.size() > 0){
               
               cout<<"After:: vec size = "<<vecOfContactObjects.size()<<endl;
               
               for(unsigned i= 0; i<vecOfContactObjects.size(); i++){
                   string last_name = vecOfContactObjects.at(i).getLastName();
                   
                   listOfContactsMap.insert(make_pair(last_name, i));
               }
               return true;
            }
        //end
    }
}

Aucun commentaire:

Enregistrer un commentaire