Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Exemplo 1 */
- #include <iostream>
- #include <map>
- using namespace std;
- int main()
- {
- map<string,int> m;
- m["teste"] = 0;
- m["abcde"] = 12345;
- cout << "m[\"teste\"] = " << m["teste"] << endl;
- cout << "m[\"abcde\"] = " << m["abcde"] << endl;
- cout << "m[\"naoexiste\"] = " << m["naoexiste"] << endl;
- if (m.find("teste") != m.end())
- cout << "Encontrado m[\"teste\"] = " << m.find("teste")->second << endl;
- else
- cout << "Nao encontrado m[\"teste\"]" << endl;
- if (m.find("naotem") != m.end())
- cout << "Encontrado m[\"naotem\"] = " << m.find("naotem")->second << endl;
- else
- cout << "Nao encontrado m[\"naotem\"]" << endl;
- m.erase(m.begin());
- }
- /* Exemplo 2 */
- #include <iostream>
- #include <map>
- using namespace std;
- int main()
- {
- map<string,int> m;
- string s;
- while (cin >> s)
- m[s]++;
- for (auto palavra : m)
- cout << palavra.first << " : " << palavra.second << endl;
- }
- /* Exemplo 3 */
- #include <iostream>
- #include <vector>
- #include <map>
- #include <functional>
- #include <algorithm>
- using namespace std;
- int main()
- {
- map<string,int> m;
- string s;
- while (cin >> s)
- m[s]++;
- vector<pair<int,string>> v;
- for (auto palavra : m)
- v.push_back(make_pair(palavra.second,palavra.first));
- sort(v.begin(),v.end(),greater<pair<int,string>>());
- for (auto palavra : v)
- cout << palavra.second << " : " << palavra.first << endl;
- }
- /* Exemplo 4 */
- #include <iostream>
- #include <map>
- using namespace std;
- int contaVogais(string s)
- {
- int quantidade = 0;
- for (auto c : s)
- if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
- quantidade++;
- return quantidade;
- }
- int main()
- {
- map<string,int> m;
- string s;
- int totalPalavras = 0;
- int totalCalculo = 0;
- while (cin >> s)
- {
- if (m.find(s) == m.end())
- {
- m[s] = contaVogais(s);
- totalCalculo++;
- }
- totalPalavras++;
- }
- cout << totalPalavras << " " << totalCalculo << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement