STL Vector Access

Discussion in 'C++' started by Mike Copeland, Feb 6, 2011.

  1. 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;
    Mike Copeland, Feb 6, 2011
    #1
    1. Advertising

  2. Mike Copeland

    RaZiel Guest

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

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

    - RaZ
    RaZiel, Feb 6, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis
    Replies:
    1
    Views:
    2,567
    Dennis
    Jun 7, 2004
  2. CD
    Replies:
    2
    Views:
    795
    Victor Bazarov
    Oct 5, 2004
  3. Replies:
    2
    Views:
    405
  4. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    333
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page