字符串专题

1. double ceil(double x)

求大于 x 的最小的数,即向上取整函数

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n,m,a;
    cin>>n>>m>>a;
    long long s=ceil(m*1.0/a)*ceil(n*1.0/a);
    //或写为
    //long long s=((m+a-1)/a)*((n+a-1)/a);
    cout<<s<<endl;
    return 0; 
}

2.A   65    Z    90

   a   97    z     122

 

3.字符串删除 

  https://blog.csdn.net/yishizuofei/article/details/79059804

  C++ string 字符串删除指定字符https://blog.csdn.net/lynn_xl/article/details/89151535  

  C++从string中删除所有的某个特定字符  https://www.cnblogs.com/7z7chn/p/6341453.html   超好

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin>>str;
    str.erase(remove(str.begin(),str.end(),'a'),str.end());
    cout<<str<<endl;
    return 0;
}

 

删除特定字符串简单做法

int pos=0;//下标 
    while( (pos=str.find("WUB"))!=-1 ){
        str.erase(pos,3);
    }

 

 

5. 字符串 大写 改为 小写

for(int i=0;i<str.size();i++){
        str[i]=tolower(str[i]);
    }

 小写改为大写

toupper();

 

 

6.字符串  str1 中是否有字符字串  str2

strstr()函数    

  extern char *strstr(char *str1, char *str2);

  作用:返回str2 在str1中第一次出现的位置(地址)

c_str() 函数

  作用:c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.,

            这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。

string str;
    string str2="1111111";
    if(strstr(str.c_str(),str2.c_str())!=NULL)flag=true;
    else flag=false;

 

7.字符串插入字符

  https://blog.csdn.net/wang1997hi/article/details/78364755

http://codeforces.com/problemset/problem/208/A

cf 208 A 考察了删除和插入

str.insert(pos,str2);

 cf  208 A

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin>>str;
    int pos=0;//下标 
    while( (pos=str.find("WUB"))!=-1 ){
        str.erase(pos,3);
        if(str[pos-1]!=' '&&pos!=0)str.insert(pos," "); 
    }
    cout<<str<<endl;
    return 0;
}
str.insert(pos,str2);
View Code

 

8.字符串输入空格

cin.getline(str,15);

cin.getline(接受字符串到m,接受个数5,结束字符)

getline(cin,str) // 接受一个字符串,可以接收空格并输出,需包含“#include<string>”

posted @ 2019-10-22 01:14  w_w_t  阅读(274)  评论(0编辑  收藏  举报