Thread: Incrementing Vectors of type int

  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
    Chappell Hill, Texas
    Posts
    2,332
    A "Map" is better suited than a Vector for what you want to do (if all you need to do is count unique occurrences)
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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