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

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;
}

http://www.xdnf.cn/news/2235.html

相关文章:

  • YTJ笔记——FFT、NCC
  • Maven的聚合工程与继承
  • Pygame动画实战:让游戏角色动起来!
  • Java24 抗量子加密:后量子时代的安全基石
  • 华为盘古OS深度评测:构建AI自进化系统的实践密码
  • 第一性原理 | 从哲学本源到多领域实践的探索与应用
  • 腾讯二面:TCC分布式事务 | 图解TCC|用Go语言实现一个TCC
  • MyBatis 常用扩展组件详解(含代码示例)
  • 有源晶振与无源晶振详解:区别、应用与选型指南
  • 计算机视觉中的二值马尔科夫随机场
  • 代码随想录算法训练营第五十九天 | 1.ford算法精讲 卡码网94.城市间货物运输
  • 长短板理论——AI与思维模型【83】
  • 如何在 Windows 10 中使用 WSL 和 Debian 安装 Postgresql 和 Postgis
  • Vue3的内置组件 -实现过渡动画 TransitionGroup
  • 计算机二级MS Office第九套演示文稿
  • 隐私守护者的觉醒——大数据时代,我们如何对抗“透明人”危机?
  • 单链表专题(1)
  • 豆包,Kim,deepseek对比
  • Java——令牌技术
  • Oracle EBS 零金额的AP付款无法过账数据修复
  • 蓝桥杯赛场反思:技术与心态的双重修炼
  • 基于libdxfrw库读取样条曲线并离散为点
  • 在Linux虚拟机下使用vscode,#include无法跳转问题
  • 前端开发中shell的使用场景
  • 部署yolo到k230教程
  • cloud项目同一个服务,执行不同业务需求,nacos进行分组
  • 数据结构之单链表C语言
  • 论人际关系发展的阶段
  • Scratch——第19课 正话反说问题
  • 内存池管理项目——面试题总结