473,388 Members | 1,423 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,388 software developers and data experts.

Return an iterator

Hi,

When I return an iterator, should I return an iterator, or should I
return a reference to the iterator?

for example:
vector<A> aVector;

Should it be:
vector<A>::Iterator getBeginIterator() {
aVector.begin();
}

or

vector<A>::Iterator& getBeginIterator() {
aVector.begin();
}

Jan 25 '06 #1
3 7068
Al************@gmail.com wrote:
Hi,

When I return an iterator, should I return an iterator, or should I
return a reference to the iterator?

for example:
vector<A> aVector;

Should it be:
vector<A>::Iterator getBeginIterator() {
aVector.begin();
}

or

vector<A>::Iterator& getBeginIterator() {
aVector.begin();
}


What happened when you tried it? (Hint: one of these has a serious
error, and understanding that error will answer your question)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 25 '06 #2
Alan Johnson wrote:
for example:
vector<A> aVector;

Should it be:
vector<A>::Iterator getBeginIterator() {
aVector.begin();
}


Except for capitalization, this is correct.


And, perhaps, the fact that you didn't actually include a return statement.

-Alan
Jan 25 '06 #3
Al************@gmail.com wrote:
Hi,

When I return an iterator, should I return an iterator, or should I
return a reference to the iterator?

for example:
vector<A> aVector;

Should it be:
vector<A>::Iterator getBeginIterator() {
aVector.begin();
}

Except for capitalization, this is correct.
or

vector<A>::Iterator& getBeginIterator() {
aVector.begin();
}


If you have a reference, it must refer to some object. To what object
does the returned reference refer? If your answer is: The iterator
returned by aVector.begin(), then what happens to that iterator when the
getBeginIterator() function ends?

-Alan
Jan 25 '06 #4

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

Similar topics

8
by: Huibuh | last post by:
I start hating RETURN!!! list<happy> //#include <list>// is searched via iterator (ptr) so long until the argument "number" of the class "happy" is equal now. The function "findhappy" works...
5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
3
by: learning_C++ | last post by:
I hope to use template. in my code I hope to sum the vector<int> and vector<double>. But there are several errors: sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no type...
11
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
1
by: Marcin Kaliciński | last post by:
Hi, map::erase(iterator) and map::erase(iterator, iterator). In STL shipped with MSVC return value of these functions is iterator. In gcc 3.3 it is void. Which one is conforming? cheers,...
6
by: TOMERDR | last post by:
Hi,i am new to stl and i have a question regarding iterators I have class day contains a map of appointments: map<time_t,Appointment> m_Appointments; I would like to write a function...
2
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
6
by: antani | last post by:
VolumeType::ISetType::iterator findFirstIteratorMarked(const VolumeType::ISetType::iterator & it,const VolumeType::ISetType::iterator & e_it) { VolumeType::ISetType::iterator _it=it; while...
3
by: Jess | last post by:
Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.