473,511 Members | 11,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using iterator in template function

I have a question about using iterator in template function

//*****code starts here*****************************
#include <vector>
using std::vector;

template<typename Tvoid foo( vector<T& a )
{
vector<T>::iterator i; // ERROR, can't use iterator
}

int main()
{
vector<inta;
foo(a);
}
//******code ends here ****************************
//****** compiler: gcc 4.1.3 ************************

Can anyone tell me why it's not allowed to use the iterator this way?
Maybe the best way is to use Iterator directly as STL does.
template<typename Itervoid foo(Iter b, Iter e);
Thank you.

Nov 8 '07 #1
1 2179
On 2007-11-08 11:10:26 -0500, wuych <ya********@gmail.comsaid:
I have a question about using iterator in template function

//*****code starts here*****************************
#include <vector>
using std::vector;

template<typename Tvoid foo( vector<T& a )
{
vector<T>::iterator i; // ERROR, can't use iterator
}

int main()
{
vector<inta;
foo(a);
}
//******code ends here ****************************
//****** compiler: gcc 4.1.3 ************************

Can anyone tell me why it's not allowed to use the iterator this way?
vector<T>::iterator is what's known as a "dependent name". Its meaning
might be different for different types T (although a program that
specialized vector<Whateverand made iterator the name of a data
object would be quite perverse). So the rule is that you have to tell
the compiler that vector<T>::iterator names a type:

typename vector<T>::iterator i; // okay
Maybe the best way is to use Iterator directly as STL does.
template<typename Itervoid foo(Iter b, Iter e);
Maybe. That way your algorithm can operate on any sequence, and not
just on a vector. That's what most of the standard library algorithms
do. But it depends on what that function is actually doing.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2335
by: Carlo Milanesi | last post by:
Let's say I want to write the following function, void f(const list<int> &l, const vector<int> &v) { cout << get_second(l) << '\n'; cout << get_second(v) << '\n'; cout << get_nth(l, 2) << '\n';...
0
1369
by: CoolPint | last post by:
I am trying to write a generic heapsort (of course as a self-exercise) with Iterator interface: something like blow.... But I got into trouble finding out the Iterator to the Child node. If...
2
1394
by: Tom McCallum | last post by:
I have looked all afternoon and I can't find a definite answer on the following problem. I have defined a class My question is why can't I define my iterator below? template<class T> class...
13
9636
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
7114
by: Simon Elliott | last post by:
I have some code where it's necessary to convert an iterator to a pointer, to pass the pointer to a legacy function. I've been looking at ways of encapsulating this, so I put together a short test...
10
1605
by: Pierre Thibault | last post by:
Hello! I am currently trying to port a C++ code to python, and I think I am stuck because of the very different behavior of STL iterators vs python iterators. What I need to do is a simple...
6
2919
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
16
2555
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
6
1826
by: tomerdr | last post by:
Hi, I have the following function and functor and i want to print all values of a map but pair does not support operator << of course. so i wonder what are my options? can i extend the pair...
0
7367
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
7430
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
5673
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,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
451
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.