Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL Vector Access

Reply
Thread Tools

STL Vector Access

 
 
Mike Copeland
Guest
Posts: n/a
 
      02-06-2011
I am converting an application element which currently is a fixed
array, sorted for searching by a binary search. Because I don't want to
maintain this process with a fixed array size limitation, I decided to
change it to use of an STL vector.
However, the binary search function is in a subprocess, and I'm
calling it with a parameter that returns the index of the matched
element. The function is a boolean function. Now that I've changed the
processing, I find that I don't know how to access the matched element
(if a match is made), because I'm using an iterator to search the
vector.
Is there a way I can return to the calling code an offset or index to
the matched vector element? TIA
Here's the search code:

bool findTeamId(int *pp) // Find Team Id
{
bool bFound = false;
string strTId = sTId;
strcpy(TeamId, copy(B40, 1, 26)), *pp = -1; // defaults
for(tIter = teamVector.begin(), bFound = false; tIter !=
teamVector.end(); tIter++)
{
if(strTId == tIter->teamCode)
{
// How can I assign pp - or is there another way to access the matched
// element?
return true;
}
}
return bFound;
}

And here is the global data definition:

struct TeamData
{
string teamCode; // Team Code (TId4)
string teamName; // Team's Name
bool isAdded; // Added to working stack of
teams
char teamTypeCode; // Team type Code
int teamMembers1; // Count of Team Members-1
int teamMembers2; // Count of Team Members-2
} ;

typedef vector<TeamData> TeamVector;
typedef TeamVector::iterator TeamIter;
extern TeamVector teamVector;
extern TeamIter tIter;
 
Reply With Quote
 
 
 
 
RaZiel
Guest
Posts: n/a
 
      02-06-2011
On 06.02.2011 02:59, Mike Copeland wrote:
> I am converting an application element which currently is a fixed
> array, sorted for searching by a binary search. Because I don't want to
> maintain this process with a fixed array size limitation, I decided to
> change it to use of an STL vector.
> However, the binary search function is in a subprocess, and I'm
> calling it with a parameter that returns the index of the matched
> element. The function is a boolean function. Now that I've changed the
> processing, I find that I don't know how to access the matched element
> (if a match is made), because I'm using an iterator to search the
> vector.
> Is there a way I can return to the calling code an offset or index to
> the matched vector element? TIA
> Here's the search code:
>
> bool findTeamId(int *pp) // Find Team Id
> {
> bool bFound = false;
> string strTId = sTId;
> strcpy(TeamId, copy(B40, 1, 26)), *pp = -1; // defaults
> for(tIter = teamVector.begin(), bFound = false; tIter !=
> teamVector.end(); tIter++)
> {
> if(strTId == tIter->teamCode)
> {
> // How can I assign pp - or is there another way to access the matched
> // element?
> return true;
> }
> }
> return bFound;
> }
>
> And here is the global data definition:
>
> struct TeamData
> {
> string teamCode; // Team Code (TId4)
> string teamName; // Team's Name
> bool isAdded; // Added to working stack of
> teams
> char teamTypeCode; // Team type Code
> int teamMembers1; // Count of Team Members-1
> int teamMembers2; // Count of Team Members-2
> } ;
>
> typedef vector<TeamData> TeamVector;
> typedef TeamVector::iterator TeamIter;
> extern TeamVector teamVector;
> extern TeamIter tIter;


This is what you are looking for:
http://www.cplusplus.com/reference/s...ator/distance/

A less elegant approach is to introduce a counter and assign it to pp
when you find a match.

- RaZ
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem storing tvmet vector objects in an stl vector container alexjcollins@gmail.com C++ 2 09-08-2008 09:18 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments