CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    94

    Question use of vector<int>::value_type?

    Hi,

    I wonder what is the use of value_type?
    If I type
    vector<int>::value_type i;
    i is an int right? But I had to specifiy the int in vector<int> anyway. So what is the use of that?
    I could have done
    int i;
    instead. I guess I am missing something.
    Thank you!

    PS: ok I guess this comes in to play when I get to templates.
    Last edited by w3rd; June 15th, 2009 at 11:02 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: use of vector<int>::value_type?

    Quote Originally Posted by w3rd
    ok I guess this comes in to play when I get to templates.
    Yes, I believe that is the idea, e.g., if the container type is T, you could get the value type as typename T::value_type.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2009
    Posts
    94

    Re: use of vector<int>::value_type?

    what do you mean by container type T? Like vector or list?
    vector::value_type wouldnt make much sense, would it?
    Well I will be patient still some hundred pages until templates...

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: use of vector<int>::value_type?

    value_type is used in templated functions and classes where it is the only way to determine the type used by a template parameter.

    Code:
    template <typename T>
    T::value_type Function(const T &t)
    {
        T::value_type i;
        ...
    
        return i;
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: use of vector<int>::value_type?

    Quote Originally Posted by w3rd
    what do you mean by container type T? Like vector or list?
    vector::value_type wouldnt make much sense, would it?
    Say, std::vector<int>, std::list<double>, std::vector<long, some_allocator_type>, etc.

    By the way, JohnW@Wessex's example is missing the typename keyword before typename T::value_type.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: use of vector<int>::value_type?

    value_type and similar typedefs are useful for writing generic code that can work with any STL container. Eg:

    Code:
    #include <vector>
    #include <list>
    #include <iostream>
    
    template<typename Container>
    class Foo
    {
      Container m_con;
    
    public:
      Foo(const Container& c) : m_con(c) { }
    
      typename Container::value_type
        GetFirst( ) { return *(m_con.begin( )); }
    };
    
    int main( )
    {
      std::vector<int> a;
      a.push_back(20);
    
      std::list<float> b;
      b.push_back(1.234f);
    
      Foo<std::vector<int> > c(a);
      Foo<std::list<float> > d(b);
    
      std::cout << c.GetFirst( ) << std::endl;
      std::cout << d.GetFirst( ) << std::endl;
      
      return 0;
    }

  7. #7
    Join Date
    Mar 2009
    Posts
    94

    Re: use of vector<int>::value_type?

    So JohnW: your Funtion() would be used like
    Code:
    std::vector<int> ivec;
    std::vector<int>::value_type result = Function(ivec)
    right? But it would work for different sequential containers with different value_types.

    ok thank you both.
    I am seeing it like through a mist now
    Will get there.


    OK one last question. Is there a difference between:
    std::vector<int>::value_type search_value;
    and
    int search_value; ?
    Last edited by w3rd; June 15th, 2009 at 11:39 AM.

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: use of vector<int>::value_type?

    std::vector<int>::value_type search_value;
    and
    int search_value; ?
    No, they're both ints. Outside of templates the main reason to use value_type would be with typedefs. If you use something like

    Code:
    typedef std::vector<int> DataVector;
    
    DataVector vec;
    DataVector::value_type value;
    cin >> value;
    vec.push_back(value);
    Then if you needed to change the type of data later on, say from int to double, you would only need to change the typedef.

  9. #9
    Join Date
    Mar 2009
    Posts
    94

    Re: use of vector<int>::value_type?

    Hey Speedo, thanks! I thought that it could be used to do that but I could not get a hold of how exactly. I tried
    Code:
    vec::value_type
    which didnt work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured