顺序容器 - string, stack, queue

顺序容器 - string, stack, queue

string的插入, 删除, 访问, 搜索等等;
以及栈, 队的适配器用法;


#include <iostream>
#include <cstdlib>
#include <string>
#include <stack>
#include <queue>

int main()
{
    //string;
    //substr(pos, n)拷贝, 从pos开始到n结束, pos默认为0;
    //insert插入;
    //append : 在string字符串之后插入元素;
    //replace : 将元素替换;
    //s.find(s1) : 查找在s中第一次出现s1的位置的下标;
    //s.rfind(s1) : 查找在s中最后出现s1的位置的下标;
    //s.find_first_of(s1) : 查找在s1字符串中任意字符第一次出现的下标;
    //s.find_last_of(s1) : 查找在s1字符串中任意字符最后出现的下标;
    //s.find_first_not_of(s1) : 查找在s1字符串不在s中第一次出现的下标;
    //s.find_last_not_of(s1) : 查找在s1字符串不在s中最后出现的下标;
    std::string s("hello world");
    std::string s1 = s.substr(0, 5);
    std::string s2 = s.substr(5);
    std::string s3 = s.substr(6, 10);

    std::string s4; s4.insert(0, s);                //s4插入s, 在s4的s[0]之前插入s;
    std::string s5; s5.insert(0, s, 1, s.size() - 2);//在s5的s5[0]之前插入s从 1 到 size - 2的元素;
    std::cout << "\n\nstring : \n" << s4 << "\n" << s5;
    s4.append("hello   hello");
    s5.replace(3, 4, "hello");                      //在s5的s[2]后面删除4个元素, 并插入"hello";
    std::cout << "\n" << s4 << "\n" << s5;

    std::string s6 = s;
    std::cout << '\n' << s6.find("o") << "  " << s6.rfind("o") << "  "
        << s6.find_first_of(s5) << "  " << s6.find_first_not_of(s5) << "  "
        << s6.find_last_of(s5) << "  " << s6.find_last_not_of(s5);



    //stack, 栈;
    //push : 入栈;
    //pop : 出栈;
    //top : 输出最后一个元素;
    //empty : 不为空栈;
    std::stack<int > intstack;
    for (int i = 0; i < 10; i++)
        intstack.push(i);
    std::cout << "\n\n stack : \n" << intstack.top() << "\n";
    while(!intstack.empty())
    {
        std::cout << intstack.top() << "  ";
        intstack.pop();
    }



    //queue, 队;
    //q.pop(); 删除第一位元素;
    //q.back(); 最后一位元素;
    //q.front(); 返回第一位元素;
    //q.top();  特殊的实用; priority_queue;
    //q.push(); 压入;
    std::queue<int > q;
    for (int i = 0; i < 10; i++)
        q.push(i);
    std::cout << "\n\n queue : \n" << q.back() << "\n";
    while (!q.empty())
    {
        std::cout << q.front() << "  ";
        q.pop();
    }



    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值