拉丁猪文字游戏

        这是一个英语语言游戏。基本规则是将一个英语单词的第一个辅音音素的字母移动到词尾并且加上后缀-ay(譬如“banana”会变成“anana-bay”)。可以在维基百科上了解更多内容。
        基本的思想是,对于aeiou这五个元音字符装起来,这里不需要序列,感觉使用set集合类挺好的,然后遍历,寻找第一个不属于元音字母的辅音字母,然后给它组合起来,然后连接到字符串的后面组成一个新的字符串。    
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4. using namespace std;
  5. int main(int argc, char const *argv[])
  6. {
  7. set<int> vowel;
  8. vowel.insert('a');
  9. vowel.insert('e');
  10. vowel.insert('i');
  11. vowel.insert('o');
  12. vowel.insert('u');
  13. vowel.insert('A');
  14. vowel.insert('E');
  15. vowel.insert('I');
  16. vowel.insert('O');
  17. vowel.insert('U');
  18. string str; cin>>str;
  19. string item;
  20. string::iterator rIt ;
  21. for (rIt = str.begin(); rIt != str.end(); ++rIt)
  22. {
  23. if (vowel.find(*rIt) == vowel.end())
  24. {
  25. item = *rIt;
  26. str.erase(rIt);
  27. str+="-"+item+"ay";
  28. cout<<str;
  29. break;
  30. }
  31. }
  32. return 0;
  33. }
set集合常用的操作:

begin()        ,返回set容器的第一个元素

end()      ,返回set容器的最后一个元素

clear()          ,删除set容器中的所有的元素

empty()    ,判断set容器是否为空

max_size()   ,返回set容器可能包含的元素最大个数

size()      ,返回当前set容器中的元素个数

rbegin     ,返回的值和end()相同

rend()     ,返回的值和rbegin()相同

如下:

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(1);
  8. s.insert(2);
  9. s.insert(3);
  10. s.insert(1);
  11. cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl;
  12. cout<<"set 中 4 出现的次数是 :"<<s.count(4)<<endl;
  13. return 0;
  14. }
count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(1);
  8. s.insert(2);
  9. s.insert(3);
  10. s.insert(1);
  11. cout<<"set 中 1 出现的次数是 :"<<s.count(1)<<endl;
  12. cout<<"set 中 4 出现的次数是 :"<<s.count(4)<<endl;
  13. return 0;
  14. }
equal_range() ,返回一对定位器,分别表示第一个大于或等于给定关键值的元素和 第一个给定关键值的元素,这个返回值是一个pair类型,如果这一对定位器中哪个返回失败,就会等于end()的值。
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. set<int>::iterator iter;
  8. for(int i = 1 ; i <= 5; ++i)
  9. {
  10. s.insert(i);
  11. }
  12. for(iter = s.begin() ; iter != s.end() ; ++iter)
  13. {
  14. cout<<*iter<<" ";
  15. }
  16. cout<<endl;
  17. pair<set<int>::const_iterator,set<int>::const_iterator> pr;
  18. pr = s.equal_range(3);
  19. cout<<"第一个大于等于 3 的数是 :"<<*pr.first<<endl;
  20. cout<<"第一个大于 3的数是 : "<<*pr.second<<endl;
  21. return 0;
  22. }
erase(iterator)  ,删除定位器iterator指向的值

erase(first,second),删除定位器first和second之间的值

erase(key_value),删除键值key_value的值

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. set<int>::const_iterator iter;
  8. set<int>::iterator first;
  9. set<int>::iterator second;
  10. for(int i = 1 ; i <= 10 ; ++i)
  11. {
  12. s.insert(i);
  13. }
  14. //第一种删除
  15. s.erase(s.begin());
  16. //第二种删除
  17. first = s.begin();
  18. second = s.begin();
  19. second++;
  20. second++;
  21. s.erase(first,second);
  22. //第三种删除
  23. s.erase(8);
  24. cout<<"删除后 set 中元素是 :";
  25. for(iter = s.begin() ; iter != s.end() ; ++iter)
  26. {
  27. cout<<*iter<<" ";
  28. }
  29. cout<<endl;
  30. return 0;
  31. }
find()  ,返回给定值的定位器,如果没找到则返回end()
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. int a[] = {1,2,3};
  7. set<int> s(a,a+3);
  8. set<int>::iterator iter;
  9. if((iter = s.find(2)) != s.end())
  10. {
  11. cout<<*iter<<endl;
  12. }
  13. return 0;
  14. }

insert(key_value); 将key_value插入到set中 ,返回值是pair<set<int>::iterator,bool>,bool标志着插入是否成功,而iterator代表插入的位置,若key_value已经在set中,则iterator表示的key_value在set中的位置

inset(first,second);将定位器firstsecond之间的元素插入到set中,返回值是void.

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. int a[] = {1,2,3};
  7. set<int> s;
  8. set<int>::iterator iter;
  9. s.insert(a,a+3);
  10. for(iter = s.begin() ; iter != s.end() ; ++iter)
  11. {
  12. cout<<*iter<<" ";
  13. }
  14. cout<<endl;
  15. pair<set<int>::iterator,bool> pr;
  16. pr = s.insert(5);
  17. if(pr.second)
  18. {
  19. cout<<*pr.first<<endl;
  20. }
  21. return 0;
  22. }

lower_bound(key_value) ,返回第一个大于等于key_value的定位器

upper_bound(key_value),返回最后一个大于等于key_value的定位器

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. int main()
  5. {
  6. set<int> s;
  7. s.insert(1);
  8. s.insert(3);
  9. s.insert(4);
  10. cout<<*s.lower_bound(2)<<endl;
  11. cout<<*s.lower_bound(3)<<endl;
  12. cout<<*s.upper_bound(3)<<endl;
  13. return 0;
  14. }
总结:使用了set集合元素,set集合元素只能是唯一的,集合元素查找很容易,在使用find() 函数中,如果查找函数找到了元素,那么就返回该元素的的定位器,否则返回end()
字符串的拼接,对于自己的一个挑战是,如何不使用多余定义的字符串就可以将拼接实现。其中char类型和string类型不可以直接的转换。需要进行中间部分的的处理。

 

 











原文地址:https://www.cnblogs.com/clifff/p/5038854.html