Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. /* Exemplo 1 */
  2. #include <iostream>
  3. #include <map>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     map<string,int> m;
  9.     m["teste"] = 0;
  10.     m["abcde"] = 12345;
  11.  
  12.     cout << "m[\"teste\"] = " << m["teste"] << endl;
  13.     cout << "m[\"abcde\"] = " << m["abcde"] << endl;
  14.     cout << "m[\"naoexiste\"] = " << m["naoexiste"] << endl;
  15.  
  16.     if (m.find("teste") != m.end())
  17.         cout << "Encontrado m[\"teste\"] = " << m.find("teste")->second << endl;
  18.     else
  19.         cout << "Nao encontrado m[\"teste\"]" << endl;
  20.  
  21.     if (m.find("naotem") != m.end())
  22.         cout << "Encontrado m[\"naotem\"] = " << m.find("naotem")->second << endl;
  23.     else
  24.         cout << "Nao encontrado m[\"naotem\"]" << endl;
  25.  
  26.     m.erase(m.begin());
  27. }
  28.  
  29. /* Exemplo 2 */
  30. #include <iostream>
  31. #include <map>
  32. using namespace std;
  33.  
  34. int main()
  35. {
  36.     map<string,int> m;
  37.     string s;
  38.     while (cin >> s)
  39.         m[s]++;
  40.  
  41.     for (auto palavra : m)
  42.         cout << palavra.first << " : " << palavra.second << endl;
  43. }
  44.  
  45. /* Exemplo 3 */
  46. #include <iostream>
  47. #include <vector>
  48. #include <map>
  49. #include <functional>
  50. #include <algorithm>
  51. using namespace std;
  52.  
  53. int main()
  54. {
  55.     map<string,int> m;
  56.     string s;
  57.     while (cin >> s)
  58.         m[s]++;
  59.  
  60.     vector<pair<int,string>> v;
  61.     for (auto palavra : m)
  62.         v.push_back(make_pair(palavra.second,palavra.first));
  63.     sort(v.begin(),v.end(),greater<pair<int,string>>());
  64.  
  65.     for (auto palavra : v)
  66.         cout << palavra.second << " : " << palavra.first << endl;
  67. }
  68.  
  69. /* Exemplo 4 */
  70. #include <iostream>
  71. #include <map>
  72. using namespace std;
  73.  
  74. int contaVogais(string s)
  75. {
  76.     int quantidade = 0;
  77.     for (auto c : s)
  78.         if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
  79.             quantidade++;
  80.     return quantidade;
  81. }
  82.  
  83. int main()
  84. {
  85.     map<string,int> m;
  86.     string s;
  87.     int totalPalavras = 0;
  88.     int totalCalculo = 0;
  89.  
  90.     while (cin >> s)
  91.     {
  92.         if (m.find(s) == m.end())
  93.         {
  94.             m[s] = contaVogais(s);
  95.             totalCalculo++;
  96.         }
  97.         totalPalavras++;
  98.     }
  99.     cout << totalPalavras << " " << totalCalculo << endl;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement