There are some operations like sort, remove, remove_if which are
available as list<Tmember functions; but vector<T>, deque<Tdo not
seem have those functions. If am correct in this, what is the reason
for that. Moreover sort, remove, remove_if are available in
<algorithmalso. Then why does list<Thave these member operations ?
Kindly clarify.
Thanks
V.Subramanian 3 1468
On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
There are some operations like sort, remove, remove_if which are
available as list<Tmember functions; but vector<T>, deque<Tdo not
seem have those functions. If am correct in this, what is the reason
for that. Moreover sort, remove, remove_if are available in
<algorithmalso. Then why does list<Thave these member operations ?
The algorithms in <algorithmoperate on ranges, not containers.
Therefore, an algorithm like std::remove_if will not actually remove
any elements, just copy the elements not removed to the front of the
range, and return a new end iterator.
The member functions of list naturally operate on the list itself.
Therefore list<T>::remove and list<T>::remove_if will actually erase
the elements from the list.
In general using the member functions where present will be faster,
and they often do slightly different things.
As for std::sort, this algorithm requires random access iterators,
while list only has bidirectional iterators. Therefore it needs its
own sort function.
Triple-DES wrote:
On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
>There are some operations like sort, remove, remove_if which are available as list<Tmember functions; but vector<T>, deque<Tdo not seem have those functions. If am correct in this, what is the reason for that. Moreover sort, remove, remove_if are available in <algorithmalso. Then why does list<Thave these member operations ?
As for std::sort, this algorithm requires random access iterators,
while list only has bidirectional iterators. Therefore it needs its
own sort function.
Does anyone know why this is not done with a specialisation of the sort
algorithm?
The same applies to find, i.e.
map m;
vector v;
m.find(...); // O(log N)
find(v.begin(),v.end(),...); // O(N)
find(m.begin(),m.end(),...); // O(N)
// but could be O(log N) if specialised?
Phil.
On 6 Feb, 12:23, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
Triple-DES wrote:
On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
There are some operations like sort, remove, remove_if which are
available as list<Tmember functions; but vector<T>, deque<Tdo not
seem have those functions. If am correct in this, what is the reason
for that. Moreover sort, remove, remove_if are available in
<algorithmalso. Then why does list<Thave these member operations ?
As for std::sort, this algorithm requires random access iterators,
while list only has bidirectional iterators. Therefore it needs its
own sort function.
Does anyone know why this is not done with a specialisation of the sort
algorithm?
It may not be impossible to do it, but I for one can't think of a good
way to implement it. The naive approach
template<typename T>
void sort(typename std::list<T>::iterator first, typename
std::list<T>::iterator last)
doesn't work because the nested type can not be deduced (14.8.2.4/4).
Obtaining the actual container from just the two iterators could also
be a problem. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
Hello all,
After perusing the Standard, I believe it is true to say that once you
insert an element into a std::list<>, its location in memory never changes.
This makes a std::list<> ideal for...
|
by: matty.hall |
last post by:
I have two classes: a base class (BaseClass) and a class deriving from
it (DerivedClass). I have a List<DerivedClass> that for various
reasons needs to be of that type, and not a List<BaseClass>....
|
by: Eric |
last post by:
I have a string representation of an object. I create an object of that type
through reflection. I would like to create a List<> of those objects. I
obviously can't do
List<myObject.GetType()>...
|
by: Varangian |
last post by:
Hello, there
I have a problem with regards to System.Collections.Generic.List<T>
I need to pass a class with implements an interface - TestClass :
IPerson
I put this class in a...
|
by: Iron Moped |
last post by:
I'm airing frustration here, but why does LinkedList<not support the
same sort and search methods as List<>? I want a container that does not
support random access, allows forward and reverse...
|
by: Andrew Robinson |
last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
1. Is there any way to convert from a dictionary to a list...
|
by: Zytan |
last post by:
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this' because it is
not a variable"
and I have no idea why! Do lists return copies...
|
by: Peted |
last post by:
I have the following code
public enum pdfFlags
{
PFD_DRAW_TO_WINDOW,
PFD_DRAW_TO_BITMAP,
PFD_SUPPORT_GDI,
PFD_SUPPORT_OPENGL,
PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
|
by: Lee Crabtree |
last post by:
This seems inconsistent and more than a little bizarre.
Array.Clear sets all elements of the array to their default values (0,
null, whatever), whereas List<>.Clear removes all items from the...
|
by: =?Utf-8?B?SkI=?= |
last post by:
Hello
List<Tis said to be more powerful than ArrayLists but if you have
something like this:
List<intmylst = new List<>;
myList.Add("Joe");
myList.Add(25);
the list doesn't seem to...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
| |