Sequence

Description

Design a class named Sequence to support several operators.
The class contains:
0、A no-arg constructor that creates a default Sequence.
1、A function named size(),return the number of the element in the sequence.
2、A function named empty(), return whether the sequence has no element.
3、A function named find(int value) that return whether the value is in the sequence.
4、A function named at(int k), return the reference of the k-th element in the sequence. 0 <= k <= size()-1.
5、A function named front(), return the first element in the sequence.
6、A function named back(), return the last element in the sequence.
7、A function named insert(int value), insert an int value to the back of the sequence.
8、A function named insert(int k, int value), insert an int value to the sequence before the k-th element.
9、A function named clear(), clear all the element in the sequence, then the sequence became empty.
10、A function named reverse(), reverse all element in the sequence.
11、A function named reverse(int fir, int las), reverse all elements from the fir-th element to the (las-1)-th element.
12、A function named replace(int value1, int value2), replace the element with value1 to value2.
13、A function named swap(Sequence &seq2), swap all elements from seq2 to seq, also seq to seq2.

Function explain, we define the initial state of the sequence seq1 with four integers {1,2,3,3}, another sequence seq2 is {3,4,5}
called size():      4
called empty():     false
called find(5):     false (because 5 is not in the sequence)
called at(3):       return the reference of third element
called front():     1 
called back():      3
called insert(6):   the sequence became {1,2,3,3,6} 
called insert(2,4): the sequence became {1,2,4,3,3} 
called clear():     the sequence became {}
called reverse:     the sequence became {3,3,2,1}
called reverse(1,3): the sequence became {1,3,2,3}
called replace(3,5): the sequence became {1,2,5,5}
called swap(seq2)  : seq1 became {3,4,5} and seq2 became {1,2,3,3}

you can assume that the total of the operators is not greater thant 1000.

public:
Sequence();
int size();
bool empty();
bool find(int value);
int &at(int pos);
int front();
int back();
void insert(int value);
void insert(int pos, int value);
void clear();
void reverse();
void reverse(int fir, int las);
void replace(int value1, int value2);
void swap(Sequence &seq2);
};
.h文件
#ifndef SEQUENCE_H_
#define SEQUENCE_H_

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Sequence {
public:
    Sequence();
    ~Sequence();
    int size();
    bool empty();
    bool find(int value);
    int &at(int pos);
    int front(); 
    int back(); 
    void insert(int value); 
    void insert(int pos, int value); 
    void clear();
    void reverse(); 
    void reverse(int fir, int las); 
    void replace(int value1, int value2);
    void swap(Sequence &seq2);
    int *a;
    int msize;
    bool mempty;
};
#endif
main函数:
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
#include "Sequence.h"
using namespace std;

int main() {
    Sequence seq;
    string st;
    int tot = 0;
    int x, y, number, number1, number2, pos;
    seq.insert(1); seq.insert(2); seq.insert(3);
    while (cin >> st) {
        if (st == "size") {
            cout << seq.size() << endl;
            ++tot;
        }
        else
            if (st == "at") {
                cin >> number;
                cout << seq.at(number) << endl;
                ++tot;
            }
            else
                if (st == "insert1") {
                    cin >> number;
                    seq.insert(number);
                    ++tot;
                }
                else
                    if (st == "insert2") {
                        cin >> pos >> number;
                        seq.insert(pos, number);
                        ++tot;
                    }
                    else
                        if (st == "reverse1") {
                            seq.reverse();
                            ++tot;
                        }
                        else
                            if (st == "reverse2") {
                                cin >> x >> y;
                                seq.reverse(x, y);
                                ++tot;
                            }
                            else
                                if (st == "replace") {
                                    cin >> number1 >> number2;
                                    seq.replace(number1, number2);
                                    ++tot;
                                }
                                else
                                    if (st == "find") {
                                        cin >> number;
                                        cout << seq.find(number) << endl;
                                        ++tot;
                                    }
                                    else
                                        if (st == "empty") {
                                            cout << seq.empty() << endl;
                                            ++tot;
                                        }
                                        else
                                            if (st == "front") {
                                                cout << seq.front() << endl;
                                                ++tot;
                                            }
                                            else
                                                if (st == "back") {
                                                    cout << seq.back() << endl;
                                                    ++tot;
                                                }
    }
    for (int i = 0; i<seq.size(); i++) cout << seq.at(i) << endl;
    Sequence tmp;
    tmp.insert(3);
    tmp.swap(seq);
    cout << tmp.size() << endl;
    for (int i = 0; i<tmp.size(); i++) cout << tmp.at(i) << endl;
    return 0;
}

#include"Sequence.h"
#include<algorithm>
    Sequence::Sequence()
    {
        a=new int[1000000];
      msize=0;
    }
    Sequence::~Sequence()
    {
     delete[]a;   
    }
    int Sequence::size()
    {
        return msize;
    }
    bool Sequence::empty()
    {
        if(msize)
            return false;
        return true;
    }
    bool Sequence::find(int value)
    {
        for(int i=0;i<msize;++i)
            if(a[i]==value)
                return true;
        return false;
    }
    int& Sequence::at(int pos)
    {
        return *(a+pos);
    }
    int Sequence::front()
    {
        return *(a);
    }
    int Sequence::back()
    {
        return *(a+msize-1);
    }
    void Sequence::insert(int value)
    {
        *(a+msize)=value;
    ++msize;
    }
    void Sequence::insert(int pos, int value)
    {

        for(int i=msize;i>pos;--i)
            *(a+i)=*(a+i-1);
        *(a+pos)=value;
      ++msize;
    }
    void Sequence::clear()
    {
        msize=0;
    }
    void Sequence::reverse()
    {/*
        int temp;
        for(int i=0;i<=(msize-1)/2;++i)
        {
            temp=a[i];
            a[i]=a[msize-1-i];
            a[msize-1-i]=temp;
        }*/
      reverse(0,size());
    }
    void Sequence::reverse(int fir, int las)
    {/*
        int temp;
        for(int i=fir;i<=(fir+las)/2;++i)
        {
            temp=a[i];
            a[i]=a[msize-1-i];
            a[msize-1-i]=temp;
        }*/
      std::reverse(a+fir,a+las);
    }
    void Sequence::replace(int value1, int value2)
    {
        for(int i=0;i<msize;++i)
            if(a[i]==value1)
                a[i]=value2;

    }
    void Sequence::swap(Sequence &seq2)
    {
        std::swap(a,seq2.a);
      std::swap(msize,seq2.msize);
    }

reverse函数可以反转一个容器中的内容,包含在 <algorithm> < a l g o r i t h m > <script type="math/tex" id="MathJax-Element-3"> </script>库中。
2、参数:first、last

first和last是双向迭代器类型,reverse函数反转的范围是[first,last),所以包括first指向的元素,不包括last指向的元素。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值