zoukankan      html  css  js  c++  java
  • basic_string定义的相关string函数

    std::basic_string::substr

    函数定义及功能描述

    注: 默认参数npos means "until the end of the string"

    示例程序

    #include<iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string t = "15:30:28";
        
        // 1 5 : 3 0 : 2 8
        // 0 1 2 3 4 5 6 7
        
        string h = t.substr(0, 2);
        string m = t.substr(3, 2);
        string s = t.substr(6);
    
        cout << h << ' ' << m << ' ' << s << endl;
    
        return 0;
    }
    

    std::basic_string::find

    函数定义及功能描述

    注:

    1. When pos is specified, the search only includes characters at or after position pos,注意当提供pos参数时,是包含pos那个位置的
    2. 可以 Find position of a character 也可以 Find position of a C string.
    3. If not found, returns npos,即-1

    实例程序

    #include<iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string t = "15:30:28";
    
        // 1 5 : 3 0 : 2 8
        // 0 1 2 3 4 5 6 7
        
        int pos1 = t.find(':');
        int pos2 = t.find(':', pos1 + 1);
    
        cout << pos1 << ' ' << pos2 << endl;
    
        return 0;
    }
    

    std::basic_string::rfind

    函数定义及功能描述

    注:

    1. When pos is specified, the search only includes sequences of characters that begin at or before position pos, rfind是倒着找,仅考虑pos及以前的位置
    2. 可以 Find position of a character 也可以 Find position of a C string.
    3. If not found, returns npos,即-1

    实例程序

    #include<iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string t = "15:30:28";
    
        // 1 5 : 3 0 : 2 8
        // 0 1 2 3 4 5 6 7
    
        int pos1 = t.rfind(':');
        int pos2 = t.rfind(':', pos1 - 1);
    
        cout << pos1 << ' ' << pos2 << endl;
    
        return 0;
    }
    

  • 相关阅读:
    Python自动化开发课堂笔记【Day02】
    Python自动化开发课堂笔记【Day01】
    Python自动化开发课堂笔记【Day01】
    Python自动化开发课堂笔记【Day01】
    SOCKET.IO 前后端使用
    socket.io的emit使用清单
    nodejs 教程
    斗地主滑动选牌&&出牌
    Cocos Creator大厅+子游戏模式
    cocoscreator热更新
  • 原文地址:https://www.cnblogs.com/G-H-Y/p/15417638.html
Copyright © 2011-2022 走看看