C++实验笔记
来源:互联网 发布:手机文件加密软件2016 编辑:程序博客网 时间:2024/06/01 17:03
4 - 5. Write a function that reads words from an input stream and stores them in a vector.Use that function both to write programs that count the number of words in the input, and to count how many times each word occurred.
#pragma once//read.h//read the words and save it into the vector#ifndef read_H#define read_H#include <vector>#include <iostream>#include <string>std::istream& read(std::istream& , std::vector<std::string>& , int &);#endif
//read.cpp#include "read.h"using std::string; using std::vector; using std::istream; using std::cout; using std::endl;std::istream& read(istream& in, vector<string>& strings, int & maxlen){string word;cout << "input words:" << endl;if (!in) return in;in.clear();while (in >> word)strings.push_back(word);maxlen = 0;for (vector<string>::const_iterator it = strings.begin();it != strings.end(); it++){string::size_type tmp; tmp = (*it).size();if (maxlen < tmp) maxlen = tmp;}return in;}
/*4 - 5. Write a function that reads words from an input stream and storesthem in a vector.Use that function both to write programs that count the number of words in the input, and to count how many times each word occurred.*///main#include <iostream>#include <vector>#include <map>#include "read.h"using namespace std;int main(){vector<string> words;int maxlen;read(cin, words, maxlen);cout << "the total words is:" << words.size() << endl;vector<string>::const_iterator i, j, first = words.begin(), second = words.end();map<string, int> sMap;for (i = first; i != second; ++i){string tmpi = *i;sMap[tmpi] = sMap[tmpi] ? sMap[tmpi] + 1 : 1;/*int k = 0;for (j = i + 1; j != second; ++j){string tmpj = *j;if (tmpi == tmpj)++k;}cout << tmpi << string(maxlen - tmpi.size() + 1, ' ') << k << endl;*/}for (i = first; i != second; ++i){string tmpi = *i;cout << tmpi << string(maxlen - tmpi.size() + 1, ' ') << sMap[tmpi] << endl;}return 0;}
0 0
- [C++]实验三
- c语言实验
- 哈希表实验C语言版
- C语言 枚举实验
- C-S通信实验
- C#-实验3
- C 实验总结
- C程序设计实验1
- C程序设计实验2
- c语言实验
- C++-实验-项目五
- C#—实验10.4
- C++---第七次实验。
- c++--补第二次实验
- C语言递归实验
- C语言实验-素数
- C+实验2
- C++STL 第二次实验
- iOS中定时器NSTimer的开启与关闭
- C++课后作业3
- 南阳理工学院软件月赛
- springMVC配置html和jsp视图
- jquery中ajax的远程请求jsonp学习
- C++实验笔记
- 2016.4.6海淀驾校考试科目二
- spring注解---@Resource和@Autowired
- Information Acquisition
- go away, white hair
- 3.2.8 record.c:传递邮件信息
- CentOS6.5安装Apache服务器
- js数组实际上是key-value对,长度可无限增长
- Sublime text 2/3 中 Package Control 的安装与使用方法