美文网首页C++
c++STL容器适配器--Apple的学习笔记

c++STL容器适配器--Apple的学习笔记

作者: applecai | 来源:发表于2019-10-24 22:10 被阅读0次

第三章习题1

从键盘读取任意个数的单词,然后把它们保存到deque<string>容器中,再把容器中的单词复制到List<string>容器中,并将列表中的内容排列成升序,最后输出排序结果。

我的练习代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <list>
#include <deque>
#include <queue>
using namespace std;
void test3_1()
{
  std::deque<std::string> words;
  std::string word;
  std::cout << "Enter words separated by spaces, enter Ctrl+Z on a separate line to end:\n";
  while (true)
  {
    if ((std::cin >> word).eof())
    {
      std::cin.clear();
      break;
    }
    words.push_back(word);
  }
  std::cout << "The words in the list are:" << std::endl;
  priority_queue<string, vector<string>, greater<string>> listwords{begin(words), end(words)};
  while (!listwords.empty())
  {
    cout << listwords.top() << " ";
    listwords.pop();
  }
  cout << endl;
}

输出结果

Enter words separated by spaces, enter Ctrl+Z on a separate line to end:
Hello Apple How Are You?
^Z
The words in the list are:
Apple Are Hello How You?

相关文章

  • c++STL容器适配器--Apple的学习笔记

    第三章习题1 从键盘读取任意个数的单词,然后把它们保存到deque 容器中,再把容器中的单词复制到List 容器中...

  • c++STL使用序列容器--Apple的学习笔记

    《C++标准模板库实战》的第二章看完了。习题操练,用的c++14编译的。 一,P87 习题2 可以从键盘读取任意个...

  • 9.7 容器适配器--C++ Primer ReadNote

    9.7 容器适配器 顺序容器适配器主要分三类: 思考:有没有关系容器适配器? 思考:queue默认基础容器是deq...

  • STL 源码剖析

    GitHub参考STL"源码"剖析-重点知识总结C++STL自己总结 序列式容器 所谓序列式容器,其中的元素都可序...

  • C++STL sort排序内部机制--Apple的学习笔记

    STL sort实现原理: STL中的sort并非只是普通的快速排序,除了对普通的快速排序进行优化,它还结合了插入...

  • C++ 设计模式 —— 16.迭代器模式

    迭代器模式:一种行为型设计模式 应用场景:刚学习C++STL容器的时候,自然也学习了迭代器。当时很不懂为什么指针可...

  • C++STL容器——vector容器

    vector 向量(vector)是动态数组,在堆中分配内存,元素连续存放,有保留内存,如果减少大小后,内存也不会...

  • Adapter 适配器模式

    设计原则学习笔记 设计模式学习笔记 作用 将原本不匹配的接口转化成匹配的接口 类图 类适配器 对象适配器 另有接...

  • linux c/c++面试知识点整理(七)

    61、for_each的用法? for_each是C++STL中用来遍历容器的函数模板,有3个参数: 第...

  • 一、容器

    (1)容器分类 <1>顺序容器(序列容器) <2>关联容器 <3>容器适配器 (2)vector容器 <1>概念 ...

网友评论

    本文标题:c++STL容器适配器--Apple的学习笔记

    本文链接:https://www.haomeiwen.com/subject/irhjvctx.html