Stange Occurance After Using std::Merge On std::vector's

Discussion in 'C++' started by Adam Teasdale Hartshorne, Aug 11, 2005.

  1. Using the code below I get a very stange result. I assume that I must be
    doing something wrong, but I can't figure out what it is.


    getTriangleEdges(t[0],t1e) ;

    for (int i = 0 ; i < (int)t1e.size() ; i++) {
    printf("T1 Edges Index:- %i \n", t1e) ;

    }

    getTriangleEdges(t[1],t2e) ;

    for (int i = 0 ; i < (int)t1e.size() ; i++) {
    printf("T2 Edges Index:- %i \n", t2e) ;
    }

    std::vector<int> edges ;
    edges.reserve(6);

    //printf("Merging lists \n") ;
    std::merge(t1e.begin(), t1e.end(), t2e.begin(), t2e.end(), edges.begin());

    printf("Edges Size:- %i \n", (int) edges.size()) ;

    for (int i = 0 ; i < 6 ; i++) {
    printf("Edges Index:- %i \n", edges) ;
    }


    The following output is seen in the terminal

    T1 Edges Index:- 7
    T1 Edges Index:- 8
    T1 Edges Index:- 6
    T2 Edges Index:- 10
    T2 Edges Index:- 8
    T2 Edges Index:- 9
    Edges Size:- 0
    Edges Index:- 7
    Edges Index:- 8
    Edges Index:- 6
    Edges Index:- 10
    Edges Index:- 8
    Edges Index:- 9

    It seems to think the size of edges is 0!!!!!

    Any thoughts would be much appreciated,

    Adam
     
    Adam Teasdale Hartshorne, Aug 11, 2005
    #1
    1. Advertising

  2. Adam Teasdale Hartshorne wrote:

    > Using the code below I get a very stange result. I assume that I must be
    > doing something wrong, but I can't figure out what it is.


    No offense, but there are so many problems with your code, it's hard to
    know where to begin.

    > getTriangleEdges(t[0],t1e) ;
    >
    > for (int i = 0 ; i < (int)t1e.size() ; i++) {


    C-style casts like this are a bad habit, particularly in a situation
    like this where there's no reason to include a cast at all.

    > printf("T1 Edges Index:- %i \n", t1e) ;
    >
    > }
    >
    > getTriangleEdges(t[1],t2e) ;
    >
    > for (int i = 0 ; i < (int)t1e.size() ; i++) {


    C-style cast again.

    > printf("T2 Edges Index:- %i \n", t2e) ;
    > }
    >
    > std::vector<int> edges ;
    > edges.reserve(6);
    >
    > //printf("Merging lists \n") ;
    > std::merge(t1e.begin(), t1e.end(), t2e.begin(), t2e.end(), edges.begin());


    That last parameter needs to be std::back_inserter(edges), not
    edges.begin(). That's the error that resulted in edges.size()
    returning the wrong result.

    But there's a more fundamental problem. Merge requires that the two
    constituent ranges both already be sorted (and in ascending order
    unless you tell it otherwise). Your two ranges are not sorted at all.
    Accordingly, the result of calling merge as you do here is undefined.

    > printf("Edges Size:- %i \n", (int) edges.size()) ;


    Another C-style cast. edges.size() returns a std::size_t, which
    generally means an unsigned int. Just change your printf accordingly -
    or better yet, use std::cout - and get rid of the cast.

    > for (int i = 0 ; i < 6 ; i++) {
    > printf("Edges Index:- %i \n", edges) ;
    > }
    >
    >
    > The following output is seen in the terminal
    >
    > T1 Edges Index:- 7
    > T1 Edges Index:- 8
    > T1 Edges Index:- 6
    > T2 Edges Index:- 10
    > T2 Edges Index:- 8
    > T2 Edges Index:- 9


    Like I said, two unsorted ranges, so undefined behavior when you call
    std::merge

    > Edges Size:- 0
    > Edges Index:- 7
    > Edges Index:- 8
    > Edges Index:- 6
    > Edges Index:- 10
    > Edges Index:- 8
    > Edges Index:- 9
    >
    > It seems to think the size of edges is 0!!!!!


    Because you mistakenly told it to send the result to edges.begin()
    instead of std::back_inserter(edges).

    Hope that helps.

    Best regards,

    Tom
     
    Thomas Tutone, Aug 11, 2005
    #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. Anonymous
    Replies:
    20
    Views:
    4,310
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    731
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,934
    Csaba
    Feb 18, 2006
  4. Rune Allnor
    Replies:
    4
    Views:
    955
    Rune Allnor
    Dec 11, 2008
  5. Replies:
    2
    Views:
    81
Loading...

Share This Page