STL Vector Access

M

Mike Copeland

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;
 
R

RaZiel

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/std/iterator/distance/

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

- RaZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Alternative STL Structure? 10
What Am I Doing Wrong? 7
Modify STL multiset 2
STL Container Choice 11
Building a Large Container 26
Sorting Structure Data within Vector 1
Sorting an STL map 1
Modify STL map Object 4

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top