How to print vector element address ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmk
    New Member
    • Oct 2006
    • 19

    How to print vector element address ?

    Hi,

    Anyone can suggest me how to print vector element address in c++.

    Thank you,
    Murali
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by mmk
    Hi,

    Anyone can suggest me how to print vector element address in c++.

    Thank you,
    Murali

    You can print the address using the & operator and the index
    [code=cpp]
    vector<int> v1;

    cout<<&v1[0]<<endl;
    [/code]

    This will get u the address

    Raghuram

    Comment

    • mmk
      New Member
      • Oct 2006
      • 19

      #3
      Originally posted by gpraghuram
      You can print the address using the & operator and the index
      [code=cpp]
      vector<int> v1;

      cout<<&v1[0]<<endl;
      [/code]

      This will get u the address

      Raghuram
      Dear raghu, i tried about what you have suggested. But, still I am facing the problem. Please suggest me how to resolve this.

      [code=cpp]
      // vector::begin
      #include <iostream>
      #include <vector>
      using namespace std;

      int main ()
      {
      vector<int> myvector;

      // Fill vector with 1 to 5 ... 3 times
      for (int i=1; i<=5; i++) myvector.push_b ack(i);
      for (int i=1; i<=5; i++) myvector.push_b ack(i);
      for (int i=1; i<=5; i++) myvector.push_b ack(i);

      vector<int>::it erator it;

      cout << "Your vector contains contains:";
      for ( it=myvector.beg in() ; it < myvector.end(); it++ )
      cout << " " << *it;
      cout << endl;

      for( it = myvector.begin( ); it != myvector.end(); )
      {
      if( *it == 5 )
      {
      cout << "Erasing" << *it << endl;
      cout << "Address of it is " << &myvector[it] << endl;
      myvector.erase( it);
      }
      else
      {
      cout << "Incrementi ng it " << *it << endl;
      cout << "Address of it is " << &myvector[it] << endl;
      ++it;
      }
      }
      cout << "After erasing all 5s it is pointing to" << *it << endl;

      cout << "Final Vector list contains:";
      for ( it=myvector.beg in() ; it < myvector.end(); it++ )
      cout << " " << *it;
      cout << endl;

      return 0;
      }

      [/code]

      In above code while trying to print the address of &myvector[it] (with iterator is giving an error message.

      Please suggest me

      Comment

      • MooX
        New Member
        • Apr 2008
        • 1

        #4
        Hi gpraghuram,


        [code=cpp]
        vector<int>::it erator it
        [/code]

        it is pointer

        [code=cpp]
        &myvector[*it]
        [/code]

        will work.

        :)

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          However, *it will only give you a number between 1 and 4, so you will get the addresses of the second through fourth element of the vector repeatedly. Your problem is you are trying to mix iterator access with array-index-like access, which shouldn't be done. If you want to print the addresses of the vector element, you should use for loops:

          [CODE=cpp]for (int i = 0; i < myvector.size() ; i++) {
          cout << "Address of " << i << "th element is " << &myvector[i] << endl;
          }[/CODE]

          Comment

          • mmk
            New Member
            • Oct 2006
            • 19

            #6
            Originally posted by MooX
            Hi gpraghuram,


            [code=cpp]
            vector<int>::it erator it
            [/code]

            it is pointer

            [code=cpp]
            &myvector[*it]
            [/code]

            will work.

            :)
            Hi Moox,

            This worked fine for me.

            Thank you and all who responded for my query.

            Cheers,
            Mmk

            Comment

            • mmk
              New Member
              • Oct 2006
              • 19

              #7
              Originally posted by Ganon11
              However, *it will only give you a number between 1 and 4, so you will get the addresses of the second through fourth element of the vector repeatedly. Your problem is you are trying to mix iterator access with array-index-like access, which shouldn't be done. If you want to print the addresses of the vector element, you should use for loops:

              [CODE=cpp]for (int i = 0; i < myvector.size() ; i++) {
              cout << "Address of " << i << "th element is " << &myvector[i] << endl;
              }[/CODE]
              Dear Ganon11,

              myvector is a vector object. As i know there is no size() member function in c++ concept. If I am wrong please correct me.

              Cheers,
              MMk

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                There is a size() member function in nearly every STL object. If you don't believe me, Google them and check.

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Originally posted by mmk
                  Dear Ganon11,

                  myvector is a vector object. As i know there is no size() member function in c++ concept. If I am wrong please correct me.

                  Cheers,
                  MMk
                  Laharl is correct. .

                  Comment

                  Working...