回分類題庫
d178: OOP Lab Exercise: Fake But Usable Vector Class
出處:

Difficulity : N/A
Accepted : 52 Times | Submit :133 Times | Clicks : 1694
Accepted : 46 Users | Submit : 49 Users | Accepted rate : 94%
Time Limit :10000 ms | Memory Limit : 64000 KBytes
題目加入時間 : 2010-05-18 15:15

Content :

As a user, we use std::vector to store elements of any type. Now you are a developer, you are asked to implement a simple vector class.

The elements of a vector is consecutive in memory space. Therefore, if you erase an element, say, at position X. Those elements after X shall be move forward to fill the blank you’ve just erased. When you insert an element at position X. If X is greater than Size, the element shall be insert at the last position of the vector.

Given the class header difinition, there are some functions you need to implement. Here are the specifications you need to follow:

1.  insert( int i, type& element); => Insert element at position I (i is the index).
2.  erase(int i); => Erase the element at position i. Notice that you must make sure the elements are consecutive.
3.  clear(); => Make the vector empty. (You don’t really need to delete the array.)
4.  type& MyVector::operator[](int pos); => Overload [] of lvalue. Ex:  vec[3] = 4;  // 3 is the parameter of []  (pos)
5.  const type& MyVector::operator[](int pos)const; => Overload [] of rvalue. Ex: a = vec[3];  // 3 is the parameter of []  (pos)

For 2, 4 and 5, you have to output a line "Array index out of bounds" if index < 0 or index >= size and do nothing.

Another thing is there are some missing statement of template, you have add them or you will get a lot of compilation error (CE).

To gain pass in this exercise, you have to:
1.  Submit with your oop account and gain AC.
2.  Follow the spec above.
3.  Start and submit your code using the code segment below.

You can download the code from here:
http://www.cs.nccu.edu.tw/~g9803/oop/MyVectorTemplate_ex.cpp

========================================================
#include <iostream>
#include <string>

using namespace std;


class MyVector{
    public:

        MyVector(int cap=1024);               // Constructor
        MyVector(const MyVector&);              // Copy Constructor
        ~MyVector();                          // Destructor
        void insert(int,const type&);             // Input an element to array
        void erase(int);                    // Erase an element to array
        void clear();                       // Reset function
        void swap(MyVector&);                 // Swap two array
        int size()const;                    // Return size
        int capacity() const;               // Return capacity
        type& operator[](int);              // Operator Overloading [] of lvalue
        const type& operator[](int) const;  // Operator Overloading [] of rvalue
        MyVector& operator=(MyVector);          // Operator Overloading =


    private:

        int Size;                           // Used size in array
        int Capacity;                       // Real capacity of the array
        type* elements;                     // Array pointer

};

// Constructor

MyVector::MyVector(int cap){
    Size = 0;
    elements = new type[cap];
    Capacity = cap;
}

// Copy constructor

MyVector::MyVector(const MyVector& intV){
    Size = intV.size();
    Capacity = intV.capacity();
    elements = new type[intV.capacity()];
    for(int i = 0 ; i < Size ; i++ ){
        elements[i] = intV[i];
    }
}

// Destructor

MyVector::~MyVector(){
    delete [] elements;
}

// Size

int MyVector::size()const{
    return Size;
}

// Capacity
int MyVector::capacity()const{
    return Capacity;
}

// Insert an element to array and prevent errors happen

void MyVector::insert(int position , const type& input){

}

// Erase an element from array and prevent error happen

void MyVector::erase(int position){

}

// Swap two MyVectors

void MyVector::swap(MyVector& intV){
    std::swap(*this , intV);
}

// Reset all

void MyVector::clear(){

}

// Operator Overloading []

type& MyVector::operator[](int position){

}

// Operator Overloading []

const type& MyVector::operator[](int position)const{

}

// Operator Overloading =

MyVector& MyVector::operator=(MyVector intV){
    Size = intV.size();
    Capacity = intV.capacity();
    std::swap(elements, intV.elements);
    return *this;
}

int main(){
    string input;
    while(getline(cin, input)){
        MyVector<char> vec1(1);
        for(int i = 0; i < input.length(); i++){
            vec1.insert(0, input.at(i));
        }
        for(int i = 0; i < vec1.size(); i++){
            cout << vec1[i];
        }
        cout << endl;
        //vec1.clear();
        for(int i = 1; i < input.length(); i++){
            vec1[i-1] = vec1[i];
        }
        for(int i = 0; i < vec1.size(); i++){
            cout << vec1[i];
        }
        cout << endl;
        for(int i = 0; vec1.size() != 0; i++){
            vec1.erase(0);
        }
        cout << ((vec1.size()==0)?"True":"False") << endl;
       
        MyVector<int> vec2(1);
        for(int i = 0; i < input.length(); i++){
            vec2.insert(i, i);
        }
        vec2.insert(vec2.size()+100, vec2.size());
        vec2.erase(vec2.size()+100);
        for(int i = 0; i < vec2.size(); i++){
            cout << vec2[i];
        }
        cout << endl;
        int result = 0;
        for(int i = 0; i < vec2.size(); i++){
            result += vec2[i];
        }
        cout << result << endl;
        for(int i = 0; i < vec2.size(); i++){
            vec2[i] += result;
        }
        for(int i = 0; i < vec2.size(); i++){
            cout << vec2[i];
        }
        cout << endl;
        for(int i = 0; vec2.size() != 0; i++){
            vec2.erase(0);
        }
        cout << ((vec2.size()==0)?"True":"False") << endl;
       
        MyVector<string> vec3(1);
        for(int i = 0; i < input.length(); i++){
            vec3.insert(i, input.substr(i, input.length()));
        }
        for(int i = 0; i < vec3.size(); i++){
            cout << vec3[i];
        }
        cout << endl;
        string res;
        for(int i = 0; i < vec3.size(); i++){
            res += vec3[i];
        }
        cout << res << endl;
        for(int i = 0; i < vec3.size(); i++){
            vec3[i] += res;
        }
        for(int i = 0; i < vec3.size(); i++){
            cout << vec3[i];
        }
        cout << endl;
        for(int i = 0; vec3.size() != 0; i++){
            vec3.erase(0);
        }
        cout << ((vec3.size()==0)?"True":"False") << endl;
    }
    return 0;
}

Input :

Don't worry be happy.

Output :

Don't worry be happy.

Sample Input :

Able was I ere I saw elba.
I am your father.

Sample Output :

.able was I ere I saw elbA
able was I ere I saw elbAA
True
Array index out of bounds
01234567891011121314151617181920212223242526
351
351352353354355356357358359360361362363364365366367368369370371372373374375376377
True
Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..
Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..
Able was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..ble was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..le was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..e was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..was I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..as I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..s I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..I ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..ere I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..re I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..e I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..I saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..saw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..aw elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..w elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a.. elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..elba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..lba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..ba.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..a.Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a...Able was I ere I saw elba.ble was I ere I saw elba.le was I ere I saw elba.e was I ere I saw elba. was I ere I saw elba.was I ere I saw elba.as I ere I saw elba.s I ere I saw elba. I ere I saw elba.I ere I saw elba. ere I saw elba.ere I saw elba.re I saw elba.e I saw elba. I saw elba.I saw elba. saw elba.saw elba.aw elba.w elba. elba.elba.lba.ba.a..
True
.rehtaf ruoy ma I
rehtaf ruoy ma II
True
Array index out of bounds
01234567891011121314151617
153
153154155156157158159160161162163164165166167168169170
True
I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..
I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..
I am your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r.. am your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..am your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..m your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r.. your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..your father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..our father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..ur father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..r father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r.. father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..father.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..ather.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..ther.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..her.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..er.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..r.I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r...I am your father. am your father.am your father.m your father. your father.your father.our father.ur father.r father. father.father.ather.ther.her.er.r..
True

Hint :


  

Author :


  Solve it!   Status Forum (0)

C++
C
JAVA
22164. oop97306011 (136 ms , 29666KB)
22129. oop98703029 (136 ms , 29662KB)
22067. oop98703052 (138 ms , 29652KB)
22072. oop98703002 (144 ms , 29664KB)
22112. oop98703049 (148 ms , 29772KB)
沒有解題記錄 沒有解題記錄

執行時間會受很多因素影響因此僅供參考,主機等級請看這裡