函数返回向量的迭代器

时间:2022-06-03 23:16:04

I have this new class Seq that inherits vector and has some additional features. I can use all methods of vector with Seq.

我有这个继承vector的新类Seq并具有一些额外的功能。我可以使用Seq的所有矢量方法。

Having this data structure:

拥有这样的数据结构:

Seq< vector<int> > sweepEvents;

I want to have a function that takes an element vector edge search for it in sweepEvents and returns the iterator to the position of the found element in sweepEvents (if and only if edge is found) and the iterator to the last elements of the vector (if and only if edge is not found).

我希望有一个函数在sweepEvents中对它进行元素向量边搜索,并将迭代器返回到sweepEvents中找到元素的位置(当且仅当找到edge时)和迭代器到向量的最后元素(当且仅当没有找到边缘时)。

Then I want to work with this iterator, in that I want to compare the elements from the prev and the next position of iterator.

然后我想使用这个迭代器,因为我想比较迭代器的prev和下一个位置的元素。

I have the following function for founding and returning the iterator:

我有以下函数用于创建和返回迭代器:

Seq< vector<int> >::iterator QSweep::insertSweepEvents(edge_t edge,int currentDim){
    int changePosition;
    int found=0;

    for (int i=0;i<currentDim;i++){
         if (edge[0]==sweepEvents[i][1]){
             changePosition=i;
             found=1;
             return sweepEvents.begin()+changePosition;
         }
    }
    if (found==1){
        sweepEvents.rep().insert(sweepEvents.begin()+changePosition,edge);
        sweepEvents.rep().erase(sweepEvents.begin()+changePosition+1);
    }   
    else{
        sweepEvents.rep().insert(sweepEvents.end(),edge);
    }

    return sweepEvents.end()-1;
}

I then call this iterator in the main function. I actually tried but it does not compile and I do not know what syntax to use other than this:

然后我在main函数中调用这个迭代器。我实际上尝试但它不编译,我不知道除了这个使用什么语法:

int main(){
    Seq< vector<int> > sweepEvents;
    vector<int> edge;
    //.....initialize sweepEvents and edge

    //declare iterator but not working
     Seq< vector<int> >::iterator comparePosition; 

   //not working neither
    comparePosition=insertSweepEvents(edge,sweepEvents.size());
}

Any idea on how I should correctly call the iterator? I see it does not work as an integer index from an array?

关于如何正确调用迭代器的任何想法?我看到它不能作为数组的整数索引?

4 个解决方案

#1


Is Seq< vector< T > >::iterator defined in your Seq class?

是否在Seq类中定义了Seq >> :: iterator?

Making the parameter of the template 'vector' does not imply that exists the type Seq< vector< int > >::iterator

制作模板'vector'的参数并不意味着存在Seq >> :: iterator类型

#2


What compilation error ? How is defined Seq<X>::iterator ?

什么编译错误?如何定义Seq :: iterator?

#include <vector>

template<typename T>
struct Seq
  : public std::vector<T>
{ };

typedef Seq< std::vector<int> > SeqI;

SeqI::iterator insertSweepEvents(SeqI &s)
{
  return s.begin();
}

int main()
{
  SeqI s;
  SeqI::iterator e = insertSweepEvents(s);
}

This works fine.

这很好用。

#3


Something illogical in your code. See my comments:

你的代码中有些不合逻辑的东西。看我的评论:

int found = 0;
for (int i=0;i<currentDim;i++){
     if (edge[0]==sweepEvents[i][1]){
                 changePosition=i;
                 found=1; 
// This place is one, where we assign 1 to found, and we do return after that (maybe you want do break/?)
                 return sweepEvents.begin()+changePosition;
         }
}
if (found==1){ // as you see we reach this place only when found == 0

#4


A little sidenote:

一点点旁注:

After a vector is changed, especially when appended to or inserted in, iterators to it become invalid. That's due to the fact that the vector tries to allocate a minimally sized contiguous block of memory for it's internal data, while at the same time it tries to minimize the number of times it needs to allocate a new, bigger chunk. So the data may move through memory, so iterators to it before a push are no longer valid after it.

更改向量后,尤其是在附加或插入时,迭代器将变为无效。这是因为向量试图为其内部数据分配最小尺寸的连续内存块,同时它试图最小化分配新的更大块的需要的次数。因此数据可能会在内存中移动,因此在推送之前它的迭代器不再有效。

Another little note:

另一个小记:

You can find the difference between two iterators by using std::difference( it1, it2 ). You can re-apply that difference by using std::advance( it1, d ).

您可以使用std :: difference(it1,it2)找到两个迭代器之间的差异。您可以使用std :: advance(it1,d)重新应用该差异。

A third little note:

第三个小记:

You seem to have a 'return' statement inside the for-loop, but the rest of the code uses variables that are set only when returning...

您似乎在for循环中有一个'return'语句,但其余代码使用仅在返回时设置的变量...

#1


Is Seq< vector< T > >::iterator defined in your Seq class?

是否在Seq类中定义了Seq >> :: iterator?

Making the parameter of the template 'vector' does not imply that exists the type Seq< vector< int > >::iterator

制作模板'vector'的参数并不意味着存在Seq >> :: iterator类型

#2


What compilation error ? How is defined Seq<X>::iterator ?

什么编译错误?如何定义Seq :: iterator?

#include <vector>

template<typename T>
struct Seq
  : public std::vector<T>
{ };

typedef Seq< std::vector<int> > SeqI;

SeqI::iterator insertSweepEvents(SeqI &s)
{
  return s.begin();
}

int main()
{
  SeqI s;
  SeqI::iterator e = insertSweepEvents(s);
}

This works fine.

这很好用。

#3


Something illogical in your code. See my comments:

你的代码中有些不合逻辑的东西。看我的评论:

int found = 0;
for (int i=0;i<currentDim;i++){
     if (edge[0]==sweepEvents[i][1]){
                 changePosition=i;
                 found=1; 
// This place is one, where we assign 1 to found, and we do return after that (maybe you want do break/?)
                 return sweepEvents.begin()+changePosition;
         }
}
if (found==1){ // as you see we reach this place only when found == 0

#4


A little sidenote:

一点点旁注:

After a vector is changed, especially when appended to or inserted in, iterators to it become invalid. That's due to the fact that the vector tries to allocate a minimally sized contiguous block of memory for it's internal data, while at the same time it tries to minimize the number of times it needs to allocate a new, bigger chunk. So the data may move through memory, so iterators to it before a push are no longer valid after it.

更改向量后,尤其是在附加或插入时,迭代器将变为无效。这是因为向量试图为其内部数据分配最小尺寸的连续内存块,同时它试图最小化分配新的更大块的需要的次数。因此数据可能会在内存中移动,因此在推送之前它的迭代器不再有效。

Another little note:

另一个小记:

You can find the difference between two iterators by using std::difference( it1, it2 ). You can re-apply that difference by using std::advance( it1, d ).

您可以使用std :: difference(it1,it2)找到两个迭代器之间的差异。您可以使用std :: advance(it1,d)重新应用该差异。

A third little note:

第三个小记:

You seem to have a 'return' statement inside the for-loop, but the rest of the code uses variables that are set only when returning...

您似乎在for循环中有一个'return'语句,但其余代码使用仅在返回时设置的变量...