C++ string(29)

一、string

数据结构:连续的存储空间,用一个char*指向这片空间
迭代器:随机访问迭代器

string和c风格字符串对比:

  1. Char是一个指针,String是一个类
    string封装了char
    ,管理这个字符串,是一个char*型的容器。
  2. String封装了很多实用的成员方法
    查找find,拷贝copy,删除delete 替换replace,插入insert不用考虑内存释放和越界
  3. string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

1.1 string构造函数

string();//创建一个空的字符串 例如: string str;      
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化
void test01() {
    string s1;
    string s2(10,'a');
    string s3(s2);
    string s4("hello world");
}

1.2 string 基本赋值操作

string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string&s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符赋值给当前的字符串
string& assign(const char *s);//把字符串s赋给当前的字符串
string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string&s);//把字符串s赋给当前字符串
string& assign(int n, char c);//用n个字符c赋给当前字符串
string& assign(const string&s, int start, int n);//将s从start开始n个字符赋值给字符串,如s=hello,那么n=3,start=1,那么是hel中从e开始赋值3个字符
void test02() {
    string s1;
    s1 = "hello";
    cout << s1 << endl;

    string s2;
    s2.assign(s1);
    cout << s2 << endl;

    s2.assign("world");
    cout << s2 << endl;

    s2.assign(s1,1,3);
    cout<

1.3 string存取字符操作

char&operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符

void test03() {
    string s = "hello world";
    for (int i = 0; i < s.size(); i++)
    {
        cout << s[i] << " ";
    }
    cout << endl;

    for (int i = 0; i < s.size(); i++)
    {
        cout << s.at(i) << " ";
    }
    cout << endl;
    // []和at的区别:[]访问元素时,越界不抛出异常,返回空字符串,at越界会抛出异常

    try
    {
        //cout << s[100] << endl;
        cout << s.at(100) << endl;
        
    }
    catch(const std::exception &e)
    {

        cout << e.what() << endl;
    }
    
}

1.4 string拼接操作

string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string&s);//同operator+=()
string& append(const string&s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c

void test04() {
    string s1 = "aaa";
    s1 += "bbb";
    s1 += 'c';
    cout << s1 << endl;

    s1.append("dddddddd",3);
    cout << s1 << endl;
    
    s1.append(3,'e');
    cout << s1 << endl;
    
    string s2 = "fffggg";
    s1.append(s2);
    cout << s1 << endl;

    s1+=s2;
    cout << s1 << endl;

    s1.append(s2,2,3);
    cout << s1 << endl;

}

1.5 string查找和替换

int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;  //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const;  //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const;  //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
void test05() {
    string s = "abcdefgd";
    cout << s.find('d') << endl; // 3

    cout << s.rfind('d') << endl; // 7

    int res = s.find("kkkk"); // string.find()返回值,如果返回给一个int或long int类型,则返回值为-1;若返回一个string::size_type类型值则返回2^32-1;
    cout << res << endl; // -1

    s.replace(2,4,"AAA");

    cout << s << endl;

}

1.6 string比较操作

compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。

int compare(conststring&s) const;//与字符串s比较
int compare(constchar *s) const;//与字符串s比较
void test06() {
    string s1 = "hello";
    string s2 = "hello";
    string s3 = "Hello";

    const char *str = "world";
    
    if (s1.compare(s2) == 0)
    {
        cout << "s1 == s2" << endl;
    }

    int res = s2.compare(str); // -15 'h' 104 'w' 119
    cout << res << endl;
    if (res == 0)
    {
        cout << "s2 == str" << endl;
    }else{
        cout << "s2 != str" << endl;
    }

    int res2 = s2.compare(s3); // 32   'h' 104 'H' 72 
    cout << res2 << endl;

}

1.7 string子串

string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
void test07() {
    string email = "hello [email protected]";
    int pos = email.find('@');
    string username = email.substr(0,pos); // hello world
    cout << username << endl;

    string prex = email.substr(pos+1,email.size()-pos-1); // itcast.com
    cout << prex << endl;

    prex = email.substr(pos+1); // itcast.com
    cout << prex << endl;
}

1.8 string插入和删除操作

string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符 
void test08() {
    string s1 = "aaaa";
    s1.insert(2,"AAA"); // aaAAAaa
    cout << s1 << endl;

    s1.insert(3,5,'C'); // aaACCCCCAAaa
    cout << s1 << endl;

    s1.erase(2,3);
    cout << s1 << endl; // aaCCCAAaa

}

1.9 string和c-style字符串转换

//string 转 char*
   string str = "itcast";
   const char* cstr = str.c_str();
//char* 转 string 
   char* s = "itcast";
   string s = str(s);
void test09() {
    const char * str = "hello";
    string s1 = string(str);
    cout << s1 << endl;

    const char *cstr = s1.c_str();
    cout << cstr << endl;

}

1.10 用迭代器遍历字符串

void test11() {
    string s = "hello world";

    for(string::iterator it = s.begin(); it != s.end(); ++it){
        cout << *it << " ";
            
    }
    cout << endl;

    // 反向遍历
    for(string::reverse_iterator it = s.rbegin(); it != s.rend(); ++it){
        cout << *it << " ";
    }
    cout << endl;
    
}

注意:

  1. [] 和 at() 的区别: [] 越界会返回空字符串,at()越界抛出异常
  2. 字符串内存重新分配,[] 和 at()获取的字符引用,再次使用时可能会出错

你可能感兴趣的:(C++ string(29))