Esempio n. 1
0
  int main(int argc, char** argv) {
//Check input.
     if (argc < 3)
        return -1;

     std::ifstream in(argv[1]);

     if (!in)
        return -1;


     StrIntMap w;
// Count the occurence of each word and store the result in the map w.
     countWords(in, w);

     std::ofstream myfile;
     myfile.open(argv[2]);
     
// Output the occurence of each word from the map (alphabetically).
     for (StrIntMap::iterator p = w.begin();p != w.end(); ++p) {
        myfile << p->first << "\t" << p->second << "\n";
     }

     myfile.close();
     return 0;
  }
Esempio n. 2
0
int main(void)
{
  typedef std::map <std::string, int> StrIntMap;
  StrIntMap months;
  
  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;
 
  StrIntMap const & m = months;

  StdMapIteratorAdaptor <StrIntMap::const_iterator> begin (m.begin());
  StdMapIteratorAdaptor <StrIntMap::const_iterator> end (m.end());
  std::copy(begin, end, std::ostream_iterator <int> (std::cout, " "));
  std::cout << std::endl;
  
  std::list<int> l(std_map_iterator_adaptor(m.begin()), 
                   std_map_iterator_adaptor(m.end()));

  std::copy (l.begin(), l.end(), std::ostream_iterator <int> (std::cout, " "));
  std::cout << std::endl;
  std::copy (std_map_iterator_adaptor(months.begin()), 
             std_map_iterator_adaptor(months.end()), 
             std::ostream_iterator <int> (std::cout, " "));
  std::min_element (std_map_iterator_adaptor(months.begin()),
                    std_map_iterator_adaptor(months.end()));
    
  return 0;
}
Esempio n. 3
0
//-------------挿入ソートを用い、wの頻度上位単語freq個をnumber_of_word[]に格納-
void InsertSort(StrIntMap& w,   StrIntMap::iterator number_of_word[], int freq){
 
  StrIntMap::iterator p = w.begin();//wの先頭freq個の単語をnumber_of_wordsに格納
  for(int i = 0 ; i<freq ; i++){    //初期化がわり。   
    number_of_word[i] = p;
    p++;
  }

  for (StrIntMap::iterator p = w.begin(); p != w.end(); ++p){
    int s = p->second;                     //sはpのさす単語の頻度。
	
    for(int i = 0; i < freq; i++){        //頻度ランキングを順番に。
      if(s > number_of_word[i]->second){  //もしランキング内より頻度高ければ挿入発生
                 
	for(int j = freq-1; j > i; j--){ //2位のものは3位へ、3位のものは4位へずれていく。。
	  number_of_word[j] = number_of_word[j-1];	  

	}
	number_of_word[i] = p;//ずらし終えたら挿入
	break;//挿入したら次の単語へ。breakしないと複数回挿入してしまう。
      }
    }
  }
}
int main(int argc, char **argv) {

    if (argc < 2)
        return (EXIT_FAILURE);

    std::ifstream in(argv[1]);

    if (!in)
        exit(EXIT_FAILURE);

    StrIntMap w;
    countWords(in, w);

    for (StrIntMap::iterator p = w.begin();
         p != w.end(); ++p) {
        std::cout << p->first << " occurred "
        << p->second << " times.\n";
    }
}