Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > nth_element question

Reply
Thread Tools

nth_element question

 
 
bsabiston@gmail.com
Guest
Posts: n/a
 
      04-19-2009
Hi,

I've been trying to figure out why nth_element is going into an
infinite loop. Is it a requirement that all of the elements in the
list be different? Ie, can I call nth_element with a list like
{0,248,248,0,248,248} and expect it to not go into a loop? Debugging
it, it seems to just forever swap the first and last elements of that
array.

Thanks
B

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-19-2009
wrote:
> I've been trying to figure out why nth_element is going into an
> infinite loop. Is it a requirement that all of the elements in the
> list be different? Ie, can I call nth_element with a list like
> {0,248,248,0,248,248} and expect it to not go into a loop? Debugging
> it, it seems to just forever swap the first and last elements of that
> array.


std::nth_element requires random access iterators. You can't specify a
list. And, no, the elements don't have to be unique. Read FAQ 5.8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
bsabiston@gmail.com
Guest
Posts: n/a
 
      04-19-2009
On Apr 18, 8:44*pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> bsabis...@gmail.com wrote:
> > * I've been trying to figure out why nth_element is going into an
> > infinite loop. *Is it a requirement that all of the elements in the
> > list be different? *Ie, can I call nth_element with a list like
> > {0,248,248,0,248,248} and expect it to not go into a loop? *Debugging
> > it, it seems to just forever swap the first and last elements of that
> > array.

>
> std::nth_element requires random access iterators. *You can't specify a
> list. *And, no, the elements don't have to be unique. *Read FAQ 5.8.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


Thank you. But what would I need to do differently? I thought that
you could pass ordinary pointers to functions that expect iterators.
For example, I read this when looking up random-access iterators:

"Because ordinary pointers have the same functionality as random
access iterators, most of the generic algorithms in the C++ Standard
Library can be used with conventional C++ arrays, as well as with the
containers provided by the C++ Standard Library."

Thanks
B
 
Reply With Quote
 
Pavel
Guest
Posts: n/a
 
      04-19-2009
wrote:
> On Apr 18, 8:44 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> bsabis...@gmail.com wrote:
>>> I've been trying to figure out why nth_element is going into an
>>> infinite loop. Is it a requirement that all of the elements in the
>>> list be different? Ie, can I call nth_element with a list like
>>> {0,248,248,0,248,248} and expect it to not go into a loop? Debugging
>>> it, it seems to just forever swap the first and last elements of that
>>> array.

>> std::nth_element requires random access iterators. You can't specify a
>> list. And, no, the elements don't have to be unique. Read FAQ 5.8.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> Thank you. But what would I need to do differently? I thought that
> you could pass ordinary pointers to functions that expect iterators.
> For example, I read this when looking up random-access iterators:
>
> "Because ordinary pointers have the same functionality as random
> access iterators, most of the generic algorithms in the C++ Standard
> Library can be used with conventional C++ arrays, as well as with the
> containers provided by the C++ Standard Library."
>
> Thanks
> B


I am slightly confused: that container of which you are trying to get
the nth element ({0,248 etc) -- is it an array or a list? Maybe just
give us a complete piece of code that does not work?

-Pavel
 
Reply With Quote
 
bsabiston@gmail.com
Guest
Posts: n/a
 
      04-19-2009
On Apr 18, 11:37*pm, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
wrote:
> bsabis...@gmail.com wrote:
> > On Apr 18, 8:44 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> >> bsabis...@gmail.com wrote:
> >>> * I've been trying to figure out why nth_element is going into an
> >>> infinite loop. *Is it a requirement that all of the elements in the
> >>> list be different? *Ie, can I call nth_element with a list like
> >>> {0,248,248,0,248,248} and expect it to not go into a loop? *Debugging
> >>> it, it seems to just forever swap the first and last elements of that
> >>> array.
> >> std::nth_element requires random access iterators. *You can't specify a
> >> list. *And, no, the elements don't have to be unique. *Read FAQ 5.8.

>
> >> V
> >> --
> >> Please remove capital 'A's when replying by e-mail
> >> I do not respond to top-posted replies, please don't ask

>
> > Thank you. *But what would I need to do differently? *I thought that
> > you could pass ordinary pointers to functions that expect iterators.
> > For example, I read this when looking up random-access iterators:

>
> > "Because ordinary pointers have the same functionality as random
> > access iterators, most of the generic algorithms in the C++ Standard
> > Library can be used with conventional C++ arrays, as well as with the
> > containers provided by the C++ Standard Library."

>
> > Thanks
> > B

>
> I am slightly confused: that container of which you are trying to get
> the nth element ({0,248 etc) -- is it an array or a list? Maybe just
> give us a complete piece of code that does not work?
>
> -Pavel


It's a pointer to a memory location where I have stored a bunch of
structs. I'm not at work right now, but I was basically trying to get
this guy's code to work from this article:

http://en.literateprograms.org/Media...m_(C_Plus_Plus)

You can see that he passes in a regular pointer to his code which uses
the pointers as iterators. But when I tried it, I get infinite loops
out of the nth_element routine. Is it dependent on the compiler?
Because this is for the NintendoDS and it's pretty limited.

Is there any way to actually use 'real' iterators (I still haven't
wrapped my head around what those really are) and somehow place them
in this pre-allocated memory space that I need to use? There isn't
enough memory for me to go allocating a bunch of memory for the
iterators.

Thanks
B


 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-20-2009
wrote:
> On Apr 18, 11:37 pm, Pavel <dot_com_yahoo@paultolk_reverse.yourself>
> wrote:
>> bsabis...@gmail.com wrote:
>>> On Apr 18, 8:44 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>>>> bsabis...@gmail.com wrote:
>>>>> I've been trying to figure out why nth_element is going into an
>>>>> infinite loop. Is it a requirement that all of the elements in the
>>>>> list be different? Ie, can I call nth_element with a list like
>>>>> {0,248,248,0,248,248} and expect it to not go into a loop? Debugging
>>>>> it, it seems to just forever swap the first and last elements of that
>>>>> array.
>>>> std::nth_element requires random access iterators. You can't specify a
>>>> list. And, no, the elements don't have to be unique. Read FAQ 5.8.

[snip signatures]
>> I am slightly confused: that container of which you are trying to get
>> the nth element ({0,248 etc) -- is it an array or a list? Maybe just
>> give us a complete piece of code that does not work?
>>
>> -Pavel

>
> It's a pointer to a memory location where I have stored a bunch of
> structs. I'm not at work right now, but I was basically trying to get
> this guy's code to work from this article:
>
> http://en.literateprograms.org/Media...m_(C_Plus_Plus)
>
> You can see that he passes in a regular pointer to his code which uses
> the pointers as iterators.


The pointers are treated as random access iterator. This is adapted
through iterator_traits<>.

> But when I tried it, I get infinite loops
> out of the nth_element routine. Is it dependent on the compiler?
> Because this is for the NintendoDS and it's pretty limited.


I have seen nothing wrong in how he uses std::nth_element(). I guess the
bug is in your code.


> Is there any way to actually use 'real' iterators (I still haven't
> wrapped my head around what those really are)


Pointers are fine.

> and somehow place them
> in this pre-allocated memory space that I need to use? There isn't
> enough memory for me to go allocating a bunch of memory for the
> iterators.


Iterators usually live on the stack. You need to know that STL may
allocate some others. I expect your STL is adapted to your environment.

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nth_element + sort versus partial_sort Scott Meyers C++ 0 03-17-2011 03:22 AM
nth_element() not compatible in VS2005 zhouchengly@gmail.com C++ 7 11-07-2006 05:50 PM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57