return iterators

trying to move through a list and return a iterator to the location

Page 1 of 1

5 Replies - 1890 Views - Last Post: 16 February 2010 - 07:59 AM Rate Topic: -----

#1 knightmare2dream  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 13-February 10

return iterators

Posted 16 February 2010 - 12:01 AM

I'm trying to create a function that moves through the list of customers, and after finding the matching name return its position for further manipulation.
What is the return type for a list iterator (or any container iterator)?

list<Customer> customerList;	// list of customers

	void searchCustomers(string fname, string lname, bool& found)
	{
		found = false;   //set found to false
	   
		list<Customer>::iterator location=customerList.begin();
		
		while (location!=customerList.end() && !found)     //search the list
		{
			if (location->checkName(fname,lname)) //the item is found
				found = true;
			else
				location++; //advance to the next node
			
		}
	}



This post has been edited by knightmare2dream: 16 February 2010 - 12:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: return iterators

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: return iterators

Posted 16 February 2010 - 12:20 AM

You may like to see this student demo of using STL list iterators ...

List iter demo ...
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: return iterators

Posted 16 February 2010 - 06:17 AM

View Postknightmare2dream, on 16 February 2010 - 07:01 AM, said:

I'm trying to create a function that moves through the list of customers, and after finding the matching name return its position for further manipulation.
What is the return type for a list iterator (or any container iterator)?
iterators aren't functions, so they do not have 'return types' (The result type of putting an iterator on the right-hand side of an assignment operation is a copy of that iterator)

Also remember that linked lists are not random access sequences, so knowing the 'position' of an element is meaningless - and is one of the reasons why iterators are used in the first place; i.e. The iterator is the mechanism which allows you to directly manipulate that data - iterators use "pointer" semantics, in that they provide a level of indirection to the data contained within the list - they exist like a reference object or a bookmark object whose purpose is to provide you with a portable access point to an element within the list.

So, with a list of names, if you wanted to update "steve" to "rupert", you might do this:
#include <list>
#include <algorithm>
#include <string>
#include <iostream>

int main()
{
    std::list<std::string> names;
    names.push_back("bob");
    names.push_back("steve");
    names.push_back("harry");
    names.push_back("simon");
    names.push_back("paul");

    std::list<std::string>::iterator i
        = std::find(names.begin(), names.end(), "steve");

    if( i != names.end() )
        *i = "rupert";

    for(i = names.begin(); i != names.end(); ++i)
        std::cout << *i << std::endl;
} 
in this context, the * operator is the de-referencing operator; the result of a de-referencing operation is to return a direct reference to the object in memory which that iterator is tasked with remembering.



Note: On an unrelated note, in C a 'pointer' is essentially a Random-Access iterator.

This post has been edited by Bench: 16 February 2010 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: return iterators

Posted 16 February 2010 - 06:58 AM

Nice example Bench, as per usual, but is this line needed?

#include <algorithm>

This post has been edited by David W: 16 February 2010 - 06:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: return iterators

Posted 16 February 2010 - 07:37 AM

View PostDavid W, on 16 February 2010 - 01:58 PM, said:

Nice example Bench, as per usual, but is this line needed?

#include <algorithm>

Well, I suppose that depends what you mean by 'needed' :) The example won't compile without it since I used std::find to initialise the iterator, but I assume you're referring to using std::find in the first place - I personally believe its better to use the standard algorithms instead of roll-your-own search etc; iterators mix quite nicely with them.
Was This Post Helpful? 0
  • +
  • -

#6 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: return iterators

Posted 16 February 2010 - 07:59 AM

View PostBench, on 16 February 2010 - 08:37 AM, said:

View PostDavid W, on 16 February 2010 - 01:58 PM, said:

Nice example Bench, as per usual, but is this line needed?

#include <algorithm>

Well, I suppose that depends what you mean by 'needed' :) The example won't compile without it since I used std::find to initialise the iterator, but I assume you're referring to using std::find in the first place - I personally believe its better to use the standard algorithms instead of roll-your-own search etc; iterators mix quite nicely with them.


Excellent answer, as usual, from the Bench :)

Thanks for listing the reasons, especially the need for that include if one uses 'find'.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1