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引用计数++,但不发生实际拷贝,效率更高。

 

  • 19
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值