typename iterator_traits::pointer

I

Ioannis Vranos

I am reading TC++PL3 and on page 552 it is mentioned:


"The related types of an iterator are described by a small set of
declarations in an iterator_traits template class:

template <class Iter> struct iterator_traits {
typedef typename Iter::iterator_category iterator_category; //19.2.3
typedef typename Iter::value_type value_type; // type of element
typedef typename Iter::difference_type difference_type;
typedef typename Iter::pointer pointer; //return type of operator->()
typedef typename Iter::reference reference; //return type of operator*()
};"


How can we use Iter.operator->() to get an iterator_traits<Iter>::
pointer? Let's assume vector<int> as an example and we want to get a
pointer to the first element of the sequence using operator->():


#include <vector>
#include <iostream>
#include <iterator>

int main()
{
using namespace std;

vector<int> vec(10);

iterator_traits<vector<int>::iterator>::pointer p= ???
}
 
V

Victor Bazarov

Ioannis said:
I am reading TC++PL3 and on page 552 it is mentioned:


"The related types of an iterator are described by a small set of
declarations in an iterator_traits template class:

template <class Iter> struct iterator_traits {
typedef typename Iter::iterator_category iterator_category; //19.2.3
typedef typename Iter::value_type value_type; // type of element
typedef typename Iter::difference_type difference_type;
typedef typename Iter::pointer pointer; //return type of
operator->() typedef typename Iter::reference reference; //return
type of operator*() };"


How can we use Iter.operator->() to get an iterator_traits<Iter>::
pointer?

You can't. The code will immediately use the pointer that operator->
returns to access the member expression following the -> token. IOW
the overloaded operator-> can never be used to initialise a pointer,
for example (unless you have a member that returns 'this', then you
need to call that member).
Let's assume vector<int> as an example and we want to get a
pointer to the first element of the sequence using operator->():


#include <vector>
#include <iostream>
#include <iterator>

int main()
{
using namespace std;

vector<int> vec(10);

iterator_traits<vector<int>::iterator>::pointer p= ???
}

There is no syntax in C++ to do what you seem to want/need.

V
 
P

Pete Becker

There is no syntax in C++ to do what you seem to want/need.

Sure there is:

p = vec.begin().operator->();

A more natural way to write it, without the artificial requirment of
using operator->, would be:

p = &*vec.begin();

which is the usual technique for getting the address of an element,
given an iterator.
 
I

Ioannis Vranos

Pete said:
Sure there is:

p = vec.begin().operator->();

A more natural way to write it, without the artificial requirment of
using operator->, would be:

p = &*vec.begin();

which is the usual technique for getting the address of an element,
given an iterator.


And what is the use/need of Iter.operator->()?
 
B

Bo Persson

Ioannis said:
And what is the use/need of Iter.operator->()?

Nothing much for vector<int>, but might be useful for accessing the
members of vector<some_struct>.


Bo Persson
 
B

Bo Persson

Ioannis said:
May you provide a working example?

Just think about it - if you have an iterator to an element of the
vector, how do you access a member of some_struct?


Bo Persson
 
R

red floyd

Ioannis said:
May you provide a working example?

Sure.

#include <utility>
#include <vector>
#include <iostream>
#include <ostream>

int main()
{
typedef std::vector< std::pair<int, int> > pairvec;
pairvec v;

for (int = 0; i < 10; ++i)
v.push_back(std::make_pair(i, 2*i));

for (pairvec::iterator it = v.begin();
it != v.end();
++it)
{
std::cout << it->first << "," << it->second << "\n";
}
std::cout << std::endl;
return 0;
}
 
R

red floyd

red said:
Sure.

#include <utility>
#include <vector>
#include <iostream>
#include <ostream>

int main()
{
typedef std::vector< std::pair<int, int> > pairvec;
pairvec v;

for (int = 0; i < 10; ++i) TYPO!!! should be int i = 0;
v.push_back(std::make_pair(i, 2*i));

for (pairvec::iterator it = v.begin();
it != v.end();
++it)
{
std::cout << it->first << "," << it->second << "\n";
}
std::cout << std::endl;
return 0;
}
 
I

Ioannis Vranos

Bo said:
Just think about it - if you have an iterator to an element of the
vector, how do you access a member of some_struct?

#include <vector>
#include <string>
#include <iostream>

struct some_struct
{
std::string s;
};


int main()
{
using namespace std;

vector<some_struct> vec(10);

vec.begin()->s= "Test";

iterator_traits<vector<some_struct>::iterator >::pointer p=
vec.begin().operator->();

cout<< vec[0].s<< endl;

//cout<< *p<< endl;


}

Thw commented line statement doesn't compile. Why?
 
I

Ioannis Vranos

Ioannis said:
Bo said:
Just think about it - if you have an iterator to an element of the
vector, how do you access a member of some_struct?

#include <vector>
#include <string>
#include <iostream>

struct some_struct
{
std::string s;
};


int main()
{
using namespace std;

vector<some_struct> vec(10);

vec.begin()->s= "Test";

iterator_traits<vector<some_struct>::iterator >::pointer p=
vec.begin().operator->();

cout<< vec[0].s<< endl;

//cout<< *p<< endl;


}

Thw commented line statement doesn't compile. Why?


The interesting part is that the compiler mentions that
iterator_traits<vector<some_struct>::iterator >::pointer p
is some_struct *.


And it makes sense. some_struct is the member type of the vector, so the
member type pointed is some_struct. So p is a some_struct * and the cout
should be:


cout<< p->s << endl;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top