我试图通过使用插入和附加函数从实现文件中排列它们来将数字存储在我的驱动程序类中。插入函数应该将数字移动到左侧或更小的位置,并且附加移动到数据数组中的右侧或更高位置。这是实现一个:
#include "Lab1A.h"
#include <iostream>
#include <cassert>
#include <algorithm>
sequence::sequence() {
used = 0;
current_index = 0;
}
// MUTATOR MEMBER FUNCTIONS
//Postcondition: The first item in the sequence becomes the current item
void sequence::start() {
current_index = 0;
//Precondition: is_item returns true.
//Postcondition: If the current item was already the last item in the
//sequence, then there is no longer any current item. Otherwise, the new
//current item is the item immediately after the original current item.
}
void sequence::advance() {
if (is_item()) {
current_index++;
}
}
//Precondition: size( ) < CAPACITY.
//Postcondition: A new copy of entry has been inserted in the sequence
//before the current item. If there was no current item, then the new entry
//has been inserted at the front of the sequence (position 0). In either //case, the newly inserted item is now the current item of the sequence.
void sequence::insert(const value_type& entry) {
if (size() < CAPACITY) {
data[used] = data[used - 1];
data[used] = entry;
data[current_index] = entry;
used++;
}
if (is_item() == false) {
data[used] = entry;
data[used] = data[used + 1];
}
}
//Precondition: size( ) < CAPACITY.
//Postcondition: A new copy of entry has been inserted in the sequence //after the current item. If there was no current item, then the new entry //has been attached to the end of the sequence. In either case, the newly
//inserted item is now the current item of the sequence.
void sequence::attach(const value_type& entry) {
if (size() < CAPACITY) {
data[used] = data[used + 1];
data[used] = entry;
data[current_index] = entry;
used++;
}
if (is_item() == false) {
data[used] = entry;
data[used] = data[used + 1];
}
}
//Precondition: is_item returns true.
//Postcondition: The current item has been removed from the sequence, and //the item after this (if there is one) is now the new current item.
void sequence::remove_current() {
int i;
if (is_item()) {
current_index--;
data[i] = data[current_index];
}
}
// ACCESSOR MEMBER FUNCTIONS
//Postcondition: The value returned is the number of items in the
//sequence.
int sequence::size() const {
return used;
}
//Postcondition: A true return value indicates that there is a valid
//"current" item that may be retrieved by invoking the current
//member function below. A false return value indicates that
//there is no valid current item.
bool sequence::is_item() const {
return (current_index < used);
}
//Precondition: is_item( ) returns true.
//Postcondition: The item returned is the current item in the sequence.
sequence::value_type sequence::current() const {
return data[current_index];
}
void sequence::print() {
for (int j = 0; j < used; j++) {
cout << data[j] << " ";
}
}
驱动程序文件:
#include <iostream>
#include <cstdlib>
#include "Lab1Aimplementation.cpp"
using namespace std;
int main()
{
sequence numbers;
numbers.insert(21);
numbers.attach(33);
numbers.insert(22);
numbers.print();
return 0;
}
我试图获得此输出:21 22 33
相反,我得到:22 33 22
可能声明sequence
,因为OP没有附加一个:
class sequence
{
using index_type = int;
using value_type = size_t;
static const index_type CAPACITY = 1024;
value_type data[CAPACITY];
index_type used;
index_type current_index;
public:
sequence();
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
int size() const;
bool is_item() const;
value_type current() const;
void print();
};
答案 0 :(得分:0)
我在这里使用这些假设:
current_index
可以用迭代器表示;当没有元素可用时,它指向past-the-end。insert
应该在当前项目之前插入,attach
应该在当前项目之后插入。21 22 33
)判断的两种插入方法都是这样的,在调用之后,当前项总是引用新插入的项。这就是说,如果你只是环绕一个列表,每个函数基本上是一个单行,你只需要知道list class does。奖励:O(1)插入和移除,容量限制几乎被删除
OP将current_index
重置为0,因此可以合理地假设它可以指向数组中的任何位置。因此,只能通过交换元素来实现插入,您需要移动整个数据块。试试看here。
#include <iostream>
#include <sstream>
#include <list>
#include <cassert>
template <typename T>
class sequence
{
private:
std::list<T> _l;
typename std::list<T>::iterator _i;
public:
using value_type = T;
using size_type = typename std::list<T>::size_type;
size_type size() const {
return _l.size();
}
T current() const {
assert(is_current_valid());
return *_i;
}
size_type current_index() const {
return _i - _l.begin();
}
void increase_current() {
if (is_current_valid()) {
++_i;
}
}
void decrease_current() {
if (_i != _l.begin()) {
--_i;
}
}
void reset_current() {
_i = _l.begin();
}
bool is_current_valid() const {
// "is_item"
return _i != _l.end();
}
void remove_current() {
assert(is_current_valid());
// _i takes the next current element, eventually end()
_i = _l.erase(_i);
}
void insert_before(const value_type &entry) {
// _i is always the newly inserted element
_i = _l.insert(_i, entry);
}
void insert_after(const value_type &entry) {
// _i is always the newly inserted element
assert(is_current_valid());
_i = _l.insert(++_i, entry);
}
friend std::ostream &operator<<(std::ostream &os, sequence const &s) {
for (auto it = s._l.begin(); it != s._l.end(); ++it) {
if (it != s._l.begin()) {
os << " " << *it;
} else {
os << *it;
}
}
return os;
}
sequence() : _l(), _i(_l.end()) {}
};
int main() {
sequence<std::size_t> numbers;
numbers.insert_before(21); // insert 21, then points to 21
numbers.insert_after(33); // 33 after 21, then points to 33
numbers.insert_before(22); // 22 before 21, then points to 22
std::cout << numbers << std::endl;
// Programmatically check if the result is the requested one
const std::string expected = "21 22 33";
std::stringstream output;
output << numbers;
if (output.str() != expected) {
std::cerr << "Error!" << std::endl;
return 1;
}
return 0;
}
答案 1 :(得分:0)
在http://www.cplusplus.com/forum/beginner/141458/找到答案。这段代码有效,但我不理解我认为背后循环背后的逻辑。
void sequence::attach(const value_type& entry) {// value_type is the declared typedef double data
int i;
if(!is_item()) // is_item checks if there's any number in the array
current_index = used - 1; // used keeps track of how many numbers are stored
for (i = used; i > current_index; --i)
data[i]=data[i-1];
data[current_index+1] = entry;
++current_index;
++used;
}
void sequence::insert(const value_type& entry){
int i;
if(!is_item())
current_index = used;
for (i = used; i > current_index; --i)
data[i]=data[i-1];
data[current_index] = entry;
++current_index;
++used;
}