gpt4 book ai didi

c++ - 嵌套迭代器类、begin() 和 end() 哨兵问题

转载 作者:行者123 更新时间:2023-11-27 23:09:52 26 4
gpt4 key购买 nike

我的嵌套迭代器类有问题。 end() 哨兵返回 0,我很迷茫!任何帮助,将不胜感激。我希望这个程序能够接受任何类型的输入,例如一个 int,创建 vector ,并能够填充数组直到到达 end() 标记,例如键入 Q 或任何非整数。我去掉了几行不必要的代码来缩短它。

template <class T>
class Set
{
private:
vector<T> m_element;
int size;
public:
Set() : size(0) {};
................
................
................

class iterator;
friend class iterator;
class iterator
{
private:
int i_index;
Set& s;
public:
iterator(Set& is) :s(is), i_index(0) {}
iterator(Set& is, bool) : s(is), i_index(s.size) {}
int operator++()
{
return s.m_element[++i_index];
}
(other postfix ++ operator)
int current() const
{
return s.m_element[i_index];
}
....................
bool operator!=(const iterator &rv) const
{
return i_index != rv.i_index;
}
.............................
int getI()
{
return i_index;
}
};
iterator begin() { return iterator(*this);}
iterator end() {return iterator(*this, true);}
};

template <class T>
istream& operator>> (istream &in, Set<T> &element)
{
T elementin;
Set<T> is;
class Set<T>::iterator it = element.begin();
while(it != element.end())
{
in >> elementin;
element.push(elementin);
}
return in;
}

int main()
{
Set<int> intset;
Set<int>::iterator it = intset.begin();
for(int i = 0; i < 5; i++)
{
intset.push(i);
cout << it.getI();
++it;
}

cout << endl << intset.begin() << intset.end();

return 0;
}

OUTPUT:
01234
00

最佳答案

首先,您应该提供简短、独立、正确的示例 (SSCCE)。

假设我已经正确假设了一些功能,下面是如何解决您面临的问题的程序:

#include <iostream>
#include <vector>

using namespace std;


template <class T>
class Set
{
private:
vector<T> m_element;
int size;
public:
Set() : size(0) {};
Set(const Set& rhs)
{
m_element = rhs.m_element;
size = rhs.size;
}

void push(T data)
{

m_element.push_back(data);
++size;
}

class iterator;
friend class iterator;
class iterator
{
private:
int i_index;
Set& s;
public:
iterator(Set& is) :s(is), i_index(0) {}
iterator(Set& is, bool) : s(is), i_index(s.size) {}
iterator(Set& is, bool val,bool last) : s(is), i_index(s.size) {

cout<<endl<<"size "<<s.size<<endl;
if(last)
i_index = s.size -1 ;
}

T operator*()
{
return s.m_element[i_index];
}

int operator++()
{
return s.m_element[++i_index];
}
int current() const
{
return s.m_element[i_index];
}
bool operator!=(const iterator &rv) const
{
return i_index != rv.i_index;
}
int getI()
{
return i_index;
}
};
iterator begin() { return iterator(*this);}
//function to check if we have passed last element
iterator end() {return iterator(*this, true);}
//function to get last element
iterator last() {return iterator(*this, true,true);}

};

template <class T>
istream& operator>> (istream &in, Set<T> &element)
{
T elementin;
Set<T> is;
class Set<T>::iterator it = element.begin();
while(it != element.end())
{
in >> elementin;
element.push(elementin);
}
return in;
}

int main()
{
Set<int> intset;
Set<int>::iterator it = intset.begin();
for(int i = 0; i < 5; i++)
{
intset.push(i);
cout << it.getI();
++it;
}

// cout << endl << intset.begin() << intset.end();
//We use last() to print the last element as end() confirms
//that we have passed last element and does not point to last element.
cout << endl << *(intset.begin()) << " " <<*(intset.last())<<endl;

return 0;
}

关于c++ - 嵌套迭代器类、begin() 和 end() 哨兵问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com