Access contents of pointer to std::vector

Discussion in 'C++' started by Si, Nov 7, 2005.

  1. Si

    Si Guest

    How do I access the contents of an std:vector pointer (I mean access
    contents of the vector)?

    What is the correct notation for a pointer ( [index] doesn't seem to work)?
     
    Si, Nov 7, 2005
    #1
    1. Advertisements

  2. Si

    Ajay Kalra Guest

    Its just like another C++ object. There is nothing special about it. []
    works fine as well. What problems are you specifically having with it?
     
    Ajay Kalra, Nov 7, 2005
    #2
    1. Advertisements

  3. Perhaps you need this

    (*pointer)[index]

    but it's hard to be sure because your description is vague. Post the code!

    In any case ther is no special syntax for vectors or pointers to
    vectors, the usual stuff works.

    john
     
    John Harrison, Nov 7, 2005
    #3
  4. Si

    Eric Pruneau Guest

    Hi

    If I understand, you have something like

    vector<int> *MyVec;

    assuming the pointer is correctly allocated and there is data in...

    to access an element you can use at() function. at() simply return the
    element at the specified position but
    with a range check. If you dont mind the overhead of a range check use it.

    int elem = MyVec->at(0); // the first element
    MyVec->at(0) = 5;

    if you really want to use [] you can do

    int elem = (*Myvec)[0];
    (*Myvec)[0] = 5;

    Eric
     
    Eric Pruneau, Nov 7, 2005
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.