zoukankan      html  css  js  c++  java
  • wenbao与string

    string容器

    输入一行,包括空格(相当于gets)

    1 #include <iostream>
    2 #include <string>
    3 #include <sstream>
    4 using namespace std;
    5 int main(){
    6     string str;
    7     getline(cin,str);
    8     cout<<str<<endl;
    9 }

    查找(指定位置的查找)转化

     1 #include <iostream>
     2 #include <string>
     3 #include <sstream>
     4 using namespace std;
     5 int main(){
     6     string s;
     7     stringstream ss;
     8     int n, i, sum, a;
     9     cin >> n;
    10     getline(cin, s); // 获取换行
    11     for (i=0; i<n; i++){
    12         getline(cin, s);
    13         ss.clear();
    14         ss.str(s);
    15         sum=0;
    16         while (1){
    17             ss >> a; //逐个获取
    18             if ( ss.fail() ) break; //s.fail 字符串结束
    19             sum+=a;
    20         }
    21     cout << sum << endl;
    22     }
    23     string str = "aaaaddddssdfsasdf";
    24     int pos = str.find("ssdf", 3);       //用if(pos == string::npos) 用来判断是否找到子串。
    25     if(pos != str.npos) cout<<pos<<endl;
    26     string str2 = str.substr(pos, 5);
    27     cout<<str2<<endl;
    28 }

     转化2

     1 #include <iostream>
     2 #include <string>
     3 #include <sstream>
     4 using namespace std;
     5 int main(){
     6     string s;
     7     int n;
     8     cin>>s;
     9     stringstream ss;  //stringstream ss(s);
    10     ss << s;
    11     cout<<ss.str()<<endl;
    12     ss >> n;
    13     cout<<n<<endl;
    14     ss.str("");//清空stream
    15     cout<<ss.str()<<endl;
    16     
    17     string line,word[10];
    18     int x=0;
    19     while(getline(cin,line)){
    20         stringstream stream(line);
    21         cout<<stream.str()<<endl; // 输出stream
    22         while(stream>>word[x]) x++;
    23         for(int i = 0; i < x; i++)
    24         cout<<word[i]<<endl;
    25     }
    26 }

     反转

    reverse 在原字符串上面反转

    assign 反转到别的字符串(当然也可以复制),万能的下标(str.begin()+n,str.begin()+m)

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string>
     4 using namespace std;
     5 int main(){
     6     freopen("in.txt", "r", stdin);
     7     freopen("out.txt", "w", stdout);
     8     string str,str2;
     9     cin>>str;
    10     str2.assign (str.rbegin(),str.rend());
    11     cout<<str2<<endl;
    12     cout<<str<<endl;
    13     reverse(str.begin(),str.end());
    14     cout<<str<<endl;
    15 }
    16 
    17 输入:    asdfg
    18 输出:    gfdsa
    19          asdfg
    20          gfdsa
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string>
     4 #include <algorithm>
     5 #include <vector>
     6 using namespace std;
     7 int main(){
     8     //string 与 char 之间的互换
     9     string str;
    10     char ss[100];
    11     scanf("%s",ss);
    12     str = ss;
    13     printf(str.c_str());
    14     cout<<endl;
    15     printf("%s
    ",ss);
    16     cout<<str<<endl;
    17     cout<<ss<<endl;
    18 
    19     //string 与 vector的结合,相当于二维数组
    20     vector <string> v;
    21     v.push_back("Jack");
    22     v.push_back("Mike");
    23     v.push_back("Tom");
    24     cout<<v[0]<<endl;
    25     cout<<v[1]<<endl;
    26     cout<<v[2]<<endl;
    27 
    28     string s;
    29     s="12345abcde";
    30     cout<<s<<endl;
    31 
    32     //反向排序,头文件<algorithm>
    33     reverse(s.begin(),s.end());
    34     cout<<s<<endl;
    35 
    36     //排序
    37     sort(s.begin(),s.end());
    38     cout<<s<<endl;
    39 
    40     //定义迭代器
    41     string::iterator it;
    42 
    43     //s与”1234“比较,相等返回0,小于返回-1,大于返回大于个数
    44     cout<<s.compare("1234")<<endl;
    45 
    46     //查找第一个字符‘1’,返回下标值
    47     cout<<s.find('1')<<endl;
    48 
    49     //查找第一个子串“1“,返回下标值
    50     cout<<s.find("1")<<endl;
    51 
    52     //查找第一个子串”abc“,返回下标值
    53     cout<<s.find("abc")<<endl;
    54 
    55     //查找第一个子串”abh“,查不到返回4294967295==(2^32)-1;
    56     //18446744073709551615==(2^16)-1;
    57     cout<<s.find("ah")<<endl;
    58 
    59     it=s.begin();
    60 
    61     //从第三个开始,将连续的3个字符替换为“good”
    62     //即将“45a”替换为“good”
    63     s.replace(3,3,"good");
    64     cout<<s<<endl;
    65 
    66     //把字符'p'插入到1个字符前
    67     s.insert(it+1,'p');
    68     cout<<endl;
    69 
    70     //删除第三个元素
    71     s.erase(it+3);
    72     cout<<s<<endl;
    73 
    74     s.erase(it,it+3);
    75     cout<<s<<endl;
    76 
    77     s="";
    78     cout<<s<<endl;
    79     cout<<s.length()<<endl;
    80     
    81     return 0;
    82 }

    string find

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 int main()
     5 {
     6     string str;
     7     cin>>str;
     8     int x=str.find("AB"),y=str.find("BA");
     9     if((x!=str.npos&&str.find("BA",x+2)!=str.npos)||y!=str.npos&&str.find("AB",y+2)!=str.npos){
    10         cout<<"YES"<<endl;
    11         return 0;
    12     }
    13     cout<<"NO"<<endl;
    14 }

     substr(子串截取函数)

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main(){
     5     string str;
     6     cin >> str;
     7     string str1 = str.substr(0, 3), str2 = str.substr(5, 8);
     8     cout<<str1<<"**"<<str2<<endl;
     9     return 0;
    10 }

    只有不断学习才能进步!

  • 相关阅读:
    全屏后无法检查键值
    根据秒转化为周天时秒分,不足补0
    日期时间格式化(Date对象)
    AS3 RGB颜色
    Egret eui中eui.ItemRenderer类型的组件在渲染界面时,dataChanged()重复调用
    egret 画圆 画圆角矩形 画矩形
    egert 缓动
    Flashdeveloper开多个实例进程解决方案
    flash 发布exe文件
    sublime Text3 如何自动排版代码
  • 原文地址:https://www.cnblogs.com/wenbao/p/5727735.html
Copyright © 2011-2022 走看看