zoukankan      html  css  js  c++  java
  • string

    c++ STL的浅谈

        c++和c语言有很多区别,其中我觉得比较大的就是STL,STL中的很多东西都是先人给我们提前写好的,我们只要拿来用就行了。

        STL最常用的有这些

        1:string

        2:vector

        3:set

        4:list

        5:map

    1:string

    1:如何定义字符串

        用string初始化字符串分两类:用“=”号就是拷贝初始化,否则就是直接初始化。//不做要求

    #include<iostream>
    #include<string>
    #include<cstring >
    using namespace std;
    
    int main()
    {
        string s1(10,'a');//拷贝初始化
        cout<<s4<<endl;        
        string s2 = string(6, 'c'); 
        cout<<s7;
    }

     

    那就是用getline来获取一整行内容。

    string str;
    getline(cin, str);
    cout << str << endl;
     

    当把string对象和字符面值及字符串面值混在一条语句中使用时,必须确保+的两侧的运算对象至少有一个是string

    string s1 = s2 + ", "; //正确
    string s3 = "s " + ", "; //错误
    string s4 = "hello" + ", " + s1; //错误
    string s5 = s1 + "hello " + ", "; //正确  

     

    还有个find函数特别好用

    a.find(b,0)字符串b在a的位置

    #include <iostream> 
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>         
    #include <map>      
    #include <iomanip>
    #include <algorithm> 
    #include <queue>     
    #include <stack>               
    #include <set>  
    #include <vector>    
    #define ll long long
    #define MAX INT_MAX
    #define MIN INT_MIN
    using namespace std;
    string a,b;
    int ans,k;
    int main()
    {
       cin>>a>>b;
       while(1)
       {
          if(a.find(b,k)!=string::npos)
          {
             ans++;
             k=a.find(b,k)+2;
          }
          else
          {
             if(ans!=0)
             {
                cout<<ans;
    
             }
             else
             {
                cout<<"mei you";
             }
             break;
    
          }
       }
       //cout<<a.find("ha",0);
    }
     
  • 相关阅读:
    转:高效使用 SSH 的 16 个技巧
    关于flash的多文件上传的http头
    使用Xmind画流程图、脑图
    用html5+flash两种方案实现前端长文转图
    用“夜间模式”模式(javascript书签)浏览网页
    浏览器上传图片技术的一点分析
    需求管理的关键步骤其实只有一个
    基于Google GWT的图形编辑框架gwthtml5graph发布了!
    软件需求与天女散花
    你和软件需求,谁管谁
  • 原文地址:https://www.cnblogs.com/jrfr/p/10329428.html
Copyright © 2011-2022 走看看