Hi (again)
I just want to check that this is the correct way to use templates. I have just done an exercise from my book that requires me to create a template class that holds pairs of data. So I chose string name and int age. Then to store them in a vector to be able to access.
My code is:
The code works, and I am aware that the data members of the class should be private. I've just left them public for ease of access.Code:template <class T, class U> class pairs { public: pairs(T n, U a) :name(n), age(a) {} T name; U age; }; int main() { vector<pairs<string, int>> pair; string name; int age; while(cin >> name >> age) pair.push_back(pairs<string, int>(name, age)); for(int i = 0; i < pair.size(); ++i) { cout << pair[i].name << '\n' << pair[i].age << endl; } cin.get(); return 0; }
Is this the correct way to implement templates and to use them to store information in a vector?
Thanks.



LinkBack URL
About LinkBacks




air. WIll have a read up on it. 