vector problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stefaan
    New Member
    • Jul 2007
    • 5

    vector problem

    hi all,

    i have a bit of a problem with pointers to vector. I have a map

    Code:
    map<int , vector<int> * > children;
    now I can access all the vectors trough commands like children[i]->begin().

    the question is how do I gain random acces to an element of a vector to edit the value?

    I have tried *(children[i])[j] = 5; but this gives me a error C2100: illegal indirection

    I have tried other options but these are just wrong. I know I can evade the problem by using iterators, but I use rather large vectors so this is not optimal.

    cheers,
    stefaan
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by stefaan
    hi all,

    i have a bit of a problem with pointers to vector. I have a map

    Code:
    map<int , vector<int> * > children;
    now I can access all the vectors trough commands like children[i]->begin().

    the question is how do I gain random acces to an element of a vector to edit the value?

    I have tried *(children[i])[j] = 5; but this gives me a error C2100: illegal indirection

    I have tried other options but these are just wrong. I know I can evade the problem by using iterators, but I use rather large vectors so this is not optimal.

    cheers,
    stefaan
    Go slowly here: children[i] is a pointer to vector, so *(children[i]) is the vector
    itself and (*(children[i]))[5] is the fifth child. When in doubt parenthesize.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Actually, you would code:
      [code=cpp]
      int main()
      {
      map<int , vector<int> * > children;
      vector<int>* v = children[5];
      int answer = (*v)[3];
      }
      [/code]

      The key 5 will return a value (pair::second), which is a pointer to a vector<int>. So, *v is the vector, so (*v)[3] is the 4th int in that vector.

      It's not going to work like a 2D array but you can bury this logic in a function that is used throughout your code.

      Be careful using pointers in STL conatiners. You should use a handle instead.

      Comment

      • phiefer3
        New Member
        • Jun 2007
        • 67

        #4
        Just a question, but isn't it usually better to use -> instead of (* ) when you have a pointer to an object? Such as:

        children[i]->[j] = 5;

        Or won't that work with []? I haven't tried it.

        Comment

        • Darryl
          New Member
          • May 2007
          • 86

          #5
          Originally posted by phiefer3
          Just a question, but isn't it usually better to use -> instead of (* ) when you have a pointer to an object? Such as:

          children[i]->[j] = 5;

          Or won't that work with []? I haven't tried it.
          Won't work, at best you'd have to

          children[i]->operator[](j) = 5; //quite ugly

          but better, you can use at()

          children[i]->at(j) = 5;

          Comment

          • stefaan
            New Member
            • Jul 2007
            • 5

            #6
            thanks all,

            (*(children[i]))[5] = 0 does the trick perfectly. Apparently the brackets do matter :)

            I don't know handles for pointers, will look in to it later.

            I know -> is the general way to go with pointers but ->[5] doesn't work.

            the reason at is no good is becaus it just returns the value at say 5. I wanted to edit the value at 5. sorry if this wasn't clear.

            anyway thanks all,

            stefaan
            Last edited by stefaan; Jul 16 '07, 10:15 AM. Reason: additional question

            Comment

            • Darryl
              New Member
              • May 2007
              • 86

              #7
              Originally posted by stefaan
              thanks all,



              the reason at is no good is becaus it just returns the value at say 5. I wanted to edit the value at 5. sorry if this wasn't clear.


              stefaan
              Actually it returns a reference so children[i]->at(5) = 0 would edit the value at 5.

              additionally at() reads cleaner than the pointer dereference and at() has bounds checking which may be desirable if speed is not a concern

              Comment

              Working...