码迷,mamicode.com
首页 > 编程语言 > 详细

【ThinkingInC++】4、统计txt文本中单词的个数

时间:2014-08-09 11:48:27      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:bitset   文件输入输出   

其中要使用的txt文本!

 header defines classes for file IO, including ifstream, whose constructor takes a file name an argument. The expression f >> word extracts the next non-whitespace token from the file and returns the stream. When a stream appears in a boolean context, such as the while statement above, it evaluates to true until end-of-file reached or an error occurs.

It turns out that on many operating systems you can take advantage of i/o redirection, which allows you to map standard input or output to a file on the command line. If you rewrite Words.cpp to read from cin, then you can read any file you want, as the following illustrates.


源程序


/**
* 功能:统计txt文本中单词的个数
* 时间:2014年8月9日09:09:33
* 作者:cutter_point
*/

#include<iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
#include<bitset>

using namespace std;

int main()
{
    ifstream fin("words.txt");
    string word;
    int wordNo=0;

    while(fin>>word)
    {
        ++wordNo;
    }

    std::bitset<8> b2(wordNo);

    cout<<"一共有:2进制"<<b2<<"个单词"<<endl;
    cout<<"一共有:8进制"<<oct<<wordNo<<"个单词"<<endl;
    cout<<"一共有:10进制"<<wordNo<<"个单词"<<endl;
    cout<<"一共有:16进制"<<hex<<wordNo<<"个单词"<<endl;


    system("pause");
    return 0;
}


【ThinkingInC++】4、统计txt文本中单词的个数,布布扣,bubuko.com

【ThinkingInC++】4、统计txt文本中单词的个数

标签:bitset   文件输入输出   

原文地址:http://blog.csdn.net/cutter_point/article/details/38453347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!