Words Words

Discussion in 'C++' started by utab, Feb 16, 2006.

  1. utab

    utab Guest

    Dear all,

    I am trying to count the occurence of words in an input stream I can
    keep my words in a vector<string> as below;

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <iomanip>
    #include <string>
    #include <cstdlib>

    using std::cout; using std::cin;
    using std::vector; using std::endl;
    using std::setprecision; using std::setw;
    using std::max; using std::string;

    int main(){
    vector<string> str;
    vector<string>::size_type sz;
    string x;
    while(cin >> x){
    str.push_back(x);
    }
    sz=str.size();
    for(vector<string>::size_type i=0; i!=sz; i++){
    cout << str << endl;
    }
    return 0;
    }

    Now I think that sorting and finding the occurences are easier so sort
    them alphabetically and then find the occurences. But the occurences
    causing problems in the algorithms I try on paper. Please DO NOT USE
    LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
    Can someone give me any ideas on this?

    Thx.
    utab, Feb 16, 2006
    #1
    1. Advertising

  2. utab

    Daniel T. Guest

    In article <>,
    "utab" <> wrote:

    > Dear all,
    >
    > I am trying to count the occurence of words in an input stream I can
    > keep my words in a vector<string> as below;
    >
    > #include <iostream>
    > #include <cmath>
    > #include <vector>
    > #include <iomanip>
    > #include <string>
    > #include <cstdlib>
    >
    > using std::cout; using std::cin;
    > using std::vector; using std::endl;
    > using std::setprecision; using std::setw;
    > using std::max; using std::string;
    >
    > int main(){
    > vector<string> str;
    > vector<string>::size_type sz;
    > string x;
    > while(cin >> x){
    > str.push_back(x);
    > }
    > sz=str.size();
    > for(vector<string>::size_type i=0; i!=sz; i++){
    > cout << str << endl;
    > }
    > return 0;
    > }
    >
    > Now I think that sorting and finding the occurences are easier so sort
    > them alphabetically and then find the occurences. But the occurences
    > causing problems in the algorithms I try on paper. Please DO NOT USE
    > LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
    > Can someone give me any ideas on this?
    >
    > Thx.


    Try using a std::map instead, and check out the bottom of my response to
    you in the "occurence problem" thread.


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 16, 2006
    #2
    1. Advertising

  3. utab

    Rolf Magnus Guest

    utab wrote:

    > Dear all,
    >
    > I am trying to count the occurence of words in an input stream I can
    > keep my words in a vector<string> as below;
    >
    > #include <iostream>
    > #include <cmath>
    > #include <vector>
    > #include <iomanip>
    > #include <string>
    > #include <cstdlib>
    >
    > using std::cout; using std::cin;
    > using std::vector; using std::endl;
    > using std::setprecision; using std::setw;
    > using std::max; using std::string;
    >
    > int main(){
    > vector<string> str;
    > vector<string>::size_type sz;
    > string x;
    > while(cin >> x){
    > str.push_back(x);
    > }
    > sz=str.size();
    > for(vector<string>::size_type i=0; i!=sz; i++){
    > cout << str << endl;
    > }
    > return 0;
    > }
    >
    > Now I think that sorting and finding the occurences are easier so sort
    > them alphabetically and then find the occurences. But the occurences
    > causing problems in the algorithms I try on paper.


    I would use std::map<std::string, int> instead of a vector.

    > Please DO NOT USE LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE
    > SOLUTIONS.


    Why not?
    Rolf Magnus, Feb 16, 2006
    #3
  4. utab

    utab Guest

    I am following a book and I have not covered map yet. I asked a close
    friend also he recommended map. But I first would like to find the
    answer this way. If can't then dive into map.

    thanks for the quick replies
    utab, Feb 16, 2006
    #4
  5. utab

    Daniel T. Guest

    In article <>,
    "utab" <> wrote:

    > Dear all,
    >
    > I am trying to count the occurence of words in an input stream I can
    > keep my words in a vector<string> as below;
    >
    > #include <iostream>
    > #include <cmath>
    > #include <vector>
    > #include <iomanip>
    > #include <string>
    > #include <cstdlib>
    >
    > using std::cout; using std::cin;
    > using std::vector; using std::endl;
    > using std::setprecision; using std::setw;
    > using std::max; using std::string;
    >
    > int main(){
    > vector<string> str;
    > vector<string>::size_type sz;
    > string x;
    > while(cin >> x){
    > str.push_back(x);


    Don't load every word into the string, only unique words. (ie: if x not
    in str, push_back x.

    Once you do the above, post your code and I'll help you more.

    > }
    > sz=str.size();
    > for(vector<string>::size_type i=0; i!=sz; i++){
    > cout << str << endl;
    > }
    > return 0;
    > }
    >
    > Now I think that sorting and finding the occurences are easier so sort
    > them alphabetically and then find the occurences. But the occurences
    > causing problems in the algorithms I try on paper. Please DO NOT USE
    > LISTS, VECTOR.ERASE(), ITERATORS IN THE PROBABLE SOLUTIONS.
    > Can someone give me any ideas on this?



    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 16, 2006
    #5
  6. utab

    utab Guest

    How to control that is not that easier to sort and apply. I am now
    trying that but still problems with the algoritm I have created two
    more vectors. One holding the words and the other holding the repeat
    times corresponding to the words. Code is wrong but I am sending it
    anyway. I think you will understand what I try to do

    Thx,

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <iomanip>
    #include <string>
    #include <cstdlib>

    using std::cout; using std::cin;
    using std::vector; using std::endl;
    using std::setprecision; using std::setw;
    using std::max; using std::string;

    bool compare(const string & str1 ,const string & str2 ){

    return str1 < str2;

    }

    void count(const vector<string> & S, vector<string> & ss,vector<int>
    &nn){

    int cnt=1;
    vector<string>::size_type i=0;
    string compare(S);
    // problems with this while loop

    while(i != S.size()){

    if(S[i+1]==compare)
    cnt++;
    else{
    compare=S[i+1];
    if(S[i+1]==compare)
    cnt++;
    }
    ss.push_back(compare);
    nn.push_back(cnt);
    i++;
    }

    }

    int main(){

    vector<string> str;
    vector<string>::size_type sz;
    vector<string> s;
    vector<int> n;

    string x;

    while(cin >> x){

    str.push_back(x);

    }

    sz=str.size();

    for(vector<string>::size_type i=0; i!=sz; i++){

    cout << str << endl;

    }

    sort(str.begin(),str.end(),compare);

    for(vector<string>::size_type i=0; i!=sz; i++){

    cout << str << endl;

    }
    count(str,s,n);

    for(vector<string>::size_type i=0; i!=sz; i++){

    cout << s << ' ' << n << endl;

    }

    return 0;
    }
    utab, Feb 16, 2006
    #6
  7. utab

    Daniel T. Guest

    In article <>,
    "utab" <> wrote:

    > How to control that is not that easier to sort and apply.


    No, it isn't easer.

    > I am now
    > trying that but still problems with the algoritm I have created two
    > more vectors. One holding the words and the other holding the repeat
    > times corresponding to the words. Code is wrong but I am sending it
    > anyway. I think you will understand what I try to do


    You need to start smaller, and work your way up. Paste the code below
    into your cpp file (overwrite what you have) and then run the program.
    What you are supposed to see is "working" what you will actually see is
    some error message. Put code in the "insert your code here" spot and
    keep running the program until you can get "working" to appear on the
    screen. Once you do that, I'll take you to the next step.

    @begin code

    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <iomanip>
    #include <string>
    #include <cstdlib>

    using std::cout; using std::cin;
    using std::vector; using std::endl;
    using std::setprecision; using std::setw;
    using std::max; using std::string;

    void count_words( const vector<string>& S, vector<string>& ss,
    vector<int>& nn )
    {
    // insert code here
    }

    int main() {
    {
    vector<string> in;
    in.push_back( "word" );
    vector<string> str;
    vector<int> count;
    count_words( in, str, count );
    assert( str.size() == 1 );
    assert( count.size() == 1 );
    assert( str[0] == "word" );
    assert( count[0] == 1 );
    }
    {
    vector<string> in;
    in.push_back( "word" );
    in.push_back( "big" );
    vector<string> str;
    vector<int> count;
    count_words( in, str, count );
    assert( str.size() == 2 );
    assert( count.size() == 2 );
    assert( str[0] == "word" );
    assert( count[0] == 1 );
    assert( str[1] == "big" );
    assert( count[1] == 1 );
    }
    cout << "working";
    }

    @end code

    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 16, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Strøiman
    Replies:
    1
    Views:
    2,070
    Peter Strøiman
    Aug 23, 2005
  2. Richard Heathfield
    Replies:
    7
    Views:
    350
    Barry Schwarz
    Oct 5, 2003
  3. BerlinBrown
    Replies:
    6
    Views:
    4,430
  4. Lasse Edsvik

    replace words with bold words

    Lasse Edsvik, Oct 5, 2003, in forum: ASP General
    Replies:
    9
    Views:
    228
  5. pantagruel
    Replies:
    8
    Views:
    422
    Dr John Stockton
    Jul 22, 2006
Loading...

Share This Page