468,491 Members | 1,916 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

How to print vector element address ?

19
Hi,

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

Thank you,
Murali
Apr 1 '08 #1
8 9227
gpraghuram
1,275 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. vector<int> v1;
  2.  
  3. cout<<&v1[0]<<endl;
  4.  
This will get u the address

Raghuram
Apr 1 '08 #2
mmk
19
You can print the address using the & operator and the index
Expand|Select|Wrap|Line Numbers
  1. vector<int> v1;
  2.  
  3. cout<<&v1[0]<<endl;
  4.  
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.

Expand|Select|Wrap|Line Numbers
  1. // vector::begin
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8.   vector<int> myvector;
  9.  
  10.   //  Fill vector with 1 to 5 ... 3 times
  11.   for (int i=1; i<=5; i++) myvector.push_back(i);
  12.   for (int i=1; i<=5; i++) myvector.push_back(i);
  13.   for (int i=1; i<=5; i++) myvector.push_back(i);
  14.  
  15.   vector<int>::iterator it;
  16.  
  17.   cout << "Your vector contains contains:";
  18.   for ( it=myvector.begin() ; it < myvector.end(); it++ )
  19.   cout << " " << *it;
  20.   cout << endl;
  21.  
  22.   for( it = myvector.begin(); it !=  myvector.end(); )
  23.   {
  24.       if( *it == 5 )
  25.       {
  26.           cout << "Erasing" << *it << endl;
  27.           cout << "Address of it is " << &myvector[it] << endl;
  28.           myvector.erase(it);
  29.       }
  30.       else
  31.       {
  32.           cout << "Incrementing it " << *it << endl;
  33.           cout << "Address of it is " << &myvector[it] << endl;
  34.           ++it;
  35.       }
  36.   }
  37.   cout << "After erasing all 5s it is pointing to" << *it << endl;
  38.  
  39.   cout << "Final  Vector list contains:";
  40.   for ( it=myvector.begin() ; it < myvector.end(); it++ )
  41.   cout << " " << *it;
  42.   cout << endl;
  43.  
  44.   return 0;
  45. }
  46.  
  47.  
In above code while trying to print the address of &myvector[it] (with iterator is giving an error message.

Please suggest me
Apr 1 '08 #3
MooX
1
Hi gpraghuram,


Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator it
  2.  
it is pointer

Expand|Select|Wrap|Line Numbers
  1. &myvector[*it]
  2.  
will work.

:)
Apr 1 '08 #4
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < myvector.size(); i++) {
  2.    cout << "Address of " << i << "th element is " << &myvector[i] << endl;
  3. }
Apr 1 '08 #5
mmk
19
Hi gpraghuram,


Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator it
  2.  
it is pointer

Expand|Select|Wrap|Line Numbers
  1. &myvector[*it]
  2.  
will work.

:)
Hi Moox,

This worked fine for me.

Thank you and all who responded for my query.

Cheers,
Mmk
Apr 2 '08 #6
mmk
19
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:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < myvector.size(); i++) {
  2.    cout << "Address of " << i << "th element is " << &myvector[i] << endl;
  3. }
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
Apr 2 '08 #7
Laharl
849 Expert 512MB
There is a size() member function in nearly every STL object. If you don't believe me, Google them and check.
Apr 2 '08 #8
Ganon11
3,652 Expert 2GB
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. .
Apr 2 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Sir_Ciph3r | last post: by
2 posts views Thread by sci | last post: by
13 posts views Thread by Joseph | last post: by
7 posts views Thread by digz | last post: by
6 posts views Thread by Jia | last post: by
3 posts views Thread by =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post: by
6 posts views Thread by .rhavin grobert | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.