I have the following as an Stl algorithm/vector:

MyClass <int> VecOfInts("A vector of integers");
VecOfInts.push_back(3)
VecOfInts.push_back(2)
VecOfInts.push_back(1)

I was able to get the numbers to display with a for loop (i < VecOfInts <size; i++).
Now my question is: how would I get "A Vector of integers", to display, irregardless of the instance.

Something like this:

#include <iostream>
#include <vector>

template < typename T > struct my_class : public std::vector<int>
{
    typedef std::vector<T> base ;
    using base::iterator ;
    // etc

    explicit my_class( const char* n ) : name(n) {}

    // ...

    private: std::string name ;

    friend inline std::ostream& operator<< ( std::ostream& stm, const my_class<T>& mc )
    {
        stm << mc.name << ": [ ";
        for( const T& v : mc ) stm << v << ' ' ;
        return stm << ']' ;
    }
};

int main()
{
    my_class<int>  VecOfInts( "A vector of integers" ) ;
    VecOfInts.push_back(3) ;
    VecOfInts.push_back(2) ;
    VecOfInts.push_back(1) ;
    std::cout << VecOfInts << '\n' ;
}

My question is how would i display the followings strings: "A Vector of integers", in the program at run time?
Another vector could be called "A Vector of DOUBLES". And I'd want to display that. Both will indeed utilize template functions.

Specifically, outside of the int main. It will be in a print function.

Also note i'm using the headers #include <algorithm>, #include <string>, not just vector.

push_back wasn't done in int main, I placed a call to a class member function that would perform push back.

Still making a wild guess as to what is it that you really want.

Assuming that you want to print out the contents of a standard sequence container along with a tag indicating the type of the container and its value_type
(via a function which is not an overloaded operator):

#include <typeinfo>
#include <string>

template< typename T > std::string type_name() ;

#ifdef __GNUG__

#include <cxxabi.h>

template< typename T > std::string type_name()
{
    char temp[ 2048 ] ;
    std::size_t sz = sizeof(temp) ;
    int status ;
    __cxxabiv1::__cxa_demangle( typeid(T).name(), temp, &sz, &status ) ;
    return temp ;
}

#else // not GNU compatible

template< typename T > std::string type_name() { return typeid(T).name() ; }

#endif // __GNUG__

#include <iostream>
#include <vector>
#include <list>

template < typename SEQUENCE_CONTAINER >
std::ostream& print( const SEQUENCE_CONTAINER& seq, std::ostream& stm = std::cout )
{
    const std::string cntr_name = type_name<SEQUENCE_CONTAINER>() ;
    const std::string value_type_name = type_name< 
                                     typename SEQUENCE_CONTAINER::value_type >() ;
    stm << cntr_name.substr( 0, cntr_name.find('<') ) << " of " 
        << value_type_name << " [ " ;
    for( const auto& v : seq ) stm << v << ' ' ;
    return stm << ']' ;
}


int main()
{
    std::vector< std::string > a = { "how", "now", "brown", "cow" } ;
    print(a) << '\n' ; // prints: std::vector of std::string [ how now brown cow ]

    std::list<double> b = { 1.2, 3.45, 6.789 } ;
    print(b) << '\n' ; // prints: std::list of double [ 1.2 3.45 6.789 ]
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.