#include <iostream>
#include <list>
usingnamespace std;
template <typename T>
void DisplayContents(const T& inList)
{
//list<T>::const_iterator element = inList.begin();
//list<int>::const_iterator element = inList.begin();
auto element = inList.begin();
for (element; element != inList.end(); ++element)
cout << *element << ", ";
cout << endl << endl;
}
template <typename T, typename Type>
void DisplayContents2(const T& inlist)
{
list<Type>::const_iterator element = inlist.begin();
for (element; element != inlist.end(); ++element)
cout << *element << ", ";
cout << endl << endl;
}
int main()
{
list <int> linkInts1;
linkInts1.insert(linkInts1.begin(), 222);
linkInts1.insert(linkInts1.begin(), 111);
linkInts1.insert(linkInts1.end(), 333);
DisplayContents(linkInts1);
//DisplayContents<list<int>>(linkInts1);
DisplayContents2<list<int>, int>(linkInts1);
return 0;
}
In my first DisplayContents function:
list<T>::const_iterator element = inList.begin();
...list<T> does not have the ability to deduce the type, based upon the list<int> that is sent in. I initially thought it might be similar to how the implicit conversion happens with functions & objects (below). But it does not appear to work that way with list<T>.
1 2 3 4 5 6 7 8 9 10 11
class ClassPrime
{
public:
ClassPrime(int intNum){}
};
void func1 (const classObject& obj1){}
int main{
func1 (10);
}
How would I get DisplayContents2 to work for list<Type>, lets say if I did not want to use auto? Thanks in advance!
1) Works out nicely then when you can use it like a typedef, but have to place typename in front. Thanks!
2) I have not seen C++20 concept/requires yet and I saw you also used it on your previous post of mine. I already acquired my C++20 book, but need more time before diving in.
3) I saw operator << that takes in ostream& and returns the stream to the cout in the main(). But it is trippy that you can set the stream = std::cout and have it return the stream and it just print it.
4) I know what the rest does, but still trying to figure out what....
...", typename A, template<typename,typename> class CNTR >" does with this
"( const CNTR<T,A>& cntr,..."
A) typename T (gets either "list<int>" or "vector<double>")
B) typename A (What does that get?????)
C) const CNTR<T,A>& cntr (that calls the 2nd template....template<T,A> class CNTR....??)
Note that template for std::list has two typenames: "T" and "Alloc". So does std::vector. We hardly ever notice that, because there is a default for Alloc.
> I know what the rest does, but still trying to figure out what....
> ...", typename A, template<typename,typename> class CNTR >" does with this
> "( const CNTR<T,A>& cntr,..."
CNTR is a template template parameter - for a template class having two template type parameters
A) typename T : the type of the element in the container. ( int in "list<int>" or double in "vector<double>"
C) const CNTR<T,A>& cntr : The template class taking two template arguments
In our example, std::list< int, std::allocator<int> > or std::vector< double, std::allocator<double> >