value_type of vector

This is a discussion on value_type of vector within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I think the value_type of vector<int> should be int and the value_type of vector<int> should be int*. But ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    value_type of vector

    Hello everyone,


    I think the value_type of vector<int> should be int and the value_type of vector<int> should be int*. But I am not 100% sure,

    1. how to write a program to verify this idea;
    2. where to find the definition that the value_type of interator of vector<int> is int*.

    It is appreciated if you could help.

    Code:
    #include <vector>
    
    using namespace std;
    
    int main (int argc, char** argv)
    {
    	vector<int>::value_type;  // should be int ?
    	vector<int>::iterator::value_type; // should be int* ?
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    I think the value_type of vector<int> should be int and the value_type of vector<int> should be int*
    That sounds like a contradiction

    The value_type of vector<int> is int. But vector<int>::iterator may not necessarily be a pointer type, so we cannot say for sure if it is actually an int*.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    The implementation of iterators is undefined in the C++ standard and can vary from compiler to compiler. It may be a pointer on one system, but it could be some type of object on another system.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, it is a type. You think it is a int*, so declare an iterator::value_type variable and try to assign a pointer to it.

    Code:
    #include <vector>
    
    int main()
    {
        int* p = 0;
        std::vector<int>::iterator::value_type c = p;
    }
    Nope, I get
    Untitled1.cpp:6: error: invalid conversion from `int*' to `int'
    Make your conclusions.

    You might also try and locate this typedef in your STL implementation.
    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).

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    For any standard container C, it is guaranteed that C::value_type == std::iterator_traits<C::iterator>::value_type. Everything else would be stupid. The iterator over a container refers to the elements of the container, thus the referred-to type of the iterator (its value_type) must be the same as the element type of the container (its value_type).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    George2 pretty much had his question answered in another community that some of us here visit: value_type of vector
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21