Help with printing out a vector, and more.

Hey!
I am trying to write a program that will sort the words in a text after how many times it appears. I am currently having a lot of trouble with it. So far i managed to open the text file and put the words in map and assign a value based on how many times the word appeared. Then i managed to print these out however they where not sorted and the words was not correct either since it was only looking for "space" between words.

Then i moved the words with their value to form "pair" in a vector. I also added a sort function that i think/hope works but i have no idea how to check because i cant for the life of me print the vector.

So what i need help with is a bit of code to print the vector and also som hints/suggestions on where to go from here.

Thanks!

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
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

ifstream book;
string tempbook;
map<string, int> words;
vector<pair<string, int> > wordsVec;

bool sorting(const pair<string, int>& firstElem, const pair<string, int>& secondElem)
{
  return firstElem.first() < secondElem.first();
}

int main()
{
  book.open("hitchhikersguide.txt");

  if(!book.is_open())
    cout << "Filen öppnades inte korrekt." << endl;

  while (book >> tempbook)
  {
    if (words.find(tempbook) == words.end())
      words[tempbook] = 1;
    else
      words[tempbook]++;
  }

  for (map<string, int>::iterator it=words.begin(); it!=words.end(); ++it)
  {
    wordsVec.push_back(*it);
  }
  sort(wordsVec.begin(), wordsVec.end(), sorting);

  return 0;

}
Well, for one, you can simplify the loop on line 35:

1
2
3
4
for(auto e : words)
{
    wordsVec.push_back(e);
}


which should work unless I've forgotten how for-each loops work (hopefully I haven't). Try and see. Printing the vector would work much the same way:

1
2
3
4
for(auto e : wordsVec)
{
    cout<<"Word: " << e.first << " Int: " << e.second;
}


Again though, test what I wrote. It should work.
Here is a slightly different example:
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>
#include <vector>

using namespace std;

typedef pair<string, int> WordInfo;

vector<WordInfo> v;

void PrintVector(vector<WordInfo> &v);

void main(void)
{
  v.push_back(make_pair("Anna", 3));
  v.push_back(make_pair("Lisa", 1));
  v.push_back(make_pair("Jenna",2));
  v.push_back(make_pair("Anja", 5));
  v.push_back(make_pair("Cassandra", 0));

  PrintVector(v);
  cin.get();
}

void PrintVector(vector<WordInfo> &v)
{
  vector<WordInfo>::iterator it;

  for (it = v.begin(); it != v.end(); ++it)
  {
    cout << it->first << " => " << it->second << endl;
  }
}

@Ispil
which should work unless I've forgotten how for-each loops work


unless he uses a compliler that is not fully c++11 compliant.
Topic archived. No new replies allowed.