473,396 Members | 1,866 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,396 software developers and data experts.

std::sort question

I came across code which contains:

std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end());

with newAnswer defined as

std::vector<unsigned char>& newAnswer

However upon attempting to compile the above code I got the following
errors (I am using g++ ver 3.3.1 on Cygwin)
error: cannot convert `
__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char,
std::allocator<unsigned char> > >' to `unsigned char*' for argument
`1' to `
void std::sort(_RandomAccessIter, _RandomAccessIter) [with
_RandomAccessIter
= unsigned char*]'
My questions are:

1. Why do we need to specify <unsigned char*> when calling sort? I have
seen many exmaples where std::sort is called as

std::vector<int> v;
//populate v
std::sort(v.begin(),v.end())

2. What does the above error message mean? How to resolve this problem?

Thanks.
Jul 22 '05 #1
1 2328
Ravi wrote:
I came across code which contains:

std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end());

with newAnswer defined as

std::vector<unsigned char>& newAnswer

However upon attempting to compile the above code I got the following
errors (I am using g++ ver 3.3.1 on Cygwin)
error: cannot convert `
__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned
char, std::allocator<unsigned char> > >' to `unsigned char*' for
argument
`1' to `
void std::sort(_RandomAccessIter, _RandomAccessIter) [with
_RandomAccessIter
= unsigned char*]'
My questions are:

1. Why do we need to specify <unsigned char*> when calling sort?
You don't need to, and actually shouldn't do it.
I have seen many exmaples where std::sort is called as

std::vector<int> v;
//populate v
std::sort(v.begin(),v.end())

2. What does the above error message mean?
It's just because you specified <unsigned char*>. The template parameter
to std::sort is supposed to be an iterator, but you forced it to be a
pointer. For arrays, a pointer would be correct, and on some platforms,
also for vectors, but that's not guaranteed, and actually isn't the
case in g++'s standard library implementation.
How to resolve this problem?


Remove the <unsigned char*>.

Jul 22 '05 #2

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

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
8
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
15
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
11
by: Jeff Schwab | last post by:
Would std::sort ever compare an object with itself? I'm not talking about two distinct, equal-valued objects, but rather this == &that. The container being sorted is a std::vector. I've never...
1
by: Markus Dehmann | last post by:
In the following code example, I define several Comparator classes which contain different compare functions to use with std::sort. I have a Sorter class that gets passed a Comparator and is...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.