Multidimentional vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amscompit
    New Member
    • Feb 2008
    • 6

    Multidimentional vectors

    I have been trying to write a concordance program using vectors. rite now, i am upto the part where i m able to count each word. Now i have to locate the first and last occurence of each word in a document. How do i do tat using vectors, and if answer is multidimensiona l vectors, how do i do it? Can anybody help me plzz..thnx

    Here is my code-
    [code=cpp]
    class Concordance
    {
    private:
    int wordcount;
    vector <string> v1; //vector of words

    vector<string> v;


    typedef vector<string>: :size_type vec_size;
    string token;
    string lines;
    int pos; //position of a word
    int linenumber;
    string word;
    public:
    void print();
    //char upperCase(); //function to search letters in uppercase
    //char lowerCase(); //function to search letters in lowercase
    void getlines();

    };

    void Concordance::ge tlines()
    {
    ifstream fin("mytext.txt ");

    while(getline(f in, lines))
    v.push_back(lin es);

    // Print document;
    int nlines = v.size();
    for(int i = 0,lineno = 0; i < nlines, lineno < nlines; i++, lineno++) {

    cout << lineno << ". " << v[i] << endl;
    cout<<endl;

    }


    }



    void Concordance::pr int()
    {
    ifstream fin("mytext.txt ");
    //ofstream fout("myoutput. txt");




    while( fin>>token)
    {
    v1.push_back(to ken);
    }


    struct WordEntry {
    vector<string> word;
    vector<int> count;

    };
    WordEntry WordCount;

    // take each word in-order and compare
    // it with the next words in the list
    sort(v1.begin() , v1.end());
    int count = 1;


    for (vec_size i=0; i < v1.size(); ++i) {
    for (vec_size j=i+1; j < v1.size(); ++j) {
    if (v1[i] == v1[j]){
    count++; //
    ++i; // jumps to next unique word
    }


    }



    WordCount.word. push_back(v1[i]);

    WordCount.count .push_back(coun t);

    count = 1;

    }

    // print out results
    for (vec_size i = 0; i < WordCount.word. size(); ++i){

    cout << WordCount.word[i] <<"-count:" << WordCount.count[i] <<setw(5)<<"Lin enumber:"<< endl;
    cout<<endl;
    //fout<<WordCount .len[i]<<":"<<WordCoun t.lencount[i]<<endl;
    }

    //fout.close();

    }[/code]
    Last edited by sicarie; Feb 27 '08, 06:56 PM. Reason: Code tags are [code=cpp] and after your code [/code]. Please use them. They want to be used. They like to be used.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use the equal_range() algorithm.

    No point in figuring this out all over again.

    That algorithm will return a pair of iterators to the first and last occurrence of the target word.

    Comment

    • amscompit
      New Member
      • Feb 2008
      • 6

      #3
      Originally posted by weaknessforcats
      Use the equal_range() algorithm.

      No point in figuring this out all over again.

      That algorithm will return a pair of iterators to the first and last occurrence of the target word.
      Isnt there other way to do it instead of algorithm? I have never used algorithm

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Of course, you don't have to use an algorithm.

        You can do all the work yourself.

        I am only suggesting that if you spent a little time, you could solve your problem in 3 lines of code.

        Comment

        Working...