for_each for Node manually c++
for_each for Node manually c++
如何为Node和List的实现实现for_each?我相信这不是很长的代码,可以分享一下他的知识吗?我需要将它作为模板来实现还是仅仅作为内部函数来实现。谢谢你的帮助。
class Node {
int data;
Node *next;
public:
Node() {}
void SetData(int aData) { data = aData; }
void SetNext(Node *aNext) {
next = aNext;
}
int Data() { return data; }
Node* Next() { return next; }
};
// List class
class List {
Node *head;
public:
List() { head = nullptr; }
void Append(int data){
// Create a new node
Node *newNode = new Node();
newNode->SetData(data);
newNode->SetNext(nullptr);
// Create a temp pointer
Node *tmp = head;
if ( tmp != nullptr ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != nullptr) {
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}
void Delete(int data){
// Create a temp pointer
Node *tmp = head;
// No nodes
if ( tmp == nullptr ) return;
// Last node of the list
if ( tmp->Next() == nullptr ) {
delete tmp;
head = nullptr;
}
else {
// Parse thru the nodes
Node* prev;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != nullptr );
// Adjust the pointers
if (tmp->Next()!=nullptr) prev->SetNext(tmp->Next());
else prev->SetNext(nullptr);
// Delete the current node
if (tmp!=nullptr) delete tmp;
}
}
};
编辑:这是迭代器的用法:
for (List<Pair, CompareFunction>::myIterator it = this->_pairs.begin();
it != this->_pairs.end(); ++it) {
Pair cur_pair = *it;
if (cur_pair.first == key) {
this->_pairs.Delete(cur_pair);
this->_size--;
}
}
这是Pair Class作为另一个模板类中的子类:
template <class KeyType, class ValueType, class CompareFunction = std::less<KeyType> >
class MtmMap {
public:
class Pair {
public:
Pair():first(KeyType()){} ////////////////////
Pair(const KeyType& key, const ValueType& value)
: first(key), second(value) {}
const KeyType first;
ValueType second;
~Pair() = default;
在我们的具体案例中,KeyType和ValueType都作为int.
将以下代码添加到您的List类中:
class List{
...
class myIterator
{
public:
typedef myIterator self_type;
typedef Node* pointer;
myIterator(pointer ptr) : ptr_(ptr) { }
self_type operator++() {
self_type i = *this;
if (ptr_) ptr_ = ptr_->Next();
return i;
}
int operator*() { if (ptr_ ) return ptr_->Data(); else return 0; }
bool operator==(const self_type& rhs) {
if (!ptr_ && !rhs.ptr_) return true;
return (ptr_ && rhs.ptr_ && rhs.ptr_->Data()==ptr_->Data());
}
bool operator!=(const self_type& rhs) {
return !(*this==rhs);
}
private:
pointer ptr_ = nullptr;
};
myIterator begin() { return myIterator(head); }
myIterator end() { return myIterator(nullptr); }
};
然后,您可以使用正常的迭代器,如以下示例所示:
List l;
l.Append(2);
l.Append(5);
l.Append(7);
l.Append(11);
for (int i: l) std::cout << i << std::endl;
//or
for (myIterator it=l.begin(); it!=l.end(); ++it) std::cout << *it << std::endl;
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- std::memory_order for std::atomic:<T>:wait
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 如何用for循环在c++中生成单词三角形