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

  • Thread starter Adam Teasdale Hartshorne
  • Start date
A

Adam Teasdale Hartshorne

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
 
T

Thomas Tutone

Adam said:
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
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top