STL向量,迭代器和插入(C ++)

编程入门 行业动态 更新时间:2024-10-22 22:56:26
本文介绍了STL向量,迭代器和插入(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个向量的迭代器传递的方法。 在这个方法中,我想添加一些元素到向量中,但是我不知道这是否可能,当只有迭代器

void GUIComponentText :: AddAttributes(vector< GUIComponentAttribute *> :: iterator begin,vector< GUIComponentAttribute *> :: iterator end) { for > :: iterator i = begin; i!= end; ++ i) { GUIComponentAttribute& attrib = *(* i) //这里是分析的GUIComponentAttribute对象 - 如果出现 //特殊类型的对象,我想向向量中添加一些元素} }

解决方案

首先,你必须改变界面。给定两个迭代器,没有办法返回到它们引用的容器;所以如果你想修改容器,你必须传递一个引用, ie:

void GUIComponentText :: AddAttributes( std :: vector< GUIComponentAttribute *>& attributes) { for(std :: vector< GUIComponentAttribute *> :: iter = attributes.begin(); iter!= attributes.end(); ++ iter) { // ... } $ b b}

完成后:插入可以使迭代器无效。所以它取决于你想插入。如果你想在当前的位置插入: std :: vector<> :: insert 单个元素返回一个迭代器元素,这是插入你的元素之前,所以你可以将它分配给你的迭代器,调整(如果必要),并继续:

code> iter = attributes.insert(iter,newAttribute); ++ iter; //返回我们的位置...

如果您要追加( push_back ),问题有点复杂; 你需要计算偏移量,然后重建迭代器:

size_t offset = iter - attributes.begin ; attributes.push_back(nweAttribute); iter = attributes.begin()+ offset;

在这种情况下,使用 size_t 和 [] ,而不是迭代器。

I have a method to which a vector's iterator is passed. In this method I'd like to add some elements into the vector, but I am not sure whether this is possible when having only the iterator

void GUIComponentText::AddAttributes(vector<GUIComponentAttribute*>::iterator begin, vector<GUIComponentAttribute*>::iterator end) { for (vector<GUIComponentAttribute*>::iterator i = begin; i != end; ++i) { GUIComponentAttribute &attrib = *(*i); // Here are the GUIComponentAttribute objects analyzed - if an object of a // special kind appears, I would like to add some elements to the vector } }

Thanks Markus

解决方案

First, you'll have to change the interface. Given two iterators, there's no way to get back to the container to which they refer; so if you want to modify the container, you'll have to pass a reference to it, i.e.:

void GUIComponentText::AddAttributes( std::vector<GUIComponentAttribute*>& attributes ) { for ( std::vector<GUIComponentAttribute*>::iter = attributes.begin(); iter != attributes.end(); ++ iter ) { // ... } }

Having done that: insertion can invalidate iterators. So it depends on where you want to insert. If you want to insert at the current position: std::vector<>::insert of a single element returns an iterator to that element, which was inserted before your element, so you can assign it to your iterator, adjust (if necessary), and continue:

iter = attributes.insert(iter, newAttribute); ++ iter; // Return to where we were...

If you're appending (push_back), the problem is a bit more complex; you need to calculate the offset, then reconstruct the iterator:

size_t offset = iter - attributes.begin(); attributes.push_back( nweAttribute ); iter = attributes.begin() + offset;

In this case, it is probably simpler to iterate using a size_t and [], rather than an iterator.

更多推荐

STL向量,迭代器和插入(C ++)

本文发布于:2023-07-30 00:30:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   迭代   STL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!