Retrieving the prior to the last element of a std::vector of struct ???

Discussion in 'C++' started by Peter Olcott, Apr 15, 2007.

  1. Peter Olcott

    Peter Olcott Guest

    What is the correct syntax for the last line???

    #include <vector>
    #include <stdio.h>

    struct TestType {
    double ONE;
    double TWO;
    };

    void main()
    {
    TestType Record;
    std::vector<TestType> Test;

    for (double N = 111.0; N <= 122.0; N++) {
    Test.push_back(Record);
    Test.back().ONE = N;
    Test.back().TWO = N / 3.0;
    }
    for (int M = 0; M < Test.size(); M++)
    printf("Test[%02d].ONE--->%6.2f
    Test[%02d].TWO--->%6.2f\n",
    M, Test[M].ONE, M, Test[M].TWO);

    printf("Test.back().ONE--->%6.2f
    Test.back().TWO--->%6.2f \n",
    Test.back().ONE, Test.back().TWO);

    // printf("Test.back().ONE--->%6.2f\n",
    (Test.back() -1).ONE);
    }
     
    Peter Olcott, Apr 15, 2007
    #1
    1. Advertising

  2. Peter Olcott

    Daniel T. Guest

    "Peter Olcott" <> wrote:

    > What is the correct syntax for the last line???


    > // printf("Test.back().ONE--->%6.2f\n", (Test.back() -1).ONE);


    printf("Test.back().ONE--->%6.2f\n", (Test.end() - 2)->ONE);
     
    Daniel T., Apr 15, 2007
    #2
    1. Advertising

  3. Peter Olcott

    Ron Natalie Guest

    Re: Retrieving the prior to the last element of a std::vector ofstruct ???


    >
    > void main()

    int main()


    >
    > // printf("Test.back().ONE--->%6.2f\n",
    > (Test.back() -1).ONE);
    > }
    >
    >


    back() returns a reference to last object, you can't
    do -1 to it (you're substracting from an the object
    stored in the last position).

    back() is equivelent to *(--end())
    your second item should be
    *(Test.end() -2)
    This will work on any container that has random
    access iterators (like vector)... alternatively
    you can do
    *(--(--end()))
    which will work on bidirectional iterators (like list).


    Of course you need to assure that there are at least
    two objects exist in the container obviously.
     
    Ron Natalie, Apr 15, 2007
    #3
  4. Peter Olcott

    dasjotre Guest

    On 15 Apr, 19:22, Ron Natalie <> wrote:
    >
    > back() is equivelent to *(--end())
    > your second item should be
    > *(Test.end() -2)
    > This will work on any container that has random
    > access iterators (like vector)... alternatively
    > you can do
    > *(--(--end()))
    > which will work on bidirectional iterators (like list).


    Also, *(rbegin()+1) where iterators are random
    access, or *(++rbegin()) where they are not.

    > Of course you need to assure that there are at least
    > two objects exist in the container obviously.
     
    dasjotre, Apr 16, 2007
    #4
    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. Anonymous
    Replies:
    20
    Views:
    4,438
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    770
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    2,007
    Csaba
    Feb 18, 2006
  4. Rune Allnor
    Replies:
    4
    Views:
    995
    Rune Allnor
    Dec 11, 2008
  5. Simon
    Replies:
    2
    Views:
    510
    Simon
    May 31, 2009
Loading...

Share This Page