zoukankan      html  css  js  c++  java
  • [C++] string

    string

    table list

    someting else basic function

    init

    string test1;    //空串
    
    string test2 = "内容"; //赋值=
    
    string test3("内容");   //使用引用字符数组作为参数传给构造函数
    
    string test4(test2); //用一个string初始化另一而string
    
    string test5(test2,pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符
    
    string test6 = test2 + "内容" + test3 //混合初始化
    
    string test7 = test2.substr(pos,num); //从test2中的第pos个位置开始,拷贝个数为num个字符
    
    string test8 = test2.substr(); //参数列表为空则会拷贝test2的整个对象(复制test2的简便方法)
    
    string test9(num,ch); //拷贝num个字符型ch到test9
    

    std::basic_string::find

    #include <string>
    #include <iostream>
     
    void print(std::string::size_type n, std::string const &s)
    {
        if (n == std::string::npos) {
            std::cout << "not found
    ";
        } else {
            std::cout << "found: " << s.substr(n) << '
    ';
        }
    }
     
    int main()
    {
        std::string::size_type n;
        std::string const s = "This is a string";
     
        // search from beginning of string
        n = s.find("is");
        print(n, s);
     
        // search from position 5
        n = s.find("is", 5);
        print(n, s);
     
        // find a single character
        n = s.find('a');
        print(n, s);
     
        // find a single character
        n = s.find('q');
        print(n, s);
    }
    

    found: is is a string
    found: is a string
    found: a string
    not found

  • 相关阅读:
    [APIO 2009] Atm
    Codeforces518 D. Ilya and Escalator
    [POJ2096] Collecting bugs
    [ZOJ3329] One Person Game
    [LightOJ1038] Race to 1 Again
    「NOI2003」逃学的小孩
    [HAOI2006] 旅行
    ☆ [POJ2411] Mondriaan's Dream 「状压DP」
    「POJ3311」Hie with the Pie
    「乘法逆元」 学习笔记
  • 原文地址:https://www.cnblogs.com/zhanxiage1994/p/6999898.html
Copyright © 2011-2022 走看看