顺序容器

来源:互联网 发布:桌面主题软件哪个好 编辑:程序博客网 时间:2024/06/08 02:11

顺序容器 - 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;}
原创粉丝点击