zoukankan      html  css  js  c++  java
  • string 学习

    #include <string>

      1.取当中某个字符

        与传统一样 c[11]="0123456789"; c[1]=1; ps:好慢 。。 会不会GG。。。

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main()
     5 {
     6     string s("0123456789");
     7     cout<<s<<endl;
     8     cout<<s[1]<<endl;//也是从0位开始
     9     return 0;
    10 }

      2.一些比较函数

        ① 重载运算符 <,>,<=,>=,==

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main()
     5 {//得出 重载符 比较的是字典序 不是长度
     6     string ss[2];
     7     while(cin>>ss[0]>>ss[1])
     8     {
     9         cout<<ss[0]<<" "<<ss[1]<<endl;
    10         cout<<" < : "<<(ss[0]<ss[1])<<endl;
    11         cout<<" > : "<<(ss[0]>ss[1])<<endl;
    12         cout<<" <= : "<<(ss[0]<=ss[1])<<endl;
    13         cout<<" >= : "<<(ss[0]>=ss[1])<<endl;
    14         cout<<" == : "<<(ss[0]==ss[1])<<endl;
    15     }
    16     return 0;
    17 }

      3.一些查找函数

        int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
        int find(const char *s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
        int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
        int find(const string &s,int pos = 0) const;//从pos开始查找字符串s在当前串中的位置 //查找成功时返回所在位置,失败返回string::npos的值

        

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int main()
     5 {
     6     string ss="abcdefghijklmnopqrstuvswxyz";
     7 
     8     cout<<ss.find('j')<<endl;
     9     cout<<ss.find("st")<<endl;
    10     cout<<ss.find("sw")<<endl;
    11     cout<<ss.find("sw",0,1)<<endl;
    12     return 0;
    13     //9 18 22 18
    14 }

        

    待续:2017-03-14 22:56:21

        
  • 相关阅读:
    java之正则表达式
    mysql之自定义函数
    mysql之replace into与 insert into duplicat key for update
    mysql之命令行导入导出
    Echarts修改legend样式
    ubuntu出现 E: Sub-process /usr/bin/dpkg returned an error code
    ubuntu总是提是E: 不能满足依赖关系。不妨试一下 -f 选项
    ubuntu安装和查看已安装软件
    放爬虫nginx
    nginx日志切割
  • 原文地址:https://www.cnblogs.com/Twobox/p/6507495.html
Copyright © 2011-2022 走看看