C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)

简介: C++字符串string容器(构造、赋值、拼接、查找、替换、比较、存取、插入、删除、子串)

一、字符串构造

// 字符串的构造
void test01() {
    //默认构造
    string s1;
    //使用char*字符串构造
    const char *str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
    //使用string对象初始化
    string s3(s2);
    cout << "s3=" << s3 << endl;
    //使用n个字符初始化
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
};

二、字符串赋值

/**
 * 赋值操作
 * 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个字符赋给当前字符串
 */
void test02() {
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;
    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;
    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;
    string str4;
    str4.assign("hello C++");
    cout << "str4 = " << str4 << endl;
    string str5;
    str5.assign("hello C++", 5);
    cout << "str5 = " << str5 << endl;
    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;
    string str7;
    str7.assign(10, 'e');
    cout << "str7 = " << str7 << endl;
};

三、字符串拼接

//string字符串拼接
/**
 *string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& operator+=(const string& str);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=(const string& str)
string& append(const string &s,int pos,int n); //字符s中从pos开始的n个字符连接到字符串结尾
  */
void test03() {
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << "str1 = " << str1 << endl;
    str1 += ":";
    cout << "str1 = " << str1 << endl;
    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;
 
    string str3 = "I";
    str3.assign(" love ");
    cout << "str3 = " << str3 << endl;
 
    str3.append("game abcde", 5);
    cout << "str3 = " << str3 << endl;
 
    // str3.append(str2);
    str3.append(str2, 0, 3);
    cout << "str3 = " << str3 << endl;
 
};

四、字符串查找

//字符串查找
void test04() {
    string str = "abcdefgde";
    // 从左往右查
    int pos = str.find("de");
    cout << "pos = " << pos << endl;
    //从右往左查
    pos = str.rfind("de");
    cout << "rfind pos = " << pos << endl;
};

五、字符串替换

//字符串替换
void test05() {
    string str1 = "abcdefg";
    //从1位置起2个字符,替换为"1231231"
    str1.replace(1, 2, "1231231");
    // str1=a1231231defg
    cout << "str1=" << str1 << endl;
};

六、字符串比较

//字符串比较
// = 返回 0
// > 返回 1
//< 返回 -1
void test06() {
    string str1 = "aello";
    string str2 = "hello";
    if (str1.compare(str2) == 0) {
        cout << "str1等于str2" << endl;
    } else if (str1.compare(str2) > 0) {
        cout << "str1大于str2" << endl;
    } else {
        cout << "str1小于str2" << endl;
    }
};

七、字符串存取

//string 字符存取
void test07() {
    string str = "hello";
    // cout << "str=" << str << endl;
    //1、通过[]访问单个字符
    for (int i = 0; i < str.size(); ++i) {
        cout << str[i] << " ";
    }
    cout << endl;
    //2、通过at方法访问单个字符
    for (int i = 0; i < str.size(); ++i) {
        cout << str.at(i) << " ";
    }
    cout << endl;
    //修改单个字符
    str[0] = 'x';
    str.at(1) = 'x';
    // xxllo
    cout << "str=" << str << endl;
}

八、字符串插入删除

/*
 * string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
 */
//字符串的插入和删除
void test08() {
    string str = "hello";
//     插入
    str.insert(1, "111");
    // h111ello
    cout << str << endl;
    //hello
    str.erase(1, 3);
    cout << str << endl;
}

九、子串

//string子串
void test09() {
    string str = "abcdef";
    string subStr = str.substr(1, 3);
    // subStr=bcd
    cout << "subStr=" << subStr << endl;
 
    string email = "zhangsan@sina.com";
    //从邮件地址中获取用户名信息
    int pos = email.find("@");
    cout<<pos<<endl;
    string userName = email.substr(0, pos);
    cout << userName << endl;
}

十、测试

#include <iostream>
#include <string>
 
using namespace std;
 
// 字符串的构造
void test01() {
    //默认构造
    string s1;
    //使用char*字符串构造
    const char *str = "hello world";
    string s2(str);
    cout << "s2=" << s2 << endl;
    //使用string对象初始化
    string s3(s2);
    cout << "s3=" << s3 << endl;
    //使用n个字符初始化
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
};
 
/**
 * 赋值操作
 * 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个字符赋给当前字符串
 */
void test02() {
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;
    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;
    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;
    string str4;
    str4.assign("hello C++");
    cout << "str4 = " << str4 << endl;
    string str5;
    str5.assign("hello C++", 5);
    cout << "str5 = " << str5 << endl;
    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;
    string str7;
    str7.assign(10, 'e');
    cout << "str7 = " << str7 << endl;
};
//string字符串拼接
/**
 *string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& operator+=(const string& str);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=(const string& str)
string& append(const string &s,int pos,int n); //字符s中从pos开始的n个字符连接到字符串结尾
  */
void test03() {
    string str1 = "我";
    str1 += "爱玩游戏";
    cout << "str1 = " << str1 << endl;
    str1 += ":";
    cout << "str1 = " << str1 << endl;
    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;
 
    string str3 = "I";
    str3.assign(" love ");
    cout << "str3 = " << str3 << endl;
 
    str3.append("game abcde", 5);
    cout << "str3 = " << str3 << endl;
 
    // str3.append(str2);
    str3.append(str2, 0, 3);
    cout << "str3 = " << str3 << endl;
 
};
 
//字符串查找
void test04() {
    string str = "abcdefgde";
    // 从左往右查
    int pos = str.find("de");
    cout << "pos = " << pos << endl;
    //从右往左查
    pos = str.rfind("de");
    cout << "rfind pos = " << pos << endl;
};
 
//字符串替换
void test05() {
    string str1 = "abcdefg";
    //从1位置起2个字符,替换为"1231231"
    str1.replace(1, 2, "1231231");
    // str1=a1231231defg
    cout << "str1=" << str1 << endl;
};
 
//字符串比较
// = 返回 0
// > 返回 1
//< 返回 -1
void test06() {
    string str1 = "aello";
    string str2 = "hello";
    if (str1.compare(str2) == 0) {
        cout << "str1等于str2" << endl;
    } else if (str1.compare(str2) > 0) {
        cout << "str1大于str2" << endl;
    } else {
        cout << "str1小于str2" << endl;
    }
};
 
    //string 字符存取
    void test07() {
        string str = "hello";
        // cout << "str=" << str << endl;
        //1、通过[]访问单个字符
        for (int i = 0; i < str.size(); ++i) {
            cout << str[i] << " ";
        }
        cout << endl;
        //2、通过at方法访问单个字符
        for (int i = 0; i < str.size(); ++i) {
            cout << str.at(i) << " ";
        }
        cout << endl;
        //修改单个字符
        str[0] = 'x';
        str.at(1) = 'x';
        // xxllo
        cout << "str=" << str << endl;
    }
/*
 * string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
 */
//字符串的插入和删除
void test08() {
    string str = "hello";
//     插入
    str.insert(1, "111");
    // h111ello
    cout << str << endl;
    //hello
    str.erase(1, 3);
    cout << str << endl;
}
 
//string子串
void test09() {
    string str = "abcdef";
    string subStr = str.substr(1, 3);
    // subStr=bcd
    cout << "subStr=" << subStr << endl;
 
    string email = "zhangsan@sina.com";
    //从邮件地址中获取用户名信息
    int pos = email.find("@");
    cout<<pos<<endl;
    string userName = email.substr(0, pos);
    cout << userName << endl;
}
 
int main() {
    // test01();
    // test02();
    // test03();
    // test04();
    // test05();
    // test06();
    // test07();
    // test08();
    test09();
    system("pause");
    return 0;
}
 
subStr=bcd
8
zhangsan

目录
相关文章
|
4天前
|
C++ 容器
C++之Queue容器
C++之Queue容器
10 3
|
4天前
|
C++ 容器
C++之set/multiset容器
C++之set/multiset容器
6 1
|
4天前
|
C++ 容器
C++之stack容器
C++之stack容器
12 1
|
4天前
|
C++ 容器
C++之评委打分案例(vector与deque容器练习)
C++之评委打分案例(vector与deque容器练习)
8 1
|
4天前
|
C++ 容器
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
C++之deque容器(构造、赋值、大小、插入与删除、存取、排序)
5 1
|
1天前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
3天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
3天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
8 0
|
4天前
|
C++ 容器
C++之map/multimap容器
C++之map/multimap容器
5 0
|
4天前
|
存储 C++ 容器
C++之list容器
C++之list容器
6 0