but why did you write "struct" instead of "class"? |
My understanding of struct, and still is, is that they're basically the same. The only main difference is that members of a struct are public and the members of a class are private by default. This doesn't mean anything if you get in the habit of declaring access for you members.
and the templated list must include same data members? |
I believe you meant you must include all of the same data types. If so, yes. If you make a list of integers, you can only add integers to it (minus some typecasting). But that's the purpose of a list. If you make a grocery list to go to the store, you're not going to put all of your family and friends' birthday on it.
for example class T and some integer i, and another list with double. possible ? |
Yes, you can make as many different lists as you want. That's the beauty of templates. You can even make up your own class to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <list>
class Names {
// Some Stuff
}
// Some Other Stuff
int main() {
std::list<int> myIntList;
std::list<double> myDoubleList;
std::list<Names> myNamesList;
return 0;
}
|
I hope that answered your questions. If you're unsure, you can always play around with the standard library list. You can't do any damage, but it may help your understanding a bit better. Just remember, a linked list allows you to read through the list from the top to the end only (unless it's doubly linked, then you can read it end to top), you can't just get an item out of the middle of it like you can with a vector. There are a bunch of different containers if you want to see what you can and can't do with each of them.