美文网首页
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和C语言中的字符char

    c++中的字符串string 在c++中使用string类,必须在程序中包含头文件string #include ...

  • 高质量C++/C编程指南(转)

    1 有如下的c++类 class String { public: String(const char *str ...

  • 字符串

    C++提供了两种字符串的表示形式: C风格字符 C++引入的string类型 C风格 函数 C++中的String类

  • string的用法

    1、使用string头文件 要想使用标准C++中string类,必须要包含: 或者 string和wstring的...

  • C/C++语言基础

    语言基础 字符串类-string 常量与变量 运算符 程序和语句 字符串类-string (属于类类型)(c++中...

  • C++ string类

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

  • C++ String 类

    源码 运行结果

  • C++STL整理

    C++ STL中最基本以及最常用的类或容器string、vector、set、list、map string 处理...

  • 十四、自定义String类

    用Sublime写的C++自定义String类,重点在重构!

  • C++ 简易String类

    以下是C++实现的简易String类,需要注意和小心函数声明里面的参数类型(特别是引用和const关键字的使用!)

网友评论

      本文标题:C++ string类

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