加速c++练习8-5解不清楚
Accelerated C++ exercise 8-5 solution not clear
我正在解决加速c++练习8-5,我不想错过这本书中的任何一个练习。
加速c++练习8-5如下:
重新实现第7章的
gen_sentence
和xref
函数以使用输出迭代器,而不是将整个输出放在一个数据中结构。通过编写附加的程序来测试这些新版本直接输出迭代器到标准输出,并通过存储结果分别为list <string>
和map<string, vector<int> >
。
为了理解这个问题的范围和本书这一部分的现有知识,这个练习是泛型函数模板和模板中迭代器使用章节的一部分。之前的练习是实现<algorithm>
库函数的简单版本,如equal, find, copy, remove_copy_if
等。
如果我理解正确,我需要修改xref
函数,所以它:
- 使用输出迭代器
- 将结果存储在
map<string, vector<int> >
我试图将映射迭代器作为back_inserter()
, .begin()
, .end()
传递给此函数,但无法编译它。这里的答案解释了为什么。
xref函数,如第7章:
// find all the lines that refer to each word in the input
map<string, vector<int> >
xref(istream& in,
vector<string> find_words(const string&) = split)
{
string line;
int line_number = 0;
map<string, vector<int> > ret;
// read the next line
while (getline(in, line)) {
++line_number;
// break the input line into words
vector<string> words = find_words(line);
// remember that each word occurs on the current line
for (vector<string>::const_iterator it = words.begin();
it != words.end(); ++it)
ret[*it].push_back(line_number);
}
return ret;
}
分裂实现:
vector<string> split(const string& s)
{
vector<string> ret;
typedef string::size_type string_size;
string_size i = 0;
// invariant: we have processed characters `['original value of `i', `i)'
while (i != s.size()) {
// ignore leading blanks
// invariant: characters in range `['original `i', current `i)' are all spaces
while (i != s.size() && isspace(s[i]))
++i;
// find end of next word
string_size j = i;
// invariant: none of the characters in range `['original `j', current `j)' is a space
while (j != s.size() && !isspace(s[j]))
++j;
// if we found some nonwhitespace characters
if (i != j) {
// copy from `s' starting at `i' and taking `j' `-' `i' chars
ret.push_back(s.substr(i, j - i));
i = j;
}
}
return ret;
}
请帮助了解我错过了什么
我在这里找到了更多关于练习的细节:https://stackoverflow.com/questions/5608092/accelerated-c-exercise-8-5-wording-help:
template <class Out> void gen_sentence( const Grammar& g, string s, Out& out )
用法:
std::ostream_iterator<string> out_str (std::cout, " "); gen_sentence( g, "<sentence>", out_str );
template <class Out, class In> void xref( In& in, Out& out, vector<string> find_words( const string& ) = split )
用法:
std::ostream_iterator<string> out_str (std::cout, " "); xref( cin, out_str, find_url ) ;
坦率地说,我不得不得出结论,这个问题是不适定的,特别是他们为xref
指定的新接口:xref应该导致映射。然而,在这种情况下,使用输出迭代器意味着使用std::inserter(map, map.end())
。虽然您可以编写代码的编译版本,但这不会达到您的期望,因为map::insert
将简单地忽略任何具有重复键的插入。
如果xref的目标只是将单词链接到它们第一次出现的行号,这仍然是可以的,但是我有一种感觉,练习的作者只是忽略了这个微妙的点:)
下面是代码(注意,我为split
发明了一个愚蠢的实现,因为它既缺失又必需):
#include <map>
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <iterator>
std::vector<std::string> split(const std::string& str)
{
std::istringstream iss(str);
std::vector<std::string> result;
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(result));
return result;
}
// find all the lines that refer to each word in the input
template <typename OutIt>
OutIt xref(std::istream& in,
OutIt out,
std::vector<std::string> find_words(const std::string&) = split)
{
std::string line;
int line_number = 0;
// read the next line
while (getline(in, line)) {
++line_number;
// break the input line into words
std::vector<std::string> words = find_words(line);
// remember that each word occurs on the current line
for (std::vector<std::string>::const_iterator it = words.begin();
it != words.end(); ++it)
*out++ = std::make_pair(*it, line_number);
}
return out;
}
int main(int argc, const char *argv[])
{
std::map<std::string, int> index;
std::ifstream file("/tmp/test.cpp");
xref(file, std::inserter(index, index.end()));
#if __GXX_EXPERIMENTAL_CXX0X__
for(auto& entry: index)
std::cout << entry.first << " first found on line " << entry.second << std::endl;
#else
for(std::map<std::string, int>::const_iterator it = index.begin();
it != index.end();
++it)
{
std::cout << it->first << " first found on line " << it->second << std::endl;
}
#endif
return 0;
}
- 关于隐式声明的复制构造函数的引用在逻辑上不清楚
- C++复制 c'tor 现在确实会采取行动。 不清楚为什么
- 矢量编程:不清楚矢量的初始化
- 调用虚函数的逻辑不清楚(或者是方法隐藏?
- cudaMemcpy 在从设备读取到主机时返回 cudaErrorInvalidArgument,不清楚原因
- 实现附加对象:不清楚的文档示例
- 智能指针移动语义我不清楚
- 在 c++ 中移动 2d 数组的构造函数(语法逻辑不清楚):
- 为什么这个错误如此不清楚
- 在特定情况下,指针删除和铸造之间的关系不清楚
- 切换语句在 C++ 中的输出不清楚
- 不清楚的副本分配操作员示例
- 我在c++应用程序上搞不清楚
- Visual Studio 2012 中的错误日志不清楚
- C++引用-有些事情我不清楚
- 解密不清楚的语法
- 使用Boost Coroutine(1.55)的不清楚分割错误
- 不清楚按位 AND 赋值的使用
- 纹理不清楚的 OpenGL 渲染
- 加速c++练习8-5解不清楚