zoukankan      html  css  js  c++  java
  • 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082

    输入一个字符串,将其中特定的单词替换成另一个单词

      代码注意点:

    1. getline(istream &in, string &s)

    • 没有读入字符,将返回false

    2. transform(tmp.begin(),tmp.end(),tmp.begin(),::tolower)

    • 将tmp的所有字母都改成小写
    • 需要包含 #include <algorithm>

    3. 查找函数:size_t find(const string& str, size_t pos = 0) const

    • 用法:string s, t; s.find(t, 0); 从s的0位置开始查找t是不是出现在s中
    • 返回出现t在s中出现的位置。如果没有找到,则返回 string::npos

    4. 替换函数:string& replace(size_type pos(开始替换的位置),size_type cnt(替换的个数), const basic_string str(用来替换的字符串))

    • 用法:string s, t; s.replace(0, 3, t); 从pos开始删除cnt个字母,然后在pos位置插入str
    • 返回替换后的字符串

      源码  

     1 #include <iostream>
     2 #include<string>
     3 #include <vector>
     4 #include <algorithm>  
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     string str;
    10     string temp;;
    11     string target="marshtomp";
    12     string Des="fjxmlhx";
    13     string ::size_type tLength=target.length();
    14     string ::size_type dLength=Des.length();
    15     vector<string> output;
    16     //getline(cin,str);
    17     while (getline(cin,str) )
    18     {
    19         temp=str;
    20         //转换成小写    
    21                 transform(temp.begin(),temp.end(),temp.begin(),::tolower);
    22         string ::size_type pos=0;
    23         //可能有多处匹配成功的位置!!!
    24         while ( (pos=temp.find(target,pos))<str.length())
    25         {
    26             str.replace(pos,tLength,Des);
    27             temp.replace(pos,tLength,Des);
    28             pos+=dLength;
    29         }
    30         output.push_back(str);            
    31     }
    32     for (vector<string>::size_type i=0;i<output.size();i++)
    33         cout<<output[i]<<endl;
    34     return 0;
    35 }
    View Code
  • 相关阅读:
    linux环境下安装nginx步骤
    时间戳—时间互转 java
    redis配置中踩过的坑
    在Windows端安装kafka 提示错误: 找不到或无法加载主类 的解决方案
    Windows平台kafka环境的搭建
    在windows上搭建redis集群(redis-cluster)
    身份证号打码隐藏
    PIL获取图片亮度值的五种方式
    Python文件排序
    PIL
  • 原文地址:https://www.cnblogs.com/coolqiyu/p/5904296.html
Copyright © 2011-2022 走看看