当前位置: 首页 > news >正文

C++:string 1

练习题:

这个题的思路是从前往后,从后往前同时找,不是字母的话就继续,是的话就交换。

代码:

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <string>
using namespace std;

//1、4个默认成员函数
void test_string1()
{
    string s1;
    string s2("hello");
    string s3("hello", 2);
    string s4(s2);
    string s5(s2, 1, 2);
    string s6(s2, 1);
    string s7(10, 'a');
    s1 = s7;

    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;
    cout << s6 << endl;
    cout << s7 << endl;
}

//遍历
void test_string2()
{

    string s1("hello");
    s1 += ' ';
    s1 += "world";
    cout << s1 << endl;

    //读  推荐用这种方式
    for (size_t i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " ";
    }

    //写
    for (size_t i = 0; i < s1.size(); i++)
    {
        s1[i] += 1;
    }
    cout << endl;

    //读
    for (size_t i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " ";
    }
    cout << endl;

    //迭代器
    //写
    string::iterator it = s1.begin();
    while (it != s1.end())
    {
        *it -= 1;
        ++it;
    }

    //读
    it = s1.begin();
    while (it != s1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    //范围for
    //C++11 -> 原理是被替换成迭代器
    for (auto ch : s1)
    {
        cout << ch << " ";
    }
    cout << endl;
}

int string2int(const string& str)
{
    int val = 0;
    string::const_iterator it = str.begin();
    while (it != str.end())
    {
        //*it = 'a';//const 迭代器不能修改
        val *= 10;
        val += *it-'0';
        ++it;
    }
    return val;
}

void test_string3()
{
    string s1("hello world!");
    //倒着往前遍历
    string::reverse_iterator rit = s1.rbegin();
    //读
    while (rit != s1.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;

    string nums("12345");
    cout << string2int(nums) << endl;

    //方向:正向和反向
    //属性:普通和const
}

void test_string4()
{
    string s1("hello world!");
    string s2("hello");
    cout << s1.size() << endl;
    cout << s2.size() << endl;
    cout << s1.length() << endl;
    cout << s2.length() << endl;

    cout << s1.capacity() << endl;
    cout << s2.capacity() << endl;

    s1 += "hehe";
    cout << s1.capacity() << endl;
    s1.clear();//只是清除了内容,并不将capacity置0
    cout << s1 << endl;
    cout << s1.capacity() << endl;
}

void test_string5()
{
    string s;
    //s.reserve(100);
    //s.resize(100, 'x');
    size_t sz = s.size();

    cout << "making s grow:\n";
    for (int i = 0; i < 100; i++)
    {
        s.push_back('c');
        if (sz != s.capacity())
        {
            sz = s.capacity();
            cout << "capacity changed:" << sz << endl;
        }
    }
}

void test_string6()
{
    //string s;
    //s.push_back('x');
    //s.append("x");

    //s += '1';
    //s += "hello";

    //cout << s << endl;

    string s;
    s += '1';
    s += "3456";
    cout << s << endl;
    s.insert(s.begin(), '0');
    cout << s << endl;
    s.insert(2, "2");
    cout << s << endl;
    s.erase(2, 3);
    cout << s << endl;
    s.erase(2, 10);
    cout << s << endl;
}


int main()
{
    //test_string1();
    //test_string2();
    //test_string3();
    //test_string4();

    //test_string5();
    test_string6();
    return 0;
}

相关文章:

  • 游戏状态管理:用Pygame实现场景切换与暂停功能
  • Java 日志:掌握本地与网络日志技术
  • 6.1腾讯技术岗2025面试趋势前瞻:大模型、云原生与安全隐私新动向
  • HTML与安全性:XSS、防御与最佳实践
  • 华为OD机试真题——二维伞的雨滴效应(2025A卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • 在WSL2+Ubuntu22.04中通过conda pack导出一个conda环境包,然后尝试导入该环境包
  • 【Linux网络】打造初级网络计算器 - 从协议设计到服务实现
  • 1.4 大模型应用产品与技术架构
  • 静态多态和动态多态的区别
  • 【Tauri】桌面程序exe开发 - Tauri+Vue开发Windows应用 - 比Electron更轻量!8MB!
  • 【高频考点精讲】实现垂直居中的多种CSS方法比较与最佳实践
  • BS架构与CS架构的对比分析:了解两种架构的不同特点与应用
  • 计算机网络 | 应用层(4)--DNS:因特网的目录服务
  • (done) 吴恩达版提示词工程 5. 推理 (情绪分类,控制输出格式,输出 JSON,集成多个任务,文本主题推断和索引,主题内容提醒)
  • 来自 Bisheng 关于微调的内容总结
  • [mysql]约束(上)
  • 19.【.NET 8 实战--孢子记账--从单体到微服务--转向微服务】--单体转微服务--当前项目拆分规划
  • 前端开发中列表无限加载功能的实现与优化
  • 神经网络与深度学习第四章-前馈神经网络
  • C++ 同步原语
  • “爱泼斯坦案”关键证人弗吉尼亚·朱弗雷自杀身亡
  • 文庙印象:一周城市生活
  • 商务部:汽车流通消费改革试点正在加快推进
  • 《我的后半生》:人生下半场,也有活力重启的可能
  • 山东一季度GDP为23466亿元,同比增长6.0%
  • 央行研究局局长答澎湃:持续优化跨境金融服务政策工具箱,有效支持企业走出去