zoukankan      html  css  js  c++  java
  • [C++]字符串处理

    字符串按行读入

    getline(cin, s1);
    

    字符串删除指定字符

    通过algorithm中的remove将值为val的元素全部移动到末尾,并返回newend的迭代器
    利用string.erase将newend和end范围内的元素删除

    s1.erase(remove(s1.begin(), s1.end(), s2[i]), s1.end());
    

    find函数的区别

    在string和algorithm中都提供了find

    std::find function template

    emplate <class InputIterator, class T>
    InputIterator find (InputIterator first, InputIterator last, const T& val);

    输入参数

    查找范围的迭代器,以及查找值

    返回值

    第一个匹配元素的迭代器,没有匹配的结果返回last迭代器

    std::string::find

    重载

    string (1)
    size_t find (const string& str, size_t pos = 0) const noexcept;
    c-string (2)
    size_t find (const char* s, size_t pos = 0) const;
    buffer (3)
    size_t find (const char* s, size_t pos, size_type n) const;
    character (4)
    size_t find (char c, size_t pos = 0) const noexcept;

    输入参数

    str/s/c:要寻找的字符串/字符
    pos :寻找的起始位置
    n :匹配的长度

    返回值

    返回找到的第一个匹配元素的位置(size_t)
    没找到则返回string::npos

    输出指定精度

    printf("%.2f",num)

  • 相关阅读:
    leetcode-409
    leetcode-836
    leetcode-1160
    leetcode-面试题13
    leetcode-695
    go的一些小lib
    leetcode-300
    cookie
    php上传文件
    PHP 文件创建/写入
  • 原文地址:https://www.cnblogs.com/wendyy/p/9332629.html
Copyright © 2011-2022 走看看