【发布时间】:2013-12-16 03:59:48
【问题描述】:
我正在为班级作业制作一个循环的双向链表,但我遇到了一些麻烦。当我尝试将迭代器放入我自己的 prefix_sum 方法或 STL 版本 (partial_sum) 时,它会在 *result = val; 和 *++result = val; 行(以及 partial_sum 的等效行)上给出错误,说明那是一个lvalue is required as left operand of assignment。
STL 版本的迭代器在我的 prefix_sum 方法中运行良好,所以我知道这是我的迭代器某处的问题,但我无法放置它。
任何帮助将不胜感激。
我的头文件:
#ifndef LIST_H
#define LIST_H
#include <string>
#include <iostream>
template<class T>
struct Node
{
Node* prev;
Node* next;
T val;
Node():prev(this), next(this) {}
Node(T aValue) :prev (this), next(this), val(aValue) {}
};
template<class T>
class s_list
{
public:
struct iterator
{
typedef std::bidirectional_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
typedef T * pointer;
typedef T &reference;
public:
// Constructors:
iterator() :cur(new Node<T>()) {}
// Operatoral Overloads
T operator *() {return cur->val;}
bool operator == (const iterator an_iter) {return (cur == an_iter.cur);}
bool operator != (const iterator an_iter) {return (cur != an_iter.cur);}
iterator& operator ++ () {cur = cur->next; return *this;}
iterator& operator -- () {cur = cur->prev; return *this;}
iterator operator ++ (int)
{
iterator temp = *this;
cur = cur->next;
return temp;
}
iterator operator -- (int)
{
iterator temp = *this;
cur = cur->prev;
return temp;
}
friend class s_list;
private:
iterator(Node<T>* to_iter):cur(to_iter) {}
Node<T>* cur;
};
// Constructors:
s_list() : head(new Node<T>()){}
s_list(const s_list<T>& aList) : head(new Node<T>()) {...}
// Destructor:
virtual ~s_list() {clear(); delete head;}
// Iterator Methods:
iterator begin() {return (iterator(head->next));}
iterator end() {return (iterator(head));}
void push_back(T aVal) {...}
void push_front(T aVal) {...}
void insert(iterator pos, T aVal) {...}
int size() {...}
bool empty() {return (head->next == head->prev);}
inline void erase(iterator to_del);
inline void erase(iterator from, iterator to);
void clear(){erase(head->next, head);}
//inline void prefix_sum(iterator start_sum, iterator end_sum, iterator write_to);
// Operational Overloads:
inline void operator = (const s_list<T>& aList);
private:
Node<T>* head;
};
template <class iterator_in, class iterator_out>
iterator_out prefix_sum (iterator_in first, iterator_in last, iterator_out result)
{
std::cerr << "\n*result = " << *result << '\n';
if (first != last) {
typename std::iterator_traits<iterator_in>::value_type val = *first;
*result = val;
while (++first != last) {
val = val + *first;
*++result = val;
}
++result;
}
return result;
}
#endif // LIST_H
我的(删节的)实现文件:
/**
* @date 24 November, 2013
* @brief Linked List 6
* @file HW7.cpp
*
* @note
* This work is licensed under a Creative Commons Attribution-NonCommercial 3.0
* Unported License.
*
* Permission is granted to copy, distribute, transmit, and/or adapt this software
* for any and all noncommercial purposes.
*
* For details, see:
* https://creativecommons.org/licenses/by-nc/3.0/
*/
#include "s_list.h"
#include <iostream>
#include <sstream>
#include <list>
#include <numeric>
for(int i = 1; i < 10; i++)
{
stlList.push_back(i);
myList.push_back(i);
}
std::cout << "\nOriginal myList:\n";
typename s_list<int>::iterator myIter = myList.begin();
while (myIter != myList.end()){
std::cout << *myIter << " ";
++myIter;
}
std::cout << "\nOriginal stlList:\n";
std::_List_iterator<int> stlIter = stlList.begin();
while (stlIter != stlList.end()){
std::cout << *stlIter << " ";
++stlIter;
}
std::partial_sum(myList.begin(), myList.end(), (myList.begin()));
prefix_sum(myList.begin(), myList.end(), myList.begin());
prefix_sum(stlList.begin(), stlList.end(), stlList.begin());
std::cout << "\nResult after running myList with STL partial_sum() algorithm\n";
myIter = myList.begin();
while(myIter != myList.end()){
std::cout << *myIter << " ";
++myIter;
}
std::cout << "\nResult after running STL list with my prefix_sum() algorithm\n";
stlIter = stlList.begin();
while (stlIter != stlList.end()){
std::cout << *stlIter<< " ";
++stlIter;
}
}
【问题讨论】:
-
太多……太多……东西。
-
只包含对我们解决问题有用的代码,而不是包括 cmets 在内的所有内容
-
很抱歉。我精简了它,但在我完成时已经得到了答案。
标签: c++ list templates iterator listiterator