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       //添加 ifstream
#include           //添加 map
#include      //添加 cout
#include       //添加 assert
int main() {
    // TODO: Write your code here

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

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

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

    // 建立map(字典)
    std::map 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       // ifstream
#include           // map
#include      // cout
#include       // assert
#include       // stringstream
int main() {
    // TODO: Write your code here

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

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

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

    // 建立map(字典)
    std::map 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;

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

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

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

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

 

你可能感兴趣的:(CS106,c++)