Thread: Another template question...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Another template question...

    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:
    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;
    }
    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.

    Is this the correct way to implement templates and to use them to store information in a vector?

    Thanks.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Looks good. I know this was done as an exercise, but you might also read up on std::pair to see what the standard library does differently.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay thanks. I wasn't aware of std:air. WIll have a read up on it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just two notes:
    • It's possible to write typename instead of class. The class keyword is actually an old keyword maintained for backwards compatibility.
    • Be careful with T1<T2<T3>>. This is not valid C++03 (two > in template declarations must not exist in C++03; separate them with spaces), but it is valid C++0x.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Just two notes:
    • It's possible to write typename instead of class. The class keyword is actually an old keyword maintained for backwards compatibility.
    • Be careful with T1<T2<T3>>. This is not valid C++03 (two > in template declarations must not exist in C++03; separate them with spaces), but it is valid C++0x.
    First point is noted. Will use typename from now.

    I'm not sure how you mean with the 2nd point. How should the template declaration have been written for C++03?

    Thanks again.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by darren78 View Post
    I'm not sure how you mean with the 2nd point. How should the template declaration have been written for C++03?
    I forgot about this since my compiler has extensions allowing it. The problem is a potential parse error caused by '>>'.
    Code:
    vector<pairs<string, int>> pair;
    C++03 will interpret >> as operator>>, not as the closing brackets of a template. To fix it, you have to include spaces.
    Code:
    vector<pairs<string, int> > pair;
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay, thanks for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template already instantiated
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2010, 12:42 PM
  2. Assignment issues
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2010, 12:55 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM