通用STL容器的函数 [英] A function for a generic STL container

查看:79
本文介绍了通用STL容器的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想编写一个函数来删除

范围内的重复元素。应该如何成为原型?


模板< class Iterator>

void eraseRepeated(Iterator begin,Iterator end);


不起作用,因为我需要容器写:container.erase(p),

其中p是迭代器。

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(Iterator begin, Iterator end);

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

推荐答案

* Nafai:
您好我想编写一个删除
范围内重复元素的函数。应该如何成为原型?

模板< class Iterator>
void eraseRepeated(Iterator begin,Iterator end);

因为我需要而无法工作要写的容器:container.erase(p),
其中p是迭代器。
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(Iterator begin, Iterator end);

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




查看std :: unique。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?



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?


Alf P.Steinbachescribió:
Alf P. Steinbach escribió:
* Nafai:
* Nafai:
您好我想写一个函数来删除
范围。应该如何成为原型?

模板< class Iterator>
void eraseRepeated(Iterator begin,Iterator end);

因为我需要而无法工作要写的容器:container.erase(p),
其中p是迭代器。
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(Iterator begin, Iterator end);

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



查看std :: unique。


Check out std::unique.




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.




Nafai写道:

Nafai wrote:
Alf P.Steinbachescribió:
Alf P. Steinbach escribió:
* Nafai:
* Nafai:
嗨我想编写一个删除重复的函数元素
在一个范围内。应该如何成为原型?

模板< class Iterator>
void eraseRepeated(Iterator begin,Iterator end);

因为我需要而无法工作要写的容器:
container.erase(p),其中p是迭代器。

查看std :: unique。
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(Iterator 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.



不存在这样的函数,因为不同的容器有不同的

"擦除"语义。例如,擦除顺序

迭代器的算法可能适用于一个容器,但可能会产生未定义的行为

。你可以做的最好是复制范围,删除

范围,然后从

复制的数据中添加回来的唯一(或非重复)元素,但是这个是非常低效的。 / david


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


这篇关于通用STL容器的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆