check if values are in map

hi im trying to create a program that counts words from a .txt file and any time it encounters a new word it starts counting how many of that word are in the file as well but i can't seem to find a way to do this.
Last edited on
A map comes to mind.

Read word. Check if word is in map. If not, create new entry in map (key = new word) with value zero. If it is in the map, increment the entry with key == word by one.
do you mind showing me an example i haven't worked with maps before each line of the file has one word on it
Last edited on
Something like:

1
2
3
4
5
6
7
8
9
10
map<string, int> theMap;
...
if (theMap.find(word) != theMap.end())
{
  theMap[word]++;
}
else
{
  theMap[word] = 0;
}
thank you i appreciate your help i think this will work i just need to read each line of the .txt file and put the word on that line in the map. i think.
@Repeater
theMap[word] = 0; shouldn't the count then be 1 if it is added?
Sure. Something like.
im having another problem im trying to check that my words are being placed into the map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int main () {
  string line;
  string word;
  ofstream outputFile("test.txt");
  ifstream file ("tim.txt");
  map<string,int> words;
  string it;
  if (file.is_open())
  {
    while ( getline (file,line) )
    {
    words.insert(make_pair(line,1));
    }
    file.close();
  }
  return 0;
}
1
2
3
4
5
6
7
8
if (theMap.find(word) != theMap.end()) // is the string already in the map?
{
  theMap[word]++;
}
else
{
  theMap[word] = 1;
}
With getline you read the whole line, not sure if you really want it.
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <cstdio>

using namespace std;

int main()
{
  map<string, int> words;
  ifstream src("words.txt");
  if (!src)
  {
    perror(nullptr);
    return errno;
  }
  string word;
  while (src >> word)
     words[word]++;

  for (auto& p : words)
  {
    cout << p.first << "\t" << p.second << "\n";
  }
  return 0;
}
i do want to read the whole line there is one word per line in the file but im trying to make sure that my words are getting inserted into the map
i believe i have a working version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;

int main () {
  string line;
  ofstream outputFile("test.txt");
  ifstream myfile ("tim.txt");
  map<string,int> words;
  string it;
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
          if (words.find(line) != words.end()) 
{
  words[line]++;
  cout<<line<<endl;
     cout<<"word exists"<<endl;
}
else
{
  words[line] = 1;
   cout<<line<<endl;
    cout<<"word dosent exist"<<endl;
}
    }
    myfile.close();
  }

  return 0;
}
Last edited on
i now need to print the all of the keys and values from the map to the console if i could get some help with that.
nvm i figured it out
Topic archived. No new replies allowed.