求助……关于iterator

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求助……关于iterator相关的知识,希望对你有一定的参考价值。

iterator怎么写啊……对于写法完全没概念……

写一个自己的 CMyostream_iterator 模板,使之能和 ostream_iterator 模板达到一样的效果,即

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;

int main()

int a[5] = 1,2,3,4,5;

CMyostream_iterator<int> output(cout,"*");

vector<int> v(a,a+5);

copy(v.begin(),v.end(),output);

return 0;



程序的输出结果是:

1*2*3*4*5*

注意,编写CMyostream_iterator时不能使用 ostream_iterator

参考 copy 的help

copy

template<class InIt, class OutIt> OutIt copy(InIt first, InIt last, OutIt x);

The template function evaluates *(x + N) = *(first + N)) once for each N in the range [0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns x + N. If x and first designate regions of storage, x must not be in the range [first, last)
希望尽快回答~谢谢~
一楼的……=口=不会这么难吧……会不会有什么别的算法可以用呢?我觉得应该不会布置这么难的……

参考技术A #ifdef __BORLANDC__
// suppress the warning message that functions containing for are not
// expanded inline
#pragma warn -8027
#endif // __BORLANDC__

class iterator;
class const_iterator;
// declare the iterator classes so the names are available

friend class iterator;
friend class const_iterator;
// allow the iterator classes to access the private section
// of BinSearchTree

class iterator

friend class BinSearchTree<T>;
friend class const_iterator;

public:

// constructor
iterator ()


// comparison operators. just compare node pointers
bool operator== (const iterator& rhs) const

return nodePtr == rhs.nodePtr;


bool operator!= (const iterator& rhs) const

return nodePtr != rhs.nodePtr;


// dereference operator. return a reference to
// the value pointed to by nodePtr
T& operator* () const

if (nodePtr == NULL)
throw
referenceError("BinSearchTree iterator operator* (): NULL reference");

return nodePtr->data;


// preincrement. move forward to next larger value
iterator& operator++ ()

BSTNode<T> *p;

if (nodePtr == NULL)

// ++ from end(). get the root of the tree
nodePtr = tree->root;

// error! ++ requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator++ (): tree empty");

// move to the smallest value in the tree,
// which is the first node inorder
while (nodePtr->left != NULL)
nodePtr = nodePtr->left;

else
if (nodePtr->right != NULL)

// successor is the furthest left node of
// right subtree
nodePtr = nodePtr->right;

while (nodePtr->left != NULL)
nodePtr = nodePtr->left;

else

// have already processed the left subtree, and
// there is no right subtree. move up the tree,
// looking for a parent for which nodePtr is a left child,
// stopping if the parent becomes NULL. a non-NULL parent
// is the successor. if parent is NULL, the original node
// was the last node inorder, and its successor
// is the end of the list
p = nodePtr->parent;

while (p != NULL && nodePtr == p->right)

nodePtr = p;
p = p->parent;


// if we were previously at the right-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;


return *this;


// postincrement
iterator operator++ (int)

// save current iterator
iterator tmp = *this;

// move myself forward to the next tree node
++*this;

// return the previous value
return tmp;


// predecrement. move backward to largest value < current value
iterator& operator-- ()

BSTNode<T> *p;

if (nodePtr == NULL)

// -- from end(). get the root of the tree
nodePtr = tree->root;

// error! -- requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator--: tree empty");

// move to the largest value in the tree,
// which is the last node inorder
while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
else if (nodePtr->left != NULL)

// must have gotten here by processing all the nodes
// on the left branch. predecessor is the farthest
// right node of the left subtree
nodePtr = nodePtr->left;

while (nodePtr->right != NULL)
nodePtr = nodePtr->right;

else

// must have gotten here by going right and then
// far left. move up the tree, looking for a parent
// for which nodePtr is a right child, stopping if the
// parent becomes NULL. a non-NULL parent is the
// predecessor. if parent is NULL, the original node
// was the first node inorder, and its predecessor
// is the end of the list
p = nodePtr->parent;
while (p != NULL && nodePtr == p->left)

nodePtr = p;
p = p->parent;


// if we were previously at the left-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;


return *this;


// postdecrement
iterator operator-- (int)

// save current iterator
iterator tmp = *this;

// move myself backward to the previous tree node
--*this;

// return the previous value
return tmp;


private:

// nodePtr is the current location in the tree. we can move
// freely about the tree using left, right, and parent.
// tree is the address of the BinSearchTree object associated
// with this iterator. it is used only to access the
// root pointer, which is needed for ++ and --
// when the iterator value is end()
BSTNode<T> *nodePtr;
BinSearchTree<T> *tree;

// used to construct an iterator return value from
// an BSTNode pointer
iterator (BSTNode<T> *p, BinSearchTree<T> *t) : nodePtr(p), tree(t)


;

class const_iterator

friend class BinSearchTree<T>;

public:

// constructor
const_iterator ()


// used to convert a const iterator to a const_iterator
const_iterator (const iterator& pos): nodePtr(pos.nodePtr)


// comparison operators. just compare node pointers
bool operator== (const const_iterator& rhs) const

return nodePtr == rhs.nodePtr;


bool operator!= (const const_iterator& rhs) const

return nodePtr != rhs.nodePtr;


// dereference operator. return a reference to
// the value pointed to by nodePtr
const T& operator* () const

if (nodePtr == NULL)
throw
referenceError("BinSearchTree const_iterator operator* (): NULL reference");

return nodePtr->data;


// preincrement. move forward to next larger value
const_iterator& operator++ ()

BSTNode<T> *p;

if (nodePtr == NULL)

// ++ from end(). get the root of the tree
nodePtr = tree->root;

// error! ++ requested for an empty tree
if (nodePtr == NULL)
throw underflowError("BinSearchTree const_iterator operator++ (): tree empty");

// move to the smallest value in the tree,
// which is the first node inorder
while (nodePtr->left != NULL)
nodePtr = nodePtr->left;

else
if (nodePtr->right != NULL)

// successor is the furthest left node of
// right subtree
nodePtr = nodePtr->right;

while (nodePtr->left != NULL)
nodePtr = nodePtr->left;

else

// have already processed the left subtree, and
// there is no right subtree. move up the tree,
// looking for a parent for which nodePtr is a left child,
// stopping if the parent becomes NULL. a non-NULL parent
// is the successor. if parent is NULL, the original node
// was the last node inorder, and its successor
// is the end of the list
p = nodePtr->parent;

while (p != NULL && nodePtr == p->right)

nodePtr = p;
p = p->parent;


// if we were previously at the right-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;


return *this;


// postincrement
const_iterator operator++ (int)

// save current const_iterator
const_iterator tmp = *this;

// move myself forward to the next tree node
++*this;

// return the previous value
return tmp;


// predecrement. move backward to largest value < current value
const_iterator& operator-- ()

BSTNode<T> *p;

if (nodePtr == NULL)

// -- from end(). get the root of the tree
nodePtr = tree->root;

// error! -- requested for an empty tree
if (nodePtr == NULL)
throw
underflowError("BinSearchTree iterator operator--: tree empty");

// move to the largest value in the tree,
// which is the last node inorder
while (nodePtr->right != NULL)
nodePtr = nodePtr->right;
else if (nodePtr->left != NULL)

// must have gotten here by processing all the nodes
// on the left branch. predecessor is the farthest
// right node of the left subtree
nodePtr = nodePtr->left;

while (nodePtr->right != NULL)
nodePtr = nodePtr->right;

else

// must have gotten here by going right and then
// far left. move up the tree, looking for a parent
// for which nodePtr is a right child, stopping if the
// parent becomes NULL. a non-NULL parent is the
// predecessor. if parent is NULL, the original node
// was the first node inorder, and its predecessor
// is the end of the list
p = nodePtr->parent;
while (p != NULL && nodePtr == p->left)

nodePtr = p;
p = p->parent;


// if we were previously at the left-most node in
// the tree, nodePtr = NULL, and the iterator specifies
// the end of the list
nodePtr = p;


return *this;


// postdecrement
const_iterator operator-- (int)

// save current const_iterator
const_iterator tmp = *this;

// move myself backward to the previous tree node
--*this;

// return the previous value
return tmp;


private:

// nodePtr is the current location in the tree. we can move
// freely about the tree using left, right, and parent.
// tree is the address of the BinSearchTree object associated
// with this iterator. it is used only to access the
// root pointer, which is needed for ++ and --
// when the iterator value is end()
const BSTNode<T> *nodePtr;
const BinSearchTree<T> *tree;

// used to construct a const_iterator return value from
// an BSTNode pointer
const_iterator (const BSTNode<T> *p, const BinSearchTree<T> *t) : nodePtr(p), tree(t)


;

使用类似的方法,使你的迭代器符合输出迭代器的概念就可以了。

关于threejs创建纹理,求助

最近项目要用threejs,遇到一个问题不能解决。通过

new THREE.BoxGeometry(2, 2, 2);

创建出的平面贴纹理没有问题,

THREE.ImageUtils.loadTexture(“url”);
...
//成功

后来自定义了一些平面、几何体,

var verticesTest = [...];
var faceTest = [...];
var geomTest = new THREE.Geometry();
geomTest.vertices = verticesTest;
geomTest.faces = faceTest;
geomTest.computeFaceNormals();
geomTest.mergeVertices();

THREE.ImageUtils.loadTexture(“url”);
...
//
失败

求大神来帮忙解答,十分感谢。























以上是关于求助……关于iterator的主要内容,如果未能解决你的问题,请参考以下文章

关于Laravel CSRF 的问题求助

【求助】关于Qt的QMdiArea的问题

求助关于speedtest中ookla跨域问题

【求助】问一个关于UF_OBJ_cycle_objs_in_part()的简单的问题

求助关于scrollToRowAtIndexPath 不执行

关于threejs创建纹理,求助