• C++之string


     1 #include <iostream>
     2 #include<string>
     3 
     4 using  namespace std;
    27 int main(){
    28 
    29     //C++标准库中的string类型
    30     string name("张三");
    31     cout<<name<<endl;
    32     //定义初始化方法
    33     string s1;
    34     string s2("李四");
    35     string s3(s2);
    36     string s4(10,'a');
    37     cout<<s1<<endl;
    38     cout<<s2<<endl;
    39     cout<<s3<<endl;
    40     cout<<s4<<endl;
    41     //读入字符串并输出
    42     string s;
    43     cin>>s; //读取到第一个空格就停止了 只能读一部分 有效字符之前如果有空格也会读进来然后扔掉
    44     cout<<s<<endl;
    45     读取一整行 用getline方法
    46     string ss;
    47     getline(cin,ss);
    48     cout<<ss<<endl;
    49 
    50     //循环每次读一行 ,getline方法是遇到换行符停止
    51     string line;
    52     while(getline(cin,line))
    53     {
    54         cout<<line<<endl;
    55     }
    56 
    57     //每次读一个单词
    58     string word;
    59     while(cin>>word)
    60     {
    61         cout<<word<<endl;
    62     }
    63     return 0;
    64 }
     1 //查看字符串的大小 size方法
     2     string st("hello");
     3     //C++中使用size_type类型专门保存字符串大小
     4     string::size_type size1=st.size();
     5     cout<<size1<<endl;
     6     if(st.empty())
     7     {
     8         cout<<"这是一个空字符串"<<endl;
     9     }else{
    10         cout<<"这是一个非空字符串"<<endl;
    11     }
    12 
    13     //比较大小 汉字通过拼音比较
    14     string s1("张飞");
    15     string s2("刘备");
    16     if(s1==s2)
    17     {
    18          cout<<"张飞和刘备相等"<<endl;
    19     }
    20     if(s1>s2)
    21     {
    22          cout<<"张飞比刘备大"<<endl;
    23     }else{
    24          cout<<"张飞比刘备小"<<endl;
    25     }
    26 
    27     //两个字符串相加 连接操作
    28     string ss1("hello,");
    29     string ss2("world
    ");
    30     string ss3=ss1+ss2;
    31     ss1+=ss2;
    32     cout<<ss3<<endl;
    33     cout<<ss1<<endl;
    34     //字符串和字符串字面值连接的时候必须有一个string类型的值 加好两边不能全是string的字面值
    35     string ss5=ss1+"nihao";
    36     cout<<ss5<<endl;
     1 #include <iostream>
     2 #include<string>
     3 //c语言中的头文件前加C 可以在c++中使用
     4 #include<cctype>
     5 
     6 using  namespace std;
     7 
     8 int main(){
     9     string str("hello");
    10     cout<<str[0]<<endl;
    11 
    12     for(string::size_type i=0;i!=str.size();i++)
    13     {
    14         cout<<str[i]<<endl;
    15     }
    16 
    17     for(string::size_type i=2;i!=str.size();i++)
    18     {
    19         str[i]='*';
    20     }
    21     cout<<str<<endl;
    22 
    23 
    24     //string 中的字符处理
    25     string s("hello!!!");
    26     string::size_type count1=0;
    27     for(string::size_type i=0;i!=s.size();i++)
    28     {
    29         //ispunct方法检测字符串中是否有标点符号
    30         //isanum()
    31         if(ispunct(s[i]))
    32             ++count1;
    33     }
    34     cout<<count1<<endl;
    35     //输入字符串把标点符号去掉
    36     string y,result;
    37     char ch;
    38     bool has=false;
    39     getline(cin,y);
    40     for(string::size_type index=0;index!=y.size();index++)
    41     {
    42         ch=y[index];
    43         if(ispunct(ch))
    44         {
    45             has=true;
    46         }else{
    47             result+=ch;
    48         }
    49     }
    50     if(has)
    51     {
    52         cout<<result<<endl;
    53     }else{
    54         cout<<"没有标点符号"<<endl;
    55     }
    56 
    57 
    58     return 0;
    59 }
  • 相关阅读:
    sql server 函数
    Idea+Maven+Jetty,代码修改后,重新部署,无法获取新内容~
    css优先级
    jquery选择器
    XML学习笔记一
    JVM参数调优实例解析
    JVM中的Stack和Heap--------堆与栈
    svn报错:“Previous operation has not finished; run 'cleanup' if it was interrupted“
    Spark中的CombineKey()详解
    Spark性能优化指南——高级篇
  • 原文地址:https://www.cnblogs.com/yh2924/p/12515814.html
Copyright © 2020-2023  润新知