美文网首页
C++ string类

C++ string类

作者: RobotBerry | 来源:发表于2017-05-09 14:44 被阅读0次

    构造函数

    string(); // 默认构造函数
    string(const string &str); // 拷贝构造函数
    string(const string &str, size_t pos, size_t len = npos); // 拷贝str的[pos, pos+len)部分
    string(const char *s);
    string(const char *s, size_t n); // 拷贝s的[0, n)部分
    string(size_t n, char c); // n个c
    

    注意最后一个构造函数,用n个c来初始化字符串。例如string(5, 'a')就表示字符串"aaaaa".

    修改函数

    string& append (const string &str, size_t subpos, size_t sublen); // 加上str的[subpos, subpos+sublen)部分
    string& insert (size_t pos, const string &str, size_t subpos, size_t sublen); // 在pos位置上插入str的[subpos, subpos+sublen)部分
    string& insert (size_t pos, size_t n, char c); // 在pos位置上插入n个c字符
    string& erase (size_t pos = 0, size_t len = npos); // 删除字符串的[pos, pos+len)部分
    string& replace (size_t pos,  size_t len,  const string &str); // 字符串的[pos, pos+len)部分用str来替换(str的长度不小于len,这一点需要用户保证)
    void swap (string& str); // 交换两个字符串,可能会用到move,效率高
    

    操作函数

    const char* data() const; const char* c_str() const; // 两者都是返回c风格的字符串
    
    // 在字符串中查找其他字符(串)第一次出现的位置,对应的rfind是找最后一次出现的位置
    size_t find (const string &str, size_t pos = 0) const; // 在字符串的[pos,...)部分找str第一次出现的位置,...是末尾
    size_t find (const char *s, size_t pos = 0) const; // 在字符串的[pos,...)部分找s第一次出现的位置,...是末尾
    size_t find (const char *s, size_t pos, size_t n) const; // 在字符串中找str的[pos, pos+n)部分第一次出现的位置
    size_t find (char c, size_t pos = 0) const;// 在字符串中找字符c第一次出现的位置
    
    // 在字符串中查找其他字符(串)第一次出现的位置,对应的find_last_of是找最后一次出现的位置
    // 注意,find_first_of是指查找第一个出现被查找字符串的字符的位置,而find需要匹配整个被查找字符串
    // find_first_not_of和find_last_not_of则正好反过来,找不存在于被查找字符串的第一个位置
    size_t find_first_of(const string &str, size_t pos = 0) const; // 在字符串的[pos,...)部分找str所有字符第一次出现的位置,...是末尾
    size_t find_first_of(const char *s, size_t pos = 0) const; // 在字符串的[pos,...)部分找s所有字符第一次出现的位置,...是末尾
    size_t find_first_of(const char *s, size_t pos, size_t n) const; // 在字符串中找str所有字符的[pos, pos+n)部分第一次出现的位置
    size_t find_first_of(char c, size_t pos = 0) const;// 在字符串中找字符c第一次出现的位置
    
    // 获取子字符串
    string substr (size_t pos = 0, size_t len = npos) const; // 返回字符串的[pos, pos+len)部分
    

    友元函数

    void swap (string &x, string &y); // 交换两个字符串
    istream& getline (istream &is, string &str, char delim); //从输入流中获取一行,delim是分隔符,默认是'\n'
    

    成员常量

    npos // size_t类型的最大值,通常作为find函数返回的不存在位置,也可以被当做字符串的结尾位置
    

    trim函数

    std::string &trim(std::string &s) {  
        if (s.empty()) return s;  
        s.erase(0, s.find_first_not_of(" "));
        s.erase(s.find_last_not_of(" ") + 1);
        return s;  
    }  
    

    split函数

    void split(const std::string &s, std::string delim, std::vector<std::string> &res) {
        size_t last = 0;
        size_t index = s.find_first_of(delim, last);
        while (index != std::string::npos) {
            if (index > last) res.push_back(s.substr(last, index - last));
            last = index + 1;
            index = s.find_first_of(delim, last);
        }
        if (index > last)
            res.push_back(s.substr(last, index - last));
    }
    

    相关文章

      网友评论

          本文标题:C++ string类

          本文链接:https://www.haomeiwen.com/subject/uwgbtxtx.html