473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 13011
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

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

Similar topics

5
by: Sir_Ciph3r | last post by:
Can someone explain what is happening in the following code? #include <iostream> #include <vector> #include <map> using namespace std; int main() {
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
7
by: digz | last post by:
template<typename _Tp, typename _Alloc> void vector<_Tp, _Alloc>:: _M_insert_aux(iterator __position, const _Tp& __x) { if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) {...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
3
by: Vince C. | last post by:
Hi. I'm writing a C++ application that uses GNU getopt_long() to parse command line argument syntaxes. http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html I'd like to...
6
by: .rhavin grobert | last post by:
assume the following: ______________________ std::vector<foom_vFoo; // we assume foo has more than four elements! foo* FifthElement(foo* pFooZero) // we call +0 the zeroth element! { return...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.