Incrementing Vectors of type int

This is a discussion on Incrementing Vectors of type int within the C++ Programming forums, part of the General Programming Boards category; Hello all, So I am teaching myself a little bit off C++, and I am trying to do an exercise ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Incrementing Vectors of type int

    Hello all,
    So I am teaching myself a little bit off C++, and I am trying to do an exercise in which it takes in a user's inputs of strings, and for every distinct string, state how many times it has occurred.

    Example:
    User Input: A B C A BOB
    OUTPUT: A 2
    B 1
    C 1
    BOB 1

    So from what I've coded I have been able to put into a vector all the words that have occurred with no duplicates. I think my way of counting the words work, but the problem is how to increment my vector of int. I have looked at the functions that vectors have, and it doesn't seem like vectors have something for me to replace the element contained in it? With arrays, usually i would just do A[i] = A[i]++. It does not seem to work though with C++ when dealing with vectors...

    Here is my code.
    Code:
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <conio.h>
    
    using std::cin;
    using std::sort;
    using std::cout;
    using std::streamsize;
    using std::endl;
    using std::string;
    using std::setprecision; 
    using std::vector;
    
    int main(){
    
        // ask for set of words
        cout << "Enter all words, "
              "followed by end-of-file: ";
        vector<string> word_set;
        string x;
    
        // invariant: word_set contains all the words entered so far
        while (cin >> x)
              word_set.push_back(x);
    
       // check that the user entered some words
       typedef vector<string>::size_type vec_sz;
       vec_sz size = word_set.size();
       if (size == 0) {
          cout << endl << "You must enter some words. "
                          "Please try again." << endl;
          cout << "Press any key to continue... ";
          _getch();
          return 1;
          }
    
          vector<string> words;
          vector<int> occurs;
          
          //adds new words to the the words vector
          for(int i = 0; i != word_set.size(); i++){
                  bool listed = false;
                  for(int i2 = 0; i2 != words.size(); i2++){
                          if(words[i2] == word_set[i]){
                                       listed = true;
                                       }
                          }
                  if(listed == false){
                           words.push_back(word_set[i]);
                           }
                  }
                  
          //initializes occurs vectors with the same size as words
          //each element is set to 0
          for(int i = 0; i != words.size(); i++){
                  occurs.push_back(0);
                  }
          
          //records occurences of words
          for(int i = 0; i != words.size(); i++){
                  for(int i2 = 0; i2 != word_set.size(); i2++){
                          if(word_set[i2] == words[i]){
                                          //PROBLEM HERE.....
                                          occurs[i] = occurs[i]++;
                                          }
                          }
                  }
                  
          //writes out the words
          cout << "Your words for the dataset are: " << endl;
               for(int i = 0; i != words.size(); i++){
                       cout << words[i] << " "
                            << occurs[i] << endl;
                       }
          //for the purpose of not quitting out the cmd
          //due to my compiler
          cout << "Press any key to continue... ";
       _getch();
       return 0;
    }
    Thanks all!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    A "Map" is better suited than a Vector for what you want to do (if all you need to do is count unique occurrences)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Well so far...I have only been taught Vectors from the book...is there a way of solving this with vectors?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You can create a struct (or a Class if you prefer) that contains the string and the count, and then add that to your Vector.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by dnguyen1022
    So from what I've coded I have been able to put into a vector all the words that have occurred with no duplicates. I think my way of counting the words work, but the problem is how to increment my vector of int. I have looked at the functions that vectors have, and it doesn't seem like vectors have something for me to replace the element contained in it? With arrays, usually i would just do A[i] = A[i]++. It does not seem to work though with C++ when dealing with vectors...
    Instead of writing this:
    Code:
    occurs[i] = occurs[i]++;
    write:
    Code:
    ++occurs[i];
    Note that this applies even if you are working with an array instead of a vector. Your original code results in undefined behaviour.

    That said, a std::map is probably a better solution here. I posted such an example program recently.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    oh wow. thanks, that was a simple fix. I'll definitely give the map method a shot though. I'll come back to here if I have any problems. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

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