zoukankan      html  css  js  c++  java
  • C++---String类小结

    本随笔是我自己在敲代码的时候一些感受,可能有的地方简略了,还有部分是融合了一点其他资料上的知识点,如有错误欢迎指出,谢谢_(:°з」∠)_

    string类是C++中常用的字符串类型,比之普通的字符串要好用不少(自我感觉)

    所需头文件:

    1 #include<iostream>
    2 #include<string>
    3 //using std::string;  在只用string时可以就加这个
    4 using namespace std;

    初始化:

     1 //注释是结果 
     2     char b[20] = "This is bibi"; //This is bibi
     3     string a = "My name is LJMZ"; //My name is LJMZ
     4     string a1 = b; //This is bibi
     5     string a2 ("This is lili"); //This is lili
     6     string a3 (5 , 'a'); //aaaaa
     7     string a4 (a , 5); //me is LJMZ
     8     string a5 (a , 5 , 2); //me
     9     string a6 (b , 5); //This
    10     string a7 (b + 2 , b + 5); //is

    赋值: 

     1  char aa[10]="789";
     2     string a = "LJMZ"; 
     3     string b = "ZY"; 
     4     string c;
     5     string a1 = a + b; //LJMZZY
     6     a += "LOVE";//LJMZLOVE
     7     a1.push_back('.'); //LJMZZY.
     8     a1.append("ni hao!"); //LJMZZY.ni hao!  
     9     a1.assign("123"); //123
    10     //insert()一共有8种函数原形,以下是比较常见的几种 
    11     b.insert(1,"aaa"); //ZaaaY  
    12     b.insert(1,a); //ZLJMZLOVEaaaY
    13     b.insert(1,aa); //Z789LJMZLOVEaaaYz
    14     //输入: ljmz!ljmz 
    15     getline(cin,c); //ljmz!ljmz 
    16     getline(cin,c,'!'); //ljmz

    长度:

    string a = "LJMZ";
        cout<<a<<endl; 
        printf("%d %d
    ",a.size(),a.length());

           结果:

        

    复制:

    1     //注释是结果 
    2     string a = "111LJMZL";
    3     char b[10];
    4     int l=a.copy(b,5,1); //把a中第1个字符到1+5个字符转存到b中 
    5     b[l]='';
    6     cout<<b<<endl; //11LJM

    交换:

    1    string a = "LJMZ";
    2     string b = "ZY";
    3     cout<<"交换前:"<<endl<<a<<endl<<b<<endl;
    4     a.swap(b);
    5     cout<<"交换后:"<<endl<<a<<endl<<b<<endl;

      结果:

      

  • 相关阅读:
    JQuery的常用方法
    Javascript的一些奇技淫巧 持续更新
    jQuery调用ASP.NET的WebService
    jquery easy ui 分页
    EF里查看/修改实体的当前值、原始值和数据库值
    oracle 游标变量ref cursor详解
    分页存储过程2
    分页存储过程
    取得HTML中所有图片的 URL 正则表达式
    Javascript跨域访问解决方案
  • 原文地址:https://www.cnblogs.com/ljmzzyk/p/6904815.html
Copyright © 2011-2022 走看看