Thread: Typeid of vectors

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Typeid of vectors

    Here is a code I wrote to check the type of a variable in run time. In this case I am trying to get the type info of a vector.

    But when I run this program I get a cryptic string from which it cannt be derived as which type of vector it is.

    Code:
    #include <typeinfo>
    
    using namespace std;
    
    //register uint32_t AVP_LENGTH_VALUE = htonl(0x00FFFFFF);
    
    int main()
    {
            vector<int> list;
    
            cout << typeid(list).name() << endl;
    }
    The output is
    Code:
    St6vectorIiSaIiEE
    Please suggest a way to decode this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Quote Originally Posted by laserlight View Post
    Why do you want to do this?
    Seconded. But in case the reason is valid why not do:

    Code:
        std::vector<int> abc;    
        if(typeid(std::vector<int>) == typeid(abc))
        {
            std::cout<<"Matching Type";
        }//if
    Woop?

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Code:
    #include <vector>
    A good idea when using vectors.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Why do you want to do this?
    I would like to perform some operation on the variable depending on the type of variable.

    For example:
    writing a toString() implementation, that will convert the object into string representation.

    if(type == "vector<int>")
    {

    }

    if(type == "vector<SomeClass>")
    {

    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sanjivko
    I would like to perform some operation on the variable depending on the type of variable.
    Ah, but then manually checking the variable's type should be a last resort.

    Quote Originally Posted by sanjivko
    For example:
    writing a toString() implementation, that will convert the object into string representation.
    Write a function template instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These kinds of things are handled by templates. Runtime type information will be completely useless for this purpose.

    For example, a function to turn a vector into a string representation might look like this:

    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    
    template <class T>
    std::string to_string(const std::vector<T>& vec)
    {
        std::stringstream result;
        result << "(";
        if (!vec.empty()) {
            result << vec[0];
            for (std::size_t i = 1; i != vec.size(); ++i) {
                result << ", " << vec[i];
            }
        }
        result << ")";
        return result.str();
    }
    
    int main()
    {
        std::vector<int> vec;
        std::cout << to_string(vec) << '\n';           // ()
        vec.push_back(1);
        std::cout << to_string(vec) << '\n';           // (1)
        std::vector<std::string> string_vec;
        string_vec.push_back("Hello");
        string_vec.push_back("world");
        std::cout << to_string(string_vec) << '\n';    //(Hello, world)
    }
    This relies on ostream operator<< being overloaded for the type T (the standard library way to convert something to its string representation).

    If you need to use a variable of type T, then you know what it is: T.

    Code:
    template <class T>
    void foo(const std::vector<T>& vec)
    {
         T first = vec.front();
         ...
    }
    Last edited by anon; 10-21-2010 at 02:48 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anon
    For example, a function to turn a vector into a string representation might look like this:
    I think phantomap once mentioned to me that an implementation is permitted additional template parameters for the standard containers, so even if we account for the allocator, it is still a little iffy as to how portable this might be. However, we could write:
    Code:
    template <class T>
    std::string to_string(const T& container)
    {
        std::stringstream result;
        result << "(";
        if (!container.empty()) {
            typedef typename T::const_iterator iterator_type;
            iterator_type i = container.begin();
            result << *i;
            ++i;
            for (const iterator_type end = container.end(); i != end; ++i) {
                result << ", " << *i;
            }
        }
        result << ")";
        return result.str();
    }
    EDIT:
    As a bonus, I expect that this will not only work for std::vector, but also std::deque, std::list, and even std::set and std::multiset.
    Last edited by laserlight; 10-21-2010 at 03:07 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Smile

    Quote Originally Posted by anon View Post
    These kinds of things are handled by templates. Runtime type information will be completely useless for this purpose.

    For example, a function to turn a vector into a string representation might look like this:

    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    
    template <class T>
    std::string to_string(const std::vector<T>& vec)
    {
        std::stringstream result;
        result << "(";
        if (!vec.empty()) {
            result << vec[0];
            for (std::size_t i = 1; i != vec.size(); ++i) {
                result << ", " << vec[i];
            }
        }
        result << ")";
        return result.str();
    }
    
    int main()
    {
        std::vector<int> vec;
        std::cout << to_string(vec) << '\n';           // ()
        vec.push_back(1);
        std::cout << to_string(vec) << '\n';           // (1)
        std::vector<std::string> string_vec;
        string_vec.push_back("Hello");
        string_vec.push_back("world");
        std::cout << to_string(string_vec) << '\n';    //(Hello, world)
    }
    This relies on ostream operator<< being overloaded for the type T (the standard library way to convert something to its string representation).

    If you need to use a variable of type T, then you know what it is: T.

    Code:
    template <class T>
    void foo(const std::vector<T>& vec)
    {
         T first = vec.front();
         ...
    }

    Anon, thanks for the reply this is what I exactly needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. how to uuse 'typeid'
    By John_L in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2008, 07:44 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM