栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【C++】string容器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++】string容器

目录

string 基本概念

string 构造函数

string 赋值操作

string 字符串拼接

string 查找和替换

string 字符串比较

string 字符读取

string 插入和删除

string 子串获取


string 基本概念

本质:

  • string 是 C++ 风格的字符串,string 本质是一个类

-----------------------

string 和 char* 的区别

  • char* 是一个指针
  • string 是一个类,类内部封装了 char* ,管理字符串,是一个 char* 型的容器

-----------------------

特点:

  • 使用的时候需要包含头文件
  • string 类内部封装了成员方法,如:查找 find、拷贝 copy、删除 delete、替代 replace、插入insert 等
  • string 管理 char* 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

string 构造函数

函数原型:

string();                  // 创建一个空的字符串 string str;

string(const char* s);     // 使用字符串 s 初始化

string(const string& str); // 使用一个 string 对象初始化另一个 string 对象

string(int n, char c);     // 使用 n 个字符 c 初始化

测试代码:

#include
using namespace std;
#include

void test() {
    string s1;

    const char* ch = "Anly";
    string s2(ch);
    cout << s2;

    const string str = "_blog";
    string s3(str);
    cout << s3;

    string s4(6, '6');
    cout << s4 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:


string 赋值操作

函数原型:

// char* 类型字符串复制给当前字符串
string& operator=(const char* s);     

// 把字符串 str 赋给当前的字符串
string& operator=(const string& str); 

// 字符 c 赋值给当前的字符串
string& operator=(char c);            

// 把字符串 s 赋给当前的字符串
string& assign(const char* s);  

// 把字符串 s 的前 n 个字符赋值给当前的字符串      
string& assign(const char* s, int n); 

// 把字符串 s 赋给当前字符串
string& assign(const string& str);   

// 用 n 个字符 c 赋给当前字符串   
string& assign(int n, char c);        

测试代码:

#include
using namespace std;
#include

void test() {
    // string& operator=(const char* s);
    string str1;
    str1 = "Anly_bolg";
    cout << str1 << endl;

    // string& operator=(const string& str);
    string str2;
    str2 = str1;
    cout << str2 << endl;

    // string& operator=(char c);
    string str3;
    str3 = 'a';
    cout << str3 << endl;

    // string& assign=(const char* s);
    string str4;
    str4.assign("C++");
    cout << str4 << endl;

    // string& assign(const char* s, int n);
    string str5;
    str5.assign("studyC++", 5);
    cout << str5 << endl;

    // string& assign(const string& str);
    string str6;
    str6.assign(str5);
    cout << str6 << endl;

    // string& assgin(int n, char c);
    string str7;
    str7.assign(6, '6');
    cout << str7 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:

string 的赋值方式很多,operator= 这种方式是比较常用


string 字符串拼接

函数原型:

// 重载 += 操作符
string& operator+=(const char* s);     

// 重载 += 操作符
string& operator+=(const char c);      

// 重载 += 操作符
string& operator+=(const string& str);

// 把字符串 s 连接到当前字符串结尾
string& append(const char* s);         

// 把字符串s 的前 n 个字符连接当前字符串尾
string& append(const char* s, int n);  

// 同 operator+=(const string& str);
string& append(const string& str);     

// 字符串 s 中从 pos 开始的 n 个字符连接
string& append(const string& str, int pos, int n);

测试代码:

#include
using namespace std;
#include

void test() {
    // string& operator+=(const char* s);
    string str1 = "Anly";
    str1 += "_blog";
    cout << str1 << endl;

    // string& operator+=(const char c);
    string str2 = "Anly_blo";
    str2 += 'g';
    cout << str2 << endl;

    // string& operator+=(const string& str);
    string str3 = "Anly";
    string str4 = "_blog";
    str3 += str4;
    cout << str3 << endl;

    // string& append(const char* s);
    string str5 = "Anly";
    str5.append("_blog");
    cout << str5 << endl;
    
    // string& append(const char* s, int n);
    string str6 = "Anly";
    str6.append("_blogC++", 5);
    cout << str6 << endl;

    // string& append(const string& str);
    string str7 = "Anly";
    string str8 = "_blog";
    str7.append(str8);
    cout << str7 << endl;

    // string& append(const string& str, int pos, int n);
    string str9 = "Anly";
    string str10 = "_blogC++";
    str9.append(str10, 0, 5); // pos下标
    cout << str9 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:


string 查找和替换

功能:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串

----------------

函数原型:

// 查找 str 第一次出现位置,从 pos 开始查找,pos(下标) 有默认参数
int find(const string& str, int pos = 0) const; 

// 查找 s 第一次出现位置,从 pos 开始查找,pos 有默认参数   
int find(const char* s, int pos = 0) const;         

// 从 pos 位置查找 s 的前 n 个字符第一次位置,无默认参数
int find(const char* s, int pos, int n) const;      

// 查找字符 c 第一次出现位置,pos 有默认参数
int find(const char c, int pos = 0) const;          

// 查找 str 最后一次位置,从 pos 开始查找
int rfind(const string& str, int pos = npos) const; 

// 查找 s 最后一次出现位置,从 pos 开始查找
int rfind(const char* s, int pos = npos) const;     

// 从 pos 查找 s 的前 n 个字符最后一次位置
int rfind(const char* s, int pos, int n) const; 

// 查找字符 c 最后一次出现位置    
int rfind(const char c, int pos = 0) const;         

// 替换从 pos 开始 n 个字符为字符串 str
string& replace(int pos, int n, const string& str); 

// 替换从 pos 开始 n 个字符为字符串 s
string& relpace(int pos, int n, const char* s);     

测试代码:

#include
using namespace std;
#include

// 查找
void test1() {
    // find
    string str1 = "fgabcdefg";
    string str2 = "de";
    int pos = (int)str1.find(str2);
    cout << pos << endl; // 3    
    pos = (int)str1.find("df");
    cout << pos << endl; // -1 ,未找到返回 -1
    if (pos == -1) {
        cout << "未找到字符串" << endl;
    }
    else {
        cout << "找到字符串" << endl;
    }

    // rfind 与 find 区别
    
    // rfind 从右往左
    pos = (int)str1.rfind("fg");
    cout << pos << endl; // 7
    // find  从左往右
    pos = (int)str1.find("fg");
    cout << pos << endl; // 0
}

// 替代
void test2() {
    string str1 = "abcdefg";
    str1.replace(1, 3, "1111"); // 从 1 号位置起,3 个字符替换为 1111
    cout << str1 << endl; // a1111efg   bcd 变成 1111
}

int main() {
    test1();
    test2();
    system("pause");
    return 0;
}

运行结果:

  • find 查找是从左往右,rfind 查找从右往左
  • find 找到字符串后返回查找的第一个字符位置(下标),找不到返回 -1 
  • replace 在替代时,要指定从哪个位置开始,多少个字符,替换成什么字符串

string 字符串比较

功能:

  • 字符串之间比较

----------------

比较方式:

  • 字符串比较是按字符的 ASCII 码进行对比
  • = 返回 0
  • > 返回 1
  • < 返回 -1

----------------

函数原型:
 

// 与字符串 s 比较
int compare(const string& str) const;

int compare(const char*s) const;

 测试代码:

#include
using namespace std;
#include

void test() {
    string str1 = "hello";
    string str2 = "hell0";

    if (str1.compare(str2) == 0) {
        cout << "str1 和 str2 相等" << endl;
    }
    else if (str1.compare(str2) == 1) {
        cout << "str1 大于 str2" << endl;
    }
    else if (str1.compare(str2) == -1) {
        cout << "str1 小于 str2" << endl;
    }
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:

主要比较两个字符串是否相等,比较大小意义不大


string 字符读取

string 中单个字符读取方式两种:

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

// 通过 at 方式读取字符
char& at(int n);

测试代码:

#include
using namespace std;
#include

void test() {   
    string str = "hello";

    // [] 方式 访问单个字符
    for (int i = 0; i < str.size(); i++) // size 获取字符串长度
    {
        cout << str[i] << "*";
    }
    cout << endl;

    // at 方式 访问单个字符
    for (int i = 0; i < str.size(); i++) 
    {
        cout << str.at(i) << "-";
    }
    cout << endl;

    // [] 方式 修改单个字符 
    str[0] = 'H';
    cout << str << endl; // Hello

    // at 方式 修改单个字符
    str.at(1) = 'E';
    cout << str << endl; // HEllo
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:


string 插入和删除

功能:

  • 对 string 字符串进行插入和删除字符操作

------------

函数原型:

// 插入字符串
string& insert(int pos, const char* s);

// 插入字符串
string& insert(int pos, const string& str);

// 在指定位置插入 n 个字符 c
string& insert(int pos, int n, char c);

// 删除从 pos 开始的 n 个字符
string& erase(int pos, int n = npos);

测试代码:

#include
using namespace std;
#include

void test() {   
    string str = "_blog";

    // insert 插入(前)
    str.insert(0, "Anly"); // 本来是 0 的位置上插入
    cout << str << endl; // Anly_blog

    // 删除
    str.erase(4, 5); // 从第 4 个位置开始,删除 5 个
    cout << str << endl; // Anly

}

int main() {
    test();
    system("pause");
    return 0;
}

 运行结果:


string 子串获取

功能:

  • 从字符串中获取想要的子串

----------------

函数原型:

// 返回由 pos 开始的 n 个字符组成的字符串,pos 有默认参数
string& substr(int pos = 0, int n = npos) const;

测试代码:

#include
using namespace std;
#include

void test() {   
    // 找邮箱用户名
    string email = "Anly_blog@gmail.com";
    int npos = email.find('@');
    string UserName = email.substr(0, npos);
    cout << "UserName:" << UserName << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

运行结果:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027254.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号