I want the add function in the Carnet class to add a number to a position (position to be a string) and when I display myclass << ["string"] to display the number
the problem is that when I run the instructions in the main it shows me wrong(7,7,7), instead of showing me 7,9,10
I think the problem is saving in vector but I do not know how to fix it, I tried this:
this->insert(this->begin() + atoi(a.c_str()), b);
#include <iostream>
#include <vector>
using namespace std;
template <typename ElementT>
class Carnet : public vector<ElementT> {
public:
Carnet() : vector<ElementT>() {
}
int operator[] (string materie) {
return this->at(atoi(materie.c_str()));
}
Carnet add(string a, int b) {
this->insert(this->begin() + atoi(a.c_str()), b);
return *this;
}
Carnet removeLast() {
this->pop_back();
return *this;
}
};
int main()
{
Carnet<int> cat;
cat.add("SDA", 9);
cat.add("OOP",7).add("FP", 10);
cout<<cat["OOP"];
cout<<cat["SDA"];
cout<<cat["FP"];
cat.removeLast().removeLast();
return 0;
}