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]
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]
Comment