P405 STL黑科技 占尽先机之string


1. string输入

string s1; 默认构造函数,s1为空串
string s2(s1); 将s2初始化为s1的一个副本
string s3("valuee"); 将s3初始化一个字符串面值副本
string s4(n,'c'); 将s4 初始化为字符'c'的n个副本
cin>>s;                       读取字符串遇到空格结束(读单词)
getline(cin,s);            读取字符串,遇'\n'结束(读行)
getline(cin,s,'a');       遇'a'结束,其中任何字符包括'\n'都能够读入

巩固练习

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1;
    s1="i love you";
    string s2(s1);  //把s2初始化为s1的一个副本,注意写法,不能前面先定义s2的类型后面直接写,也不能定义两次s2
    string s3("value");  //将s3初始化一个字符串面值副本
    string s4(10,'s');   //将s4初始化为字符‘s'的10个副本
    /*注意字符串面值与标准库string不是同一个类型*/
    cout<<s2<<" "<<s3<<" "<<s4<<endl;
    string s5;
    while(cin>>s5)  //这里可以输入“  hello world  ”测试,发现只读取有效字符到遇到空格结束
    {
        cout<<s5<<endl;
    }
    return 0;
}

  

 

2. string对象操作
s.empty() 判断是否为空,bool型
s.size() 或 s.length() 返回字符的个数
s[n] 返回位置为n的字符,从0开始计数
s1+s2 连接,看下面例子:
可用此方法给字符串后面添加字符如:s=s+'a';
a: string s2=s1+", "; //对,把一个string对象和一个字符面值连接起来是允许的
b: string s4="hello "+", "; //错,不能将两个字符串面值相加
c: string s5=s1+", "+"world"; //对,前面两个相加相当于一个string对象;
d: string s6="hello" + ", " + s2; //错
(注:字符串尾部追加还可用s.append("abc")函数添加)
s1=s2 替换
s1==s2 相等,返回true或false
!=,<,<=,>,>= 字符串比较,两个字符串短的与长的前面匹配,短的小于长的
巩固练习

#include<iostream>
#include<string>
using namespace std;
int main(){
    string str1;
    string str2("the size of ");
    string str3=" hello world  ";//空格不会忽略
    str3+=str2;
    str3.append("haha secessful");
    cout<<str3<<endl;
    cout<<"the size of is "<<str2.size()<<endl;
    /*注意这里取长度的str2.size(),和str2.length(),但是注意str2.size()返回的值并不是int类型,
    事实表明size_type存储的string长度是int所能存储的两倍*/
    getline(cin,str1);  //read line at time until end-of-file,注意写法。
    while(!str1.empty())  //返回一个bool值,空的话返回true,否则返回false。
    {
        for(string::size_type i=0;i!=str1.size();++i)  //注意size_type类型
        {
            cout<<str1[i];
        }
        cout<<endl;break;
    }
    return 0;
}

  

 

3. string对象中字符的处理(头文件cctype)

 

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
    string str1="Hello World!!!";
    string::size_type punct_cnt = 0;
    for(string::size_type i=0;i!=str1.size();++i)
    {
        if(ispunct(str1[i]))
            ++punct_cnt;
        str1[i]=toupper(str1[i]);
    }
    cout<<"字符中标点符号有:"<<punct_cnt<<endl;
    cout<<str1<<endl;
    return 0;
}

  

 

4:string对象中一些函数

/*-----------插入函数-------包括迭代器操作和下标操作,下标操作更灵活*/
s.insert(it,p); 把字符串p插入到it的位置
s.insert(p,n,t); 迭代器p元素之前插入n个t的副本
s.insert(p,b,e); 迭代器p元素之前插入迭代器b到e之间的所有元素
s.insert(p,s2,poe2,len); 在下标p之前插入s2下标从poe2开始长度为len的元素
s.insert(pos,cp,len); 下标pos之前插入cp数组的前len个元素。

/*-----------------------替换函数-----------------------------------*/
s.assign(b,e); 用迭代器b到e范围内的元素替换s
s.assign(n,t); 用n个t的副本替换s
a.assign(s1,pos2,len); 从s1的下标pos2开始连续替换len个。
s.replace (3,3,"good") ; 从第三个起连续三个替换为good
s.substr(i,len) 截取s串中从i开始的len个字符

/*-----------------------删除函数-----------------------------------*/
s.erase(s.begin()+4) 删除第四个元素
s.erase(0,4) 或第一到第四个元素
s.erase(it1,it2)

/*----------------------其他函数------------------------------------*/

s.find("cat") ; 查找第一个出现的字符串”cat“,返回其下标值,查不到返回 4294967295,也可查找字符;//string::npos 判断字符串是否结束//即-1
s.find("cat",4) ; 从s的第5个字符起查找第一个出现字符串"cat"的位置
s.append(args); 将args接到s的后面
s.compare("good") ; s与”good“比较相等返回0,比"good"大返回值大于0,小则返回值小于0;
reverse(s.begin(),s.end ()); 反向排序函数,即字符串反转函数

 

#include<iostream>
#include<algorithm>
#include<string>
#include<numeric>
using namespace std;
int main(){
    string s;
    s="54268713";
    reverse(s.begin(),s.end()); //字符串反转
    cout<<s<<endl;
 
    string s1="i love you";
    string::iterator it;
    it=s1.begin();
    s1.insert(it+1,'p');  //插入
    cout<<s1<<endl;
 
    string s2("abc123456");
    string::iterator it2=s2.begin();
    s2.erase(it2+6);  //删除
    cout<<s2<<endl;
    s2.erase(it2,it2+3);
    cout<<s2<<endl;
    s2.replace(2,1,"good");  //替换
    cout<<s2<<endl;
    cout<<s2.find("good")<<endl;  //搜索返回下标值
    cout<<s2.compare("12good56")<<endl;  //比较,自行修改值看其返回值
    cout<<s2.compare("12good56758")<<endl;
 
    return 0;
}

  

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    string s;
    s="123cat&&dog";
    cout<<s.find("cat")<<endl;
    if(s.find("cat")!=string::npos)cout<<"Found"<<endl;
    else cout<<"not Found!"<<endl;
    return 0;
}

  

 

#include<iostream>
#include<string>
using namespace std;
int main(){
    string s="0123456789";
    //cin>>s;
    cout<<s.substr(3,5)<<endl;//34567
    return 0;
}

  

 

5:string的一些常用操作及用法
***string对象作为vector元素
***string对象的数字化处理
***string对象与sscanf函数

#include <iostream>
#include <algorithm>
#include <string>
#include <numeric>
#include <vector>
#include <cstdio>
using namespace std;
int main(){
    vector<string> v;   //vector的string
    v.push_back("Iack");
    v.push_back("Mike");
    v.push_back("Tom cluce");
    cout<<v[0]<<endl;
    cout<<v[1][1]<<endl;
    cout<<v[2].size()<<endl;

    char s3[100],s2[100];
    string str3,str2;
    int ab,ac,ad;
    sscanf("abc fsaf","%s %s",s2,s3);  //注意string不能直接用于sscanf
    str3=s3;str2=s2;
    cout<<str3<<" "<<str2<<endl;
    sscanf("4,5$10000","%d,%d$%d",&ab,&ac,&ad);
    cout<<ab<<" "<<ac<<" "<<ad<<endl;

    char s[200];
    cin>>s;
    cin>>s;
    string s1=s;
    printf(s1.c_str());  //c输出字符串对象

    return 0;
}

  

 

6. 类型转换

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cctype>
#include<string>
#include<cstring>
using namespace std;
int main(){
    double x; string s;
    
    //string=>double
    s="3.1415";
    istringstream IS(s);  IS>>x;
    printf("%.3f\n",x);
    
    //double=>string
    x=0.618;
    ostringstream OS;  OS<<x;
    cout<<OS.str()<<endl;   
    
    return 0;
}

  

7.   大小写互转

(1)单字符操作

#include<iostream>
#include<cctype>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    string s;
    cin>>s;
    s[0]=tolower(s[0]);
    cout<<s<<endl;
    return 0;
}

  

(2)整串操作

stl::string的大小写转换
进行string开发,需要进行大小写转换,发现STL的string没有提供这些方法。
查找资料,发现STL中还是提供了这类方法,在Algorithm中包含。

#include<iostream>
#include<cctype>
#include<string>
#include<algorithm>
using namespace std;
    int main(){
    string s,s1;
    cin>>s;
    transform(s.begin(),s.end(),s.begin(),::tolower);
    cout<<s<<endl;
    return 0;
}

  

 8.sstream的简单使用

读入一行,按空格分割

#include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
using namespace std;

int main(){
    string line;
    while(getline(cin,line)){
        int sum=0;
        string x;
        stringstream ss(line);
        while(ss>>x)cout<<x<<endl;
    }
    return 0;
}

  

 

 

下面推荐一些字符串的题目
hdoj 2017 字符串中统计数字,直接调用上面s.digit()函数
hdoj 1020 判断输出重复、水题、
hdoj 1062 逆转字符串 注意1:getchar()吸收3后'\n',2:空格不止有一个
hdoj 1039,字符串处理,清晰思路,可以写三个判断条件的3个函数,调用函数判断,思路清晰,容易判断;
hdoj 1088 对字符串按一个一个处理。一次性输入一行不好控制
hdoj 1113 map容器+字典序。值得做
hdoj 1161 tolower() 函数转化为小写就ok
1200、1251、1256、1288、1321、1328、1379、1804、1860、 1982、1984、2017、2024、2025、2026、2027、2043、2052、2054、2072、2074、2087、2131、 2137、2140、2163、2203、2206、2352、2500、2549、2564、2565、2567、2572、2609、2607、 2707、2708、2719、2721、2723、


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM