具备迭代器功能的容器类sequence
来源:互联网 发布:招聘数据统计分析表 编辑:程序博客网 时间:2024/06/02 02:24
头文件sequence.h:
#ifndef MAIN_WTF_SEQUENCE_H
#define MAIN_WTF_SEQUENCE_H
#include <cstdlib>
namespace main_wtf_2
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
sequence();
void start();
void advance();
void insert(const value_type& entry);
void attach(const value_type& entry);
void remove_current();
size_type size() const;
bool is_item() const;
value_type current() const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}
#endif
实现文件sequence.cpp:
#include "sequence.h"
#include<iostream>
using namespace std;
namespace main_wtf_2
{
sequence::sequence()
{
used = 0;
current_index = 0;
}
void sequence::start()
{
current_index = 0;
}
void sequence::advance()
{
// if(current_index <= used)
current_index++;
}
bool sequence::is_item() const
{
if(current_index >= used)
return false;
return true;
}
void sequence::insert(const value_type& entry)
{
if(used < sequence::CAPACITY)
{
int count = used-current_index;
int i=0;
for(;i<count;i++)
{
data[used-i] = data[used-i-1];
}
data[used-i] = entry;
used++;
}
else
cout<<"No room for new entry"<<endl;
}
void sequence::attach(const value_type& entry)
{
if(used < sequence::CAPACITY)
{
int count = used-current_index-1;
int i=0;
for(;i<count;i++)
{
data[used-i] = data[used-i-1];
}
data[used-i] = entry;
used++;
current_index++;
}
else
cout<<"No room for new entry"<<endl;
}
void sequence::remove_current()
{
int count = used-current_index-1;
for(int i=0;i<count;i++)
{
data[current_index+i] = data[current_index+i+1];
}
used--;
}
sequence::size_type sequence::size() const
{
return used;
}
sequence::value_type sequence::current() const
{
return data[current_index];
}
}
测试代码:
#include<cctype>
#include<iostream>
#include<cstdlib>
#include "sequence.h"
using namespace std;
using namespace main_wtf_2;
void print_menu();
char get_user_command();
void show_sequence(sequence display);
char get_user_command();
double get_number();
int main()
{
sequence test;
char choice;
cout<<"I have initialized an empty sequence of real numbers"<<endl;
do
{
print_menu();
choice = toupper(get_user_command());
switch(choice)
{
case '!': test.start();
break;
case '+': test.advance();
break;
case '?': if(test.is_item())
cout<<"There is an item."<<endl;
else
cout<<"There is no current item."<<endl;
break;
case 'C': if(test.is_item())
cout<<"Current item is"<<test.current()<<endl;
else
cout<<"There is no current item."<<endl;
break;
case 'P': show_sequence(test);
break;
case 'S': cout<<"Size is "<<test.size()<<'.'<<endl;
break;
case 'I': test.insert(get_number());
break;
case 'A': test.attach(get_number());
break;
case 'R': test.remove_current();
cout<<"The current item has been removed"<<endl;
break;
case 'Q':cout<<"Ridicule is the best test of truth."<<endl;
break;
default:cout<<choice<<"is invalid"<<endl;
}
}while((choice != 'Q'));
return EXIT_SUCCESS;
}
void print_menu()
{
cout<<endl;
cout<<"The following choices are available"<<endl;
cout<<"! Activate the start() function"<<endl;
cout<<"+ Activate the advance() function"<<endl;
cout<<"? print the Result from the is_item() function"<<endl;
cout<<"C print the Result from the current() function"<<endl;
cout<<"P Print a copy of the entir sequence"<<endl;
cout<<"S Print the result from the size() function"<<endl;
cout<<"I Insert a new number with the insert() function"<<endl;
cout<<"A Attach a new number with attach function"<<endl;
cout<<"R Activate the remove_current() function"<<endl;
cout<<"Q Quit this test program"<<endl;
}
char get_user_command()
{
char command;
cout<<"Enter choice: ";
cin>>command;
return command;
}
void show_sequence(sequence display)
{
for(display.start();display.is_item();display.advance())
cout<<display.current()<<endl;
}
double get_number()
{
double result;
cout<<"Please enter a real number for the sequence: ";
cin>>result;
cout<<result<<" has been read. "<<endl;
return result;
}
- 具备迭代器功能的容器类sequence
- Appium具备的功能
- 通用底层具备的功能
- 教育类App应该具备的核心功能
- 企业信息化系统应该具备的功能
- 具备“鬼魂探测”功能的USB闪存
- 让你的浏览器具备Wap功能
- 医院呼叫中心应具备的功能
- 如何使类具备序列化功能
- 容器的功能
- FMDBModel使你的实体类具备直接操作数据库的功能
- 不具备自动清除功能的窗口类 释放 (在网上看到 摘过来 哈哈)
- 移动应用具备功能
- 一个软件系统应该具备的最基本的功能
- 公司的Java框架应具备的通用功能
- 我认为的公司成熟框架应该具备的通用功能
- 公司的Java框架应具备的通用功能
- 公司的Java框架应具备的通用功能
- 指针遍历二维数组的三种方式
- 安装smooks以及eclipse插件
- 观察者模式——解决、解耦的钥匙
- Swift 中枚举高级用法及实践
- 视屏播放(直播)
- 具备迭代器功能的容器类sequence
- 自定义view--自定义分类Tab菜单条,自定义垂直progressbar(类似温度计)
- 1. 支付宝、微信支付接入集成
- Java类加载机制浅析
- weblogic Error [Management:141191]The prepare phase of the configuration update failed with an excep
- 30. Substring with Concatenation of All Words
- target情感分类(1,0,-1)——Target-dependent sentiment classification with long short term memory
- iOS中为项目添加自定义的字体
- ansible获取主机信息并存入mysql数据库