CS106L stream练习

Problem 1: The More Simple Problem

Problem

Imagine you have a super exciting book that contains a lot of words, and you want to know which words are the most common in the book. Can you write a program in C++ that reads the book, counts the number of occurrences of each word, and prints the results to the console, so you can check which words are the most common in your book?

Input

A text file called "words.txt" that contains multiple lines of text from your super exciting book.

Output

A list of words and their respective number of occurrences in the book, one word per line.

Note

The output should be case-sensitive, words in uppercase and lowercase should be treated as distinct words.

With this question prompt, you are expected to use std::ifstream, std::getline, and std::stringstream to read the book, extract each word, and count the occurrences of each word. Have fun with it!

We set up an online IDE for you to solve the problem. Here's a link to the practice environment and here's a link to the solution. Click fork this to create a copy of the file that you can make changes to. Make sure to try the problem out before looking at the solution!

  1.不使用getline 和 stringstream:

#include<fstream>       //添加 ifstream
#include <map>          //添加 map
#include <iostream>     //添加 cout
#include <cassert>      //添加 assert
int main() {
    // TODO: Write your code here

    // ifstream 流用于文件输入流
    std::ifstream file("words.txt");

    // 返回文件打开情况,若失败执行断点
    assert(file.is_open());

    // 临时记录
    std::string word;

    // 建立map(字典)
    std::map<std::string, int > wordcount;

    // 逐个将file输入流输入到word
    while (file >> word)
    {
        wordcount[word]++;
    }
    
    // 输出
    for (const auto& pair : wordcount) {
        std::cout << pair.first << " : " << pair.second << std::endl;
    }


    return 0;
}

 2.使用getline 和 stringstream:

/*
Problem:
Given a text file called "words.txt", write a C++ program
that reads in the file, counts the number of occurrences of each word,
and prints the results to the console.

The output should be case-sensitive, words in uppercase and lowercase
should be treated as distinct words.
The new streams we learned about in lecture namely std::ifstream, std::getline,
and std::stringstream will be helpful here!
*/
#include <fstream>      // ifstream
#include <map>          // map
#include <iostream>     // cout
#include <cassert>      // assert
#include <sstream>      // stringstream
int main() {
    // TODO: Write your code here

    // ifstream 流用于文件输入流
    std::ifstream file("words.txt");

    // 返回文件打开情况,若失败执行断点
    assert(file.is_open());

    // 临时记录
    std::string line;

    // 建立map(字典)
    std::map<std::string, int > wordcount;

    // 逐行将file输入流输入到line
    while (std::getline(file,line))
    {
        // 初始化stringstream流
        std::stringstream ss(line);
        std::string word;

        // stringstream流才能使用>>输入到string
        while(ss>>word)
            wordcount[word]++;
    }

    // for循环遍历std::map (const auto & 使)
    for (const auto& pair : wordcount) {
        std::cout << pair.first << " : " << pair.second << std::endl;
    }

    return 0;
}

分析:

1.这里 const auto& pair的意思是:

  • const表示pair是常量引用,它不会被修改;

  • auto自动推导pair的类型为map的value_type,也就是std::pair<const Key, T>;

  • &表示为引用,而不做值拷贝。

2.与直接写成auto pair不同,const auto&版本避免了额外的构造和拷贝操作:

  • auto pair会隐式地对map的每个元素进行值拷贝(拷贝完,输出);

  • const auto& version引用计数++,但不发生实际拷贝,效率更高。

 

相关推荐

  1. CS106L stream练习

    2023-12-15 19:04:02       41 阅读
  2. <span style='color:red;'>CSS</span><span style='color:red;'>练习</span>

    CSS练习

    2023-12-15 19:04:02      16 阅读
  3. C 练习实例16

    2023-12-15 19:04:02       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 19:04:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 19:04:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 19:04:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 19:04:02       20 阅读

热门阅读

  1. C# 避免定时器重入的4种方法

    2023-12-15 19:04:02       36 阅读
  2. 洛谷 P5483 小A的烦恼 题解

    2023-12-15 19:04:02       45 阅读
  3. 如何使用Composer安装和管理依赖?

    2023-12-15 19:04:02       47 阅读
  4. docker 定时检查磁盘并清理

    2023-12-15 19:04:02       40 阅读
  5. 爬虫心得分享小实用策略(应该不能算技巧)

    2023-12-15 19:04:02       37 阅读
  6. K8s client go 合并informer

    2023-12-15 19:04:02       40 阅读
  7. Scala-初学

    2023-12-15 19:04:02       41 阅读
  8. HackTheBox-Redeemer:Redis未授权访问

    2023-12-15 19:04:02       29 阅读
  9. SQL数列

    SQL数列

    2023-12-15 19:04:02      43 阅读