Creating my own vector class using a statically allocated array

Hi,
For my school assignment I have been instructed to write code that takes a text file and counts either the total number of words or the number of unique words, depending on the optind variable which is controlled by the getopt function. TO reach a given checkpoint I must exclude the statement #include <vector>. and create a template class which uses a statically allocated array to perform the same function.

The skeleton code for this template code is shown below. Can anybody tell me how I edit this code to make this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#ifndef VECTOR_H
#define	VECTOR_H

template<class T>
class Vector{
public:
    typedef T* iterator;
    Vector() {
        
        
    }
    iterator begin(){}
    iterator end(){}
    int size() {}
    iterator insert(iterator position, const T& item){}
private:
    T items[1000];
    int used;
};


The main method of the code is as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main(int argc, char** argv) {

    enum {
        total, unique
    } mode = total;
    for (int c; (c = getopt(argc, argv, "tu")) != -1;) {
        switch (c) {
            case 't': mode = total;
                break;
            case 'u': mode = unique;
                break;
        }
    }
    argc -= optind;
    argv += optind;
    string word;
    vector<string> words;
    int count = 0;

    while (cin >> word) {
        count += 1;
        if (words.size() == 0) {
            words.push_back(word);
        }

        vector<string>::iterator i;
      
            for ( i = words.begin(); i != words.end(); ++i) {

                if (word == *i) {
                    break;
                }
               
                }
                if (i == words.end()) {
                    words.push_back(word);
                }
            }
        
    
    


        switch (mode) {
            case total: cout << "Total: " << count << endl;
                break;
            case unique: cout << "Unique: " << words.size() << endl;
                break;
        }
    

    return 0;

}

Last edited on
Topic archived. No new replies allowed.