GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jul-2009, 07:01
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

Problem about the return value type of generic algorithm find().


Hi,
I know the return value of generic search algorithm "find()" is of iterator type. Now how can I output the return value as numeric type? For example:
CPP / C++ / C Code:
short myArray[4] = {2,1,3,7};
vector<short>mVec(myArray,myArray+4);
vector<short>::iterator found;
found = find(mVec.begin(),mVec.end(),2);
In this code the variable found will be assigned the "mVec.begin", but can I obtain its numeric counter part,say, 0 instead? How? Thank u in advance.
  #2  
Old 09-Jul-2009, 07:12
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Problem about the return value type of generic algorithm find().


Are you looking to get the index at which the element was found? Or are you looking to get the value of an iterator? I'll assume the former since by virtue of having the iterator and search term you already have all the ingredients for the latter.
I dont know that there is a direct way to do that with the iterator without keeping an index and comparing each element in the vector. If that is the case, you may be better off just iterating over the vector by hand anyway.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #3  
Old 09-Jul-2009, 07:21
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Problem about the return value type of generic algorithm find().


You can dereference the returned iterator:

CPP / C++ / C Code:
std::cout << *found << std::endl;

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #4  
Old 09-Jul-2009, 07:28
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,031
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Problem about the return value type of generic algorithm find().


Quote:
Originally Posted by L7Sqr
Are you looking to get the index at which the element was found? Or are you looking to get the value of an iterator? I'll assume the former since by virtue of having the iterator and search term you already have all the ingredients for the latter.
I dont know that there is a direct way to do that with the iterator without keeping an index and comparing each element in the vector. If that is the case, you may be better off just iterating over the vector by hand anyway.

After reading your post I realize the OP was probably asking about the numeric index.
You can subtract iterators in a vector:

CPP / C++ / C Code:
std::cout << "Found 2 at index : " << found - mVec.begin() << std::endl;

Of course, the proper checks should be made (std::find will return end if it can't find the item).

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
"Linker Error" problem with C++ The Great Dane C++ Forum 3 29-Jan-2010 04:29
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
yet another linked list program aijazbaig1 C Programming Language 15 11-Sep-2007 13:18
Re: Things to Avoid in C/C++ -- gets() , Part 1 WaltP C Programming Language 5 21-Jun-2007 12:13
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:31.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.