| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Deleting what i want in vectors
in a vector i put in what it is i want.
like cust id 1001 reg AAA ect up to 1005 and EEE. how do i make it delete 1003 CCC from the vector. code i have is this. void Manager::showHires() { string reg; system("cls"); cout << endl << "List of Hires" << endl << "==============" << endl; vector <Hired>::iterator iter = theHired.begin(); for(iter = theHired.begin();iter != theHired.end();++iter) { if(!isHired(reg)) { (*iter).getDescription(); cout << endl; } } } void Manager::returnHire() { bool custfound = false; bool carfound = false; int id; cout << "Enter Customer ID: "; cin >> id; if(findCustomer(id)) { custfound = true; } string reg; cout << "Enter CarRegno: "; cin >> reg; if(findVehicle(reg)) { carfound = true; } if(custfound == false) { cout << "CustomerID not found!" << endl; } if(carfound == false) { cout << "Car RegNo not found!" << endl; } else { if(isHired(reg)) { cout << "Car was hired and has now been set to AVAILABLE!" << endl; setHired(reg,false); Hired h(id,reg); theHired.erase_element(h); } else { cout << "Car has already been returned!" << endl; } } } void Manager::createHire() { bool custfound = false; bool carfound = false; int id; cout << "Enter Customer ID: "; cin >> id; if(findCustomer(id)) { custfound = true; } string reg; cout << "Enter CarRegno: "; cin >> reg; if(findVehicle(reg)) { carfound = true; } if(custfound == false) { cout << "CustomerID not found!" << endl; } if(carfound == false) { cout << "Car RegNo not found!" << endl; } else { if(!isHired(reg)) { cout << "Car is available and has been set to HIRED!" << endl; setHired(reg,true); Hired h(id,reg); theHired.push_back(h); } else { cout << "Car is NOT available!" << endl; } } } trying to make it when i return the car it deletes what i put in and still shows the rest as they are still hired. and help would be gratful |
|
#2
|
|||
|
|||
|
Don't know if this will apply to your problem, but there is a member function common to what you want to do. that is:
Code:
vectorName.erase(position) This code deletes the element at the position specified by the iterator. |
|
#3
|
|||
|
|||
|
everytime i try this bit of code i get an error.
the program adds hires in through the vector but it's not always the last or first hire that gets returned so i tryed Hired h(id,reg); theHired.erase(h); hoping that after i have put the details in of the car im returning the erase might take it out, anyone got other idea's??? |
|
#4
|
||||
|
||||
|
Quote:
Hired h(id,reg); theHired.erase(theHired.find(h)); If you know its offset into the vector, do something like theHired.erase(theHired.begin()+3); // erase the 3rd element
__________________
Quote:
|
|
#5
|
|||
|
|||
|
there is no such thing as a .find in a vector.
when i tryed it i got errors and when i looked up what goes with vectors it was not in it. i tryed doing this instead Hired h(id,reg); theHired.erase(theHired.at(h)); but i get this error error C2664: 'const Hired &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'Hired' to 'unsigned int' 1> with 1> [ 1> _Ty=Hired 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called |
|
#6
|
|||
|
|||
|
Quote:
Oops. Code:
#include <vector>
using std::vector;
#include <algorithm>
using std::find;
int main() {
vector<int> v;
// fill v
int value = 5; // first to compare equal is removed
v.erase(find(v.begin(), v.end(), value));
}
|
|
#7
|
|||
|
|||
|
You could say :
Code:
#include <vector>
using std::vector;
#include <algorithm>
using std::find;
int main() {
vector<int> v;
// fill v
v[0] = 0;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Deleting what i want in vectors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|