CS106L stream exercises

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. Do not use getline and 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. Use getline and 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;
}

analyze:

1. Here const auto& pair means:

  • const means that pair is a constant reference and it will not be modified;

  • auto automatically deduces that the type of pair is the value_type of map, that is, std::pair<const Key, T>;

  • & is expressed as a reference without copying the value.

2. Unlike writing directly as auto pair, the const auto& version avoids additional construction and copy operations:

  • Auto pair will implicitly copy the value of each element of the map (after copying, output);

  • const auto& version reference counting++, but no actual copy occurs, which is more efficient.

 

おすすめ

転載: blog.csdn.net/2301_79140115/article/details/134948146