zoukankan      html  css  js  c++  java
  • 用 string 进行插入、替代、查找输出下标等操作

     6 string s;
     7 s = "124567";
     8 string::iterator it;
     9 it = s.begin();//让s指向第一个元素
    10 cout << s;
    11 system("pause");
    12 return 0;
    

    在未进行插入之前的运行结果:

    进行插入操作后运行结果,在字符串第二个元素后面进行插入元素 ‘3’ 操作:

    1 string::iterator it;
    2 it = s.begin();
    3 s.insert(it + 2, '3');

    运行结果如下所示:

    替代操作:

    将初始化的数组从第三个下标开始的元素连续4个替代

    1 s.replace(3, 4, "good");

    未替代前运行结果:

    替代后运行结果:

     查找操作:

    初始化:

    1 string s;
    2  s = "I am people";

    查找 a 元素 单个元素并输出下标:

    1  s.find('a');
    2  cout << s.find('a');

    运行结果:

    中间有空格影响:

    把空格去了:

    初始化:

     s = "Iampeople";
     cout << s<<endl;

    输出结果:

    查找people 单词:

    初始化:

    string s;
     s = "I am people";
     cout << s<<endl;

    查找

    1  s.find("people");
    2  cout << s.find("people");

    运行结果:

    不知道为什么,打出的只是首个元素的下标,并且系统自动把空格当一个元素处理了

  • 相关阅读:
    css3 object-fit详解
    Timing path
    IUS
    FIFO深度
    UVM中的class--2
    UVM中的class
    Binding
    Concurrent Assertion
    Immediate assertion
    sdf
  • 原文地址:https://www.cnblogs.com/Mr210843013/p/4979013.html
Copyright © 2011-2022 走看看