std::string 各种操作

本文通过一个具体的C++程序示例介绍了如何使用标准库中的字符串类进行操作,包括查找特定子串的位置、提取子串以及替换字符串内容等实用功能。

#include <string>    
std::string str_strtest="username=123@qq.com&password=123123";
    std::cout <<"str_strtest.back():111 " << str_strtest.back() <<std::endl;
    std::size_t found_name = str_strtest.find("username=");
    std::size_t found_and = str_strtest.find("&");
    std::size_t found_password = str_strtest.find("password=");
    if(found_name!=std::string::npos)
    {
        std::cout <<"found_name: " << found_name <<std::endl;
    }
    if(found_and!=std::string::npos)
    {
        std::cout <<"found_and: " << found_and <<std::endl;
    }
    if(found_password!=std::string::npos)
    {
        std::cout <<"found_password: " << found_password <<std::endl;
    }
    std::string str_myusername, str_mypassword;
    str_myusername.assign(str_strtest,found_name+9,found_and-9);
    std::cout << "str_myusername: " << str_myusername << std::endl;
    std::cout <<"str_strtest.back():222 " << str_strtest.back() <<std::endl;
    str_mypassword.assign(str_strtest,found_and+10,str_strtest.back());
    std::cout << "str_mypassword: " << str_mypassword << std::endl;
//    if(iter !=std::string::npos)
//    {
//        std::cout << " str_strtest iter: " << iter << std::endl;
//        str_strtest.replace(str_strtest.find("weifuliang"),10,"sucuihuang_hahhah");
//        std::cout << "str_strtest : " << str_strtest << std::endl;
//    }

//    std::string str("There are two needles in this haystack with needles.");
//    std::string str2("needle");
//    // different member versions of find in the same order as above:
//    std::size_t found = str.find(str2);
//    if (found != std::string::npos)
//        std::cout << "first 'needle' found at: " << found << '\n';
//    found = str.find("needles are small", found + 1, 6);
//    if (found != std::string::npos)
//        std::cout << "second 'needle' found at: " << found << '\n';
//    found = str.find("haystack");
//    if (found != std::string::npos)
//        std::cout << "'haystack' also found at: " << found << '\n';
//    found = str.find('.');
//    if (found != std::string::npos)
//        std::cout << "Period found at: " << found << '\n';
//    // let's replace the first needle:
//    str.replace(str.find("needles"), str2.length(), "preposition 123123123122312231123");  //replace 用法
//    std::cout << str << '\n';
你提供的 `String` 类是一个从 `StringBase` 继承而来的类,它实现了一些标准的接口方法(如 `mutate()`、`calculate_size()` 等),并且包含了一些静态常量向量和成员变量。 下面我将: 1. 为你补全该类的完整定义; 2. 提供一个完整的实现示例; 3. 解释每个部分的作用; 4. 列出相关问题。 --- ## ✅ 完整代码实现 ### 头文件 `String.h` ```cpp #pragma once #include "StringBase.h" #include "MutatorConfig.h" #include <string> #include <vector> #include <map> class String : public StringBase { public: // 默认构造函数 String() = default; // 带参构造函数 String(MutatorConfig mutator_config, const std::string& value = {}, const std::string& string_type = ""); // 覆盖虚函数 void initialize_mutators() override; std::int64_t calculate_size() override; std::int64_t calculate_bit_size() override; std::string mutate() override; // 成员变量 std::map<std::string, std::string> hints; std::int64_t size; // 静态常量向量 static const std::vector<std::string> vector1; static const std::vector<std::string> vector2; static const std::vector<std::string> vector3; static const std::vector<std::string> vector4; static const std::vector<std::string> vector5; static const std::vector<std::string> vector6; }; ``` --- ### 实现文件 `String.cpp` ```cpp #include "String.h" #include <iostream> // 初始化静态常量向量 const std::vector<std::string> String::vector1 = {"a", "b", "c"}; const std::vector<std::string> String::vector2 = {"x", "y", "z"}; const std::vector<std::string> String::vector3 = {"alpha", "beta", "gamma"}; const std::vector<std::string> String::vector4 = {"red", "green", "blue"}; const std::vector<std::string> String::vector5 = {"up", "down", "left", "right"}; const std::vector<std::string> String::vector6 = {"one", "two", "three"}; // 构造函数实现 String::String(MutatorConfig mutator_config, const std::string& value, const std::string& string_type) : StringBase(value, string_type), mutator_config(mutator_config) { this->size = static_cast<std::int64_t>(value.size()); } // 初始化变异器 void String::initialize_mutators() { std::cout << "Initializing string mutators..." << std::endl; // 这里可以注册或初始化不同的变异策略 } // 返回字符串长度 std::int64_t String::calculate_size() { return size; } // 返回字节位数 std::int64_t String::calculate_bit_size() { return size * 8; } // 执行变异操作 std::string String::mutate() { // 示例:返回 vector1 的第一个元素作为变异结果 if (!vector1.empty()) { return vector1[0]; } return ""; } ``` --- ## 📌 解释说明 - **构造函数**: - 默认构造函数 `String() = default;` 允许无参数创建对象。 - 带参构造函数用于初始化配置、值、类型等信息,并调用基类构造函数。 - **虚函数重写**: - `initialize_mutators()`:初始化所有可用的变异器(mutator)。 - `calculate_size()`:计算字符串大小。 - `calculate_bit_size()`:计算字节位数。 - `mutate()`:执行实际的变异逻辑,返回新的字符串。 - **静态常量向量**: - `vector1 ~ vector6` 是一些预定义的字符串列表,可能用于不同类型的变异策略。 - **成员变量**: - `hints`:用于存储提示信息,可能是用于 UI 或日志记录。 - `size`:缓存字符串长度。 --- ##
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值