C++标准库中的函数模板,STL组件中数据类型转换用法使用解读


//验证STL标注库中的一些语法用法
#include <iostream>

int main()
{
    //String
    //--------------修改
    std::string str = "HelloWorld";

    // operator+=	Append to string (public member function )
    str += "+qwe";
    std::cout << "operator追加后为:" << str << std::endl;

    // append	Append to string (public member function )
    str.append("附加到字符串");
    std::cout << "append:" << str << std::endl;

    // push_back	Append character to string (public member function )
    str.push_back('q');
    std::cout << "push_back:" << str << std::endl;

    // assign	Assign content to string (public member function )
    str.assign("分配给str");  //会将原有里面的值覆盖,替换为新值
    std::cout << "assign:" << str << std::endl;

    // insert	Insert into string (public member function )
    str.insert(0, "qwe1");  //插入,注意插入时位置切割里面内容字节读取/乱码
    std::cout << "insert:" << str << std::endl;

    // erase	Erase characters from string (public member function )
    str.erase(0, 1);   //擦除,擦除0-1索引位置的内容
    std::cout << "erase:" << str << std::endl;

    // replace	Replace portion of string (public member function )
    str.replace(2, 3, "勾八"); // 替换索引2-3位置的值为"勾八",注意替换插入时位置切割里面内容字节读取/乱码
    std::cout << "replace:" << str << std::endl;

    // swap	Swap string values (public member function )
    std::string otherstr = "ojbk";  //替换,交换字符串
    str.swap(otherstr);
    std::cout << "swap:" << str << std::endl;
    std::cout << "swap原值:" << otherstr << std::endl;

    // pop_back	Delete last character (public member function )
    str.pop_back();
    std::cout << "删除最后一个元素pop_back:" << str << std::endl;

    //------------操作

    // c_str	Get C string equivalent (public member function )
    const char *cstr = str.c_str();   //获取字符串
    std::cout << "使用stoi函数将字符串转换为整数:" << str << std::endl;

    // data	Get string data (public member function )
    str.data();
    std::cout << "使用data函数获取字符串的数据:" << str << std::endl;

    // get_allocator	Get allocator (public member function )
    std::allocator<char> alloc = str.get_allocator();
    std::cout << "使用get_allocator函数获取分配器get_allocator:" << &alloc << std::endl;

    // copy	Copy sequence of characters from string (public member function )
    std::string copyStr = "copy的";
    str.copy(&copyStr[0], 1, 2);
    std::cout << "使用copy函数将字符串的一部分复制到另一个字符串:" << &copyStr << std::endl;

    // find	Find content in string (public member function )
    size_t pos = str.find("q");
    std::cout << "使用find函数查找子字符串在字符串中的位置:" << pos << std::endl;

    std::string str1 = "HelloWorld";

    // rfind	Find last occurrence of content in string (public member function )
    size_t lastPos = str1.rfind("l");
    std::cout << "使用rfind函数查找子字符串在字符串中最后一次出现的位置:" << lastPos << std::endl;

    // find_first_of	Find character in string (public member function )
    size_t firstPos = str1.find_first_of("l");
    std::cout << "使用find_first_of函数查找字符在字符串中的位置:" << firstPos << std::endl;

    // find_last_of	Find character in string from the end (public member function )
    size_t lastof_Pos = str1.find_last_of("l");
    std::cout << "使用find_last_of函数从字符串末尾开始查找字符在字符串中的位置:" << lastof_Pos << std::endl;

    // find_first_not_of	Find absence of character in string (public member function )
    size_t lastof_not_Pos = str1.find_first_not_of("H");
    std::cout << "使用find_first_not_of函数查找字符串中第一个不在指定字符集中的字符的位置:" << lastof_not_Pos << std::endl;

    //--------------字符串转换为对应类型:
    std::string str2 = "123456";

    // stoi 	Convert string to integer (function template )
    int intValue = std::stoi(str2);
    std::cout << "将字符串转换为整数:" << intValue << std::endl;

    // stol 	Convert string to long int (function template )
    long longValue = std::stoi(str2);
    std::cout << "将字符串转换为长整型:" << longValue << std::endl;

    // stoull 	Convert string to unsigned long long (function template )
    unsigned int unsignedValue = std::stoul(str);
    std::cout << "使用stoul函数将字符串转换为无符号整数: " << unsignedValue << std::endl;

    // stoll 	Convert string to long long (function template )
    unsigned long long unsignedlonglongValue = std::stoi(str2);
    std::cout << "将字符串转换为长长整型:" << unsignedlonglongValue << std::endl;

    // stof 	Convert string to float (function template )
    float floatValue = std::stof(str2);
    std::cout << "使用stof函数将字符串转换为浮点数: " << floatValue << std::endl;

    // stod 	Convert string to double (function template )
    double doubleValue = std::stod(str2);
    std::cout << "使用stod函数将字符串转换为双精度浮点数: " << doubleValue << std::endl;

    // stold	Convert string to long double (function template )
    long double longDoubleValue = std::stold(str2);
    std::cout << "使用stold函数将字符串转换为长双精度浮点数: " << longDoubleValue << std::endl;
    return 0;


}

部分额外解释:

// get_allocator	Get allocator (public member function )
    std::allocator<char> alloc = str.get_allocator();
    std::cout << "使用get_allocator函数获取分配器get_allocator:" << &alloc << std::endl;
这里面的分配器有什么作用,实际开发中怎么使用的?应用场景有哪些?比如?

分配器(Allocator)在C++中用于管理内存分配和释放的机制。在`std::string`中,你可以使用`get_allocator()`函数来获取与字符��对象关联的分配器。这个分配器的主要作用是为字符串对象分配和释放内存,它提供了一种抽象接口,允许你自定义内存管理策略,以满足特定的应用需求。 实际开发中,你可能会在以下情况下使用分配器:

1. **自定义内存管理**:如果你希望自定义字符串的内存管理方式,例如使用特定的内存池或内存分配策略,可以通过自定义分配器来实现。

2. **性能优化**:某些应用场景可能需要优化内存分配和释放的性能。通过选择合适的分配器,你可以针对特定的工作负载实现更高效的内存管理。

3. **与其他数据结构整合**:在某些情况下,你可能需要将字符串对象与其他自定义数据结构整合在一起,这时可以使用相同的分配器来确保内存一致性。

4. **跨平台兼容性**:不同平台和编译器可能有不同的默认分配器行为,使用自定义分配器可以提高代码的可移植性。

5. **调试和性能分析**:通过自定义分配器,你可以在内存分配和释放的过程中添加调试信息或性能分析代码,以便更好地监控和调试应用程序。

应用场景包括但不限于:

- **实时系统**:在实时系统中,内存分配和释放的性能和可预测性非常重要,因此可以使用自定义分配器来满足实时要求。

- **游戏开发**:游戏引擎和资源管理器通常需要高效的内存管理,自定义分配器可以帮助游戏开发者更好地控制内存。

- **嵌入式系统**:在资源有限的嵌入式系统中,内存管理可能需要特殊的优化,自定义分配器可以帮助减少内存碎片和提高性能。

- **大规模应用**:对于大规模的应用程序,如数据库管理系统,自定义分配器可以帮助优化内存使用,减少内存泄漏和性能问题。

总之,分配器提供了一种强大的工具,可以根据应用程序的需求来优化内存管理,但需要谨慎使用,确保正确地管理内存生命周期,以避免内存泄漏和潜在的问题。在实际开发中,通常需要根据具体场景和性能需求来决定是否使用自定义分配器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值