Q:

Sorting Words from a input text depending on how many times it was repeated?

I am trying to make a Program that reads an input text from the user and then  prints out each word and how many times it was repeated in a sorted order. ( the input is separated by white spaces and it can hold punctuation but no new lines)
Example of Input : I am a good programmer and I love C language.
output should be : 

a : 2 times
                   I : 1 time
                 am : 1 time
                 good : 1 time
... and so on.

** for the input I used a vector and classes  but i don't know why i can't push several items in it.
class word {public:
word();
void set_data(string data_value);
void printing_data();
void counter_addition();
int count;
string data;};

int main(){ /*creating vecotr called items of list class type*/
vector<word>items;

/*creating a variable of list class type which is used to push data into the vector*/

word *element;

/*Reading the input text seperated by white spaces*/
int size = 0;
string text;

cout << "Enter the text: " << endl;
getline(cin, text);

size = text.length();
int left_space = -1;
int right_space = 0;
string Sub_String;

/*main loop for constructing substring of words and then manipulating them*/

for (int i = 0; i<size; i++)
{

    /*splitting the string into words*/
    if (isspace(text[i]) || ispunct(text[i]))
    {
        right_space = i;

        Sub_String = text.substr(left_space + 1, right_space - left_space - 1);
        /*for first word just push it*/

        if (left_space == -1)
        {
            element = new word();
            element->set_data(Sub_String);
            items.push_back(*element);

        }

        else
        {
            /*compare to vector's data */
            for (int j = 0; j < items.size(); j++)
            {
                if (Sub_String == items[j].data)
                {
                    items[j].count = items[j].count + 1;
                }
                else
                {
                    element = new word();
                    element->set_data(Sub_String);
                    items.push_back(*element);
                }

            }
        }
            left_space = right_space;
    }

if the words are the same the output is correct .

input : Life life life life

output: Life repeated:4

Please help me through this I am new to programming .

A:

Store the string values and count into std::map< std::string, int > and for each word, check if it is already in the map. If it is there, increment the count. In opposite case insert it with count = 1

