473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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[i]) ;

}

getTriangleEdges(t[1],t2e) ;

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

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[i]) ;
}
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
Aug 11 '05 #1
1 2657
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[i]) ;

}

getTriangleEdges(t[1],t2e) ;

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

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[i]) ;
}
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

Aug 11 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: bartek d | last post by:
Hello, I have a class which is used to encapsulate a RenderMan Interface variable. Generally speaking, such variable may be of integral, float, string type, or an array of those. I thought I...
8
by: Christian Stigen Larsen | last post by:
Consider the following: class parent { public: virtual void print() { printf("Parent\n"); } }; class child : public parent {
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
1
by: Daniel J Watkins | last post by:
Hi, When I attempt to resize a column in a 2d vector I receive the error listed below. I'm using the libraries supplied with VxWorks. 0xf3fe450 (tExcTask): memPartFree: invalid block...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
2
by: Buck Brown | last post by:
Hi, I am using Visual C++ 6.0 MFC. I have been trying to build an array of objects. First I tried using CArray but it was just giving me fits. Once I got my copy contructor built so I would not...
21
by: Peter Olcott | last post by:
I got the previous alias to std::vector working, and found that it takes up the space of a pointer. I want to find a way to do an alias to a std::vector that does not take up any space. Is there...
5
MrPickle
by: MrPickle | last post by:
I googled and got some answers but whenever I used it I kept getting and std::out_of_range exception and I'm not completely clear on how to use it. I know how to initialize it; std::vector<...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.