473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A function for a generic STL container

Hi I want to write a function which erases al the repeated elements in a
range. How should be the prototype?

template <class Iterator>
void eraseRepeated(I terator begin, Iterator end);

doesn't work because I need the container to write: container.erase (p),
where p is an iterator.
Jul 23 '05 #1
19 2257
* Nafai:
Hi I want to write a function which erases al the repeated elements in a
range. How should be the prototype?

template <class Iterator>
void eraseRepeated(I terator begin, Iterator end);

doesn't work because I need the container to write: container.erase (p),
where p is an iterator.


Check out std::unique.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach escribió:
* Nafai:
Hi I want to write a function which erases al the repeated elements in a
range. How should be the prototype?

template <class Iterator>
void eraseRepeated(I terator begin, Iterator end);

doesn't work because I need the container to write: container.erase (p),
where p is an iterator.

Check out std::unique.


Unique only erases consecutive elements. I want to erase all of them.
And I don't want to sort them.

But above all I would like to know how to program a function like that,
which can get any container and do something with its iterators.
Jul 23 '05 #3

Nafai wrote:
Alf P. Steinbach escribió:
* Nafai:
Hi I want to write a function which erases al the repeated elements in a range. How should be the prototype?

template <class Iterator>
void eraseRepeated(I terator begin, Iterator end);

doesn't work because I need the container to write: container.erase (p),where p is an iterator.

Check out std::unique.


Unique only erases consecutive elements. I want to erase all of them.

And I don't want to sort them.

But above all I would like to know how to program a function like that, which can get any container and do something with its iterators.

No such function exists because different containers have different
"erase" semantics. For example, an algorithm that erases sequential
iterators may work for one container, but may yield undefined behavior
for another. The best you can do is to copy the range, remove the
range, and then add back unique (or non-repeated) elements from the
copied data, but this is very inefficient. /david

Jul 23 '05 #4
* Nafai:
Alf P. Steinbach escribió:
* Nafai:
Hi I want to write a function which erases al the repeated elements in a
range. How should be the prototype?

template <class Iterator>
void eraseRepeated(I terator begin, Iterator end);

doesn't work because I need the container to write: container.erase (p),
where p is an iterator.

Check out std::unique.


Unique only erases consecutive elements. I want to erase all of them.
And I don't want to sort them.


I don't think you understand the requirements.

Some way of identifying an element as duplicate is needed.

If not consecutive, and no sorting, you'll need to register elements in
a hash table or something like that.

But above all I would like to know how to program a function like that,
which can get any container and do something with its iterators.


See std::unique.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #5
Alf P. Steinbach wrote:

Some way of identifying an element as duplicate is needed.


A and B are duplicate if and only if A==B.
Jul 23 '05 #6
Alf P. Steinbach wrote:
* Nafai:
Alf P. Steinbach escribió:
> * Nafai:
>
>>Hi I want to write a function which erases al the repeated elements in
>>a
>> range. How should be the prototype?
>>
>>template <class Iterator>
>>void eraseRepeated(I terator begin, Iterator end);
>>
>>doesn't work because I need the container to write: container.erase (p),
>>where p is an iterator.
>
>
> Check out std::unique.
>


Unique only erases consecutive elements. I want to erase all of them.
And I don't want to sort them.


I don't think you understand the requirements.

Some way of identifying an element as duplicate is needed.

If not consecutive, and no sorting, you'll need to register elements in
a hash table or something like that.

But above all I would like to know how to program a function like that,
which can get any container and do something with its iterators.


See std::unique.


Let's see. It's quite simple. I have a collection C (ie. list or vector):
C=(1 4 4 2 1 1 4) and I get: C'=(1 4 2)

The algorithm is just this:

insert all elements of C into a set.

iterate through C and check if each element is in the set.
If it is, erase it from the set.
If it isn't erase it from C.

Or even more simpler (and suitable for any collection):

....
If it is, erase it from the set and insert it into C'.
If it isn't do nothing.
I just need to know how to declare the prototype!! ("template". ..)

Jul 23 '05 #7
Nafai wrote:
Alf P. Steinbach wrote:
* Nafai:
Alf P. Steinbach escribió:
> * Nafai:
>
>>Hi I want to write a function which erases al the repeated elements in >>a
>> range. How should be the prototype?
>>
>>template <class Iterator>
>>void eraseRepeated(I terator begin, Iterator end);
>>
>>doesn't work because I need the container to write: container.erase (p), >>where p is an iterator.
>
>
> Check out std::unique.
>

Unique only erases consecutive elements. I want to erase all of them. And I don't want to sort them.


I don't think you understand the requirements.

Some way of identifying an element as duplicate is needed.

If not consecutive, and no sorting, you'll need to register elements in a hash table or something like that.

But above all I would like to know how to program a function like that, which can get any container and do something with its iterators.


See std::unique.


Let's see. It's quite simple. I have a collection C (ie. list or
vector):
C=(1 4 4 2 1 1 4) and I get: C'=(1 4 2)

The algorithm is just this:

insert all elements of C into a set.

iterate through C and check if each element is in the set.
If it is, erase it from the set.
If it isn't erase it from C.

Or even more simpler (and suitable for any collection):

...
If it is, erase it from the set and insert it into C'.
If it isn't do nothing.
I just need to know how to declare the prototype!! ("template". ..)


Does this suit you?

template <class T>
void eraseRepeated(T &container);

Kristo

Jul 23 '05 #8
Kristo escribió:
Nafai wrote:
Alf P. Steinbach wrote:

* Nafai:

Alf P. Steinbach escribió:

>* Nafai:
>
>
>>Hi I want to write a function which erases al the repeated
elements in
a
>> range. How should be the prototype?
>>
>>templat e <class Iterator>
>>void eraseRepeated(I terator begin, Iterator end);
>>
>>doesn't work because I need the container to write:
container.erase (p),
where p is an iterator.
>
>
>Check out std::unique.
>

Unique only erases consecutive elements. I want to erase all of
them.
And I don't want to sort them.

I don't think you understand the requirements.

Some way of identifying an element as duplicate is needed.

If not consecutive, and no sorting, you'll need to register
elements in
a hash table or something like that.

But above all I would like to know how to program a function like
that,
which can get any container and do something with its iterators.

See std::unique.


Let's see. It's quite simple. I have a collection C (ie. list or
vector):
C=(1 4 4 2 1 1 4) and I get: C'=(1 4 2)

The algorithm is just this:

insert all elements of C into a set.

iterate through C and check if each element is in the set.
If it is, erase it from the set.
If it isn't erase it from C.

Or even more simpler (and suitable for any collection):

...
If it is, erase it from the set and insert it into C'.
If it isn't do nothing.
I just need to know how to declare the prototype!! ("template". ..)

Does this suit you?

template <class T>
void eraseRepeated(T &container);

Kristo


No, it doesn't. This doesn't work:

template <class T>
void f(T& Container)
{
T::iterator it; // <- parse error before token ';'
it = Container.begin ();
....
}
Jul 23 '05 #9
Nafai wrote:
Kristo escribió:
Nafai wrote:

Let's see. It's quite simple. I have a collection C (ie. list or
vector):
C=(1 4 4 2 1 1 4) and I get: C'=(1 4 2)

The algorithm is just this:

insert all elements of C into a set.

iterate through C and check if each element is in the set.
If it is, erase it from the set.
If it isn't erase it from C.

Or even more simpler (and suitable for any collection):

...
If it is, erase it from the set and insert it into C'.
If it isn't do nothing.
I just need to know how to declare the prototype!! ("template". ..)

Does this suit you?

template <class T>
void eraseRepeated(T &container);

Kristo


No, it doesn't. This doesn't work:

template <class T>
void f(T& Container)
{
T::iterator it; // <- parse error before token ';'
it = Container.begin ();
...
}


That's because you forgot the typename keyword. The compiler has no
other way of knowing that T::iterator is a type.

typename T::iterator it;

Kristo

Jul 23 '05 #10

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

Similar topics

1
1612
by: Stewart Rogers | last post by:
Hi all, I have been working on an ASP.NET application that is a kind of wizard ( a list of sequential pages ). We built that application for the CLIENT-A and it worked fine. After six months CLIENT-B came along and and requested the same application but with little bit different requirements (both in terms of the front-end and back-end processing), so we had to create a new application for that client. Now came CLIENT-C !!!!!! I am...
2
1895
by: Luc Claustres | last post by:
I have a generic container such as: template<class T> class Container { // some data structure that store elements of type T } I use this container in a hierarchical manner, that is elements can be themselves containers:
1
2506
by: Anton Pervukhin | last post by:
Hi everybody! While trying to implement a generic sorting function which takes a member function(on which base the actual sort happens) as a parameter, I have met the problem that I need to use the result type of the member function and as I have understood there is no way in standart stl to extract it. While searching through this groop and in general, I could found some references to the problem in the following topic : ...
7
1154
by: Alfonso Morra | last post by:
I have written a class, and several of it's methods are function templates. When I compiled the code, I realized that I had made some typos in the code - but the compiler seemed to skip right over these obvious errors. I gre suspicious and enterred XXX in one of the function templates - and the compiler succesfully compile the code (obviously completely skipped my templated functions). I am using VC 7.1 Is this a "bug", (highly unlikely,...
4
1823
by: Anders | last post by:
Hi, I was wondering what is most efficient of the two. Is it more efficient to add server controls within the Itemtemplate and use OnItemDataBound to manipulate and databind the servercontrols. Or is better to send the DataItems via. a method/function from within the Itemtemplate (<%# call function %>) Id love to hear your thoughts. Anders
4
4937
by: Mitchel Haas | last post by:
Hello, Feeling a need for a generic tree container to supplement the available containers in the STL, I've created a generic tree container library (TCL). The library usage is very much like the containers in the STL, including iterator usage. Info on the library can be found here.. http://www.visuallandlord.com/developers_corner/open_source/tree_container_library/overview.php The library and sample code can be downloaded from this...
18
3498
by: Robbie Hatley | last post by:
A couple of days ago I dedecided to force myself to really learn exactly what "strtok" does, and how to use it. I figured I'd just look it up in some book and that would be that. I figured wrong! Firstly, Bjarne Stroustrup's "The C++ Programming Language" said: (nothing)
4
1730
by: Terence Wilson | last post by:
I'm having trouble designing a container for all the basic types(char, int, float, double). The container should be able to hold contiguous arrays of type T. I would like some kind of generic iterator for accessing the data that is type independent in order that I could write generic algorithms. Basically I need something like std::vector that does not require a compile type. I could use some kind of Variant for each element, but the...
6
5932
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9077
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.