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

        
  • 相关阅读:
    关于Mac上的开发工具
    关于VS2008和VS2013中字体的选择
    实验四 使用ASP.NET内置对象 总结
    实验三 使用ASP.NET常用服务器控件 总结
    实验二 C#程序设计 总结
    实验一 ASP.NET应用环境配置 总结
    关于PHP.INI中的错误ERROR报告级别设置
    获取当前网址跟目录
    PHP获取站点根目录
    php 上传图片
  • 原文地址:https://www.cnblogs.com/Twobox/p/6507495.html
Copyright © 2011-2022 走看看