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);
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]
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.
Comment