C ++容器的通用操作 [英] Generic operations on C++ containers

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

问题描述

如何在C ++ STL容器上写通用操作?例如,Java具有集合界面,每个Java容器(除了地图)实现。我可以做像add,remove,contains和iterations的操作,不管实际的容器是LinkedList,HashSet,ArrayBlockingQueue等等。我发现它非常强大。
C ++有迭代器,但是像add和remove这样的操作呢?
vector有push_back,set有insert,queue有push。

解决方案

迭代:



所有标准容器都有迭代器,它们可以对容器的元素进行有序访问。



插入



所有序列和关联容器可以通过表达式 c.insert(i,x)插入元素 - 其中 c 是序列或关联容器, i c x 是您要添加到 c 的值。



std :: inserter 和朋友可用于以通用方式向序列或关联容器添加元素。



删除:



以下代码适用于任何序列或关联容器:

  while(true){
X :: iterator it(std :: find(c.begin(),c.end(),elem)
if(it == c.end())break;
c.erase(it);
}

其中 X 容器的类型, c 是容器对象, elem 是具有要删除的值的对象



对于序列,有erase-remove idiom,其格式如下:

  c.erase(std :: remove(c.begin(),c.end(),elem),c.end()); 

对于关联容器,您也可以:

  c.erase(k); 

其中 k 要擦除的元素。


$ b

http://www.boost.org/doc/libs/release/libs/range/index.html\">Boost.Range 。



注意 - 这些是可编译时可替换的,而java是运行时可替换的。为了允许运行时替换,有必要使用类型erasure(即 - 创建一个模板化的子类,将所需的接口转发到它被实例化的容器)。


How to write generic operations on C++ STL containers? For example, Java has Collection interface, which every Java containers (except for maps) implements. I can do operations like add, remove, contains, and iterations regardless of whether the actual container is LinkedList, HashSet, ArrayBlockingQueue, etc. I find it very powerful. C++ has iterators, but what about operations like add and remove? vector has push_back, set has insert, queue has push. How to add something to C++ container in a generic way?

解决方案

Iteration:

All standard containers have iterators which give ordered access to the elements of the containers. These can also be used in generic algorithms which work on any conforming iterator type.

Insertion:

All sequences and associative containers can have elements inserted into them by the expression c.insert(i, x) -- where c is a sequence or associative container, i is an iterator into c and x is a value that you want to add to c.

std::inserter and friends can be used to add elements to a sequence or associative container in a generic way.

Removal:

For any sequence or associative container the following code works:

while (true) {
    X::iterator it(std::find(c.begin(), c.end(), elem));
    if (it == c.end()) break;
    c.erase(it);
}

Where X is the type of the container, c is a container object and elem is an object with the value that you want to remove from the container.

For sequences there is the erase-remove idiom, which looks like:

c.erase(std::remove(c.begin(), c.end(), elem), c.end());

For associative containers you can also do:

c.erase(k);

Where k is a key corresponding to the element that you want to erase.

A nice interface to all of this:

See Boost.Range.

Note -- these are compile time substitutable, whereas the java ones are run time substitutable. To allow run-time substitution it is necessary to use type erasure (that is -- make a templated sub-class that forwards the required interface to the container that it is instantiated with).

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

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