-1

Here is the Situation:

I am taking sting as a input using this:

string s;
getline (cin , s);

Now I want to fill a map of type <string, int>. The key of this map will be individual words of the input string. The value will be storing the frequency of the word.

Example : input string - " Hello My name is OP Hello World"

Map Should be like:

Hello - 2

My - 1

name - 1

is - 1

OP - 1

World - 1

The method which i know is using string manipution to divide string into an array of seperate words.

Is there any other efficient way to split a string into array of words and fill in the map with word as a key?

Snake Eyes
  • 325
  • 5
  • 18
  • Assuming the input is space delimited, just read each word one at a time and add it to the map. If this isn't what you need maybe you could clarify your question. – James Adkison Jul 01 '16 at 19:11
  • *The question as you've written it is unclear.* If you're using `std::string` as a key, what is `int` storing? A count of the number of times that word appears? The order in which the word first appears? You need to provide more information. – Xirema Jul 01 '16 at 19:11
  • Made changes as suggested by u all – Snake Eyes Jul 01 '16 at 19:19
  • 1
    @FirstStep You might be right on one side of that. I'm sure there are plenty of good libraries people could recommend to handle string splitting with various delimiters, charsets, languages, etc. However, yeah, I'm pretty sure that would be closed as too opinion-based. SO generally avoids 'recommend an X' questions. However, CodeReview wouldn't be the right place for such a question either. In both cases, code is needed, not just an idea of what someone would like to do with some other, suggested code. – underscore_d Jul 01 '16 at 19:20
  • @SnakeEyes You're still missing the [MCVE]. – πάντα ῥεῖ Jul 01 '16 at 20:35

4 Answers4

0
  1. You will need to split the input string into substring.
  2. Find the number of occurrences of each substring in the entered string.
  3. Store the substring as key and count as a value in the map.

int main()
{
    std::string input="Hello My name is OP Hello World";
    std::map<std::string, int> myMap;
    std::istringstream iss(input);
    while (iss) {
        std::string substr;
        std::getline(iss,substr,' ');
        int count = 0;
        auto pos = input.find(substr, 0);
        while (pos != std::string::npos) {
            ++count;
            pos = input.find(substr, pos + 1);
        }
        if(substr.size() != 0)
            myMap[substr] = count;
    }
    for (const auto &p : myMap) {
        std::cout << p.first << "=" << p.second << '\n';
    }
    return 0;
}

Output

Hello=2
My=1
OP=1
World=1
is=1
name=1
ani627
  • 5,578
  • 8
  • 39
  • 45
-1

So you said you have your string splitted but you would like to find a better way so here is the most elegant way for string spliting in c++ Split a string in C++, assuming that your words are seperated with Whitespaces.

Now you have your words, get your words and iterate all of them using a loop, and use this command inside your loop:

WhateverYourMapName[WhateverTheCurrentWordIs]++;

This statement will increment the value of your existing key (word) by 1. OR it will add a new key(word) if it was not found, with an initial value of 1.

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
-2

If you have a function for splitting a std::string object into an array of strings based on a delimiter, then your task is actually quite simple:

std::vector<std::string> lines;
std::string line;
while(std::getline(std::cin, line)) lines.emplace_back(line);
std::map<std::string, int> word_map;
for(const std::string & line : lines_of_input) {
    //my_split_method can either be a standard library function, if one exists, or a function you've written yourself
    std::vector<std::string> words = my_split_method(line);
    for(const std::string & word : words) {
        word_map[word]++;
    }
}

for(const auto & word_pair : word_map) {
    std::cout << "Frequency of \"" << word_pair.first << "\" is " << word_pair.second << std::endl;
}
Xirema
  • 19,889
  • 4
  • 32
  • 68
  • If there's a problem with this answer, then explain it, instead of just downvoting. – Xirema Jul 01 '16 at 19:26
  • First of all, I am not the one who down voted your answer, Second the question is to split a string into array of its constituent words. – Snake Eyes Jul 01 '16 at 19:29
  • @SnakeEyes From your post: *"The method which i know is using string manipution to divide string into an array of seperate words."* implies that you already have the algorithm which would allow you to do that. Is that not the case? – Xirema Jul 01 '16 at 19:30
  • @SnakeEyes Define "words". Separated by spaces? hyphens? em dashes? something else? Which characters should and should not be considered part of a word? – underscore_d Jul 01 '16 at 19:30
  • @underscore_d separated by spaces – Snake Eyes Jul 01 '16 at 19:31
-3

Use strtok to split the white spaces and iterate the words, check each if the word is in the map by find and assign it into the map or change it's value's value.

K. Orel
  • 1
  • 2
  • 1
    `strtok` is a C function for destroying, um I mean splitting null-terminated `char` arrays. It wasn't very well-liked there anyway. However, it's completely irrelevant to C++ with `std::string`. It just won't work. Even if it did somehow, the sheer clunkiness and often danger of dealing with raw strings in C is why `std::string` exists in C++. So this answer is wrong both technically and in terms of good C++ philosophy. – underscore_d Jul 01 '16 at 19:46
  • You can strengthen that to can't work, without assumptions being made about undefined behaviour. C++17 may change this to merely won't work with a non-`const` `data` method. – user4581301 Jul 01 '16 at 19:51