使用Ubuntu Linux上使用G 编译引物示例代码时出错
Error when compile the primer example code using g++ on Ubuntu Linux
代码来自 C 引物(3第三)。错误是:
*filterstring.cpp:在函数'int main()'中:filterstring.cpp:32:68:错误:无法转换'__gnu_cxx :: __ _____ normal_iterator*,std :: vector>>'to'std :: string :: string* {aka std :: basic_string :: basic_string }'initialization
请帮助我分析错误,谢谢。
代码:
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
using namespace std;
template <class InputIterator>
void filter_string(InputIterator first, InputIterator last, string filt_elems = string("",?.")) {
for (; first != last; first++){
string:: size_type pos = 0;
while ((pos = (*first).find_first_of(filt_elems, pos)) != string::npos)
(*first).erase(pos, 1);
}
}
bool length_less (string s1, string s2) {
return s1.size() < s2.size();
}
int main() {
istream_iterator<string> input(cin), eos;
vector<string> text;
copy(input, eos, back_inserter(text));
string filt_elems("",.?;:");
filter_string(text.begin(), text.end(), filt_elems);
int cnt = text.size();
string *max = max_element(text.begin(), text.end(), length_less);
int len = max->size();
cout << "The number of words read is " << cnt << endl;
cout << "The longest word has a length of " << len << endl;
cout << "The longest word is " << *max << endl;
return 0;
}
第32行,
std::max_element(text.begin(), text.end(), length_less);
此函数返回一个向前的迭代器,该函数解决了搜索范围内最大元素的第一个出现的位置,而不是字符串。
您可以做什么,而不是这样的行:
string *max = max_element(text.begin(), text.end(), length_less);
您必须这样做,
//First find the index of the max_element , by subtracting the forward iterator you get from calling max_element from the iterator for first element .
int index=max_element(text.begin(), text.end(), length_less) - text.begin();
//And then find string stored on that index.
string *max = text.at(index);
这很有趣。迭代器的行为就像指针一样,但不完全是。特别是,您不能将迭代器转换为指针。
但是,您可以将此代码更改为使用迭代器作为字符串*指针:
vector<string>::iterator max = max_element(text.begin(), text.end(), length_less);
声明Max不是字符串的指针,而是迭代器中的字符串向量,这是Max_Element算法应用于字符串向量时返回的内容。
。您也可以使用指针,但这是一个坏习惯。仅用于测试这个想法,您可以:
string *max = &*max_element(text.begin(), text.end(), length_less);
*max_element(...)返回对字符串的引用。创建一个(字符串*)指向该字符串的指针。
这引起了麻烦,因为对向量的结构修改可能会悄悄地使该指针无效。随后使用指针将将"随机"内存视为字符串对象。更糟糕的是,它可能在您的测试过程中起作用,并且在软件发货之前不会失败!
迭代器的体面实现应检测到无效并引发例外。可预测的失败比随机崩溃更好。
好的,所以我走了。我认为这是使用Lambdas和Auto更现代的解决方案。我把它留给别人,以决定是否更容易理解。
#include <algorithm>
#include <iostream>
#include <iterator>
#include <ostream>
#include <string>
#include <vector>
using namespace std;
template <class InputIterator>
void filter_string(InputIterator first, InputIterator last,
const string filt_elems = const string("",?."))
{
for_each(first, last,
[filt_elems](string& s)
{
s.erase(
// Shift valid characters up before erasing the undesirable
remove_if(s.begin(), s.end(),
[filt_elems](string::value_type c)
{ return filt_elems.find_first_of(c) != string::npos; }),
s.end());
});
}
int main()
{
istream_iterator<string> input(cin);
istream_iterator<string> eos;
vector<const string> words;
copy(input, eos, back_inserter(words));
const string filt_elems("",.?;:");
filter_string(words.begin(), words.end(), filt_elems);
const int count = words.size();
// Get a reference to the longest word
const auto& max_word = *max_element(words.cbegin(), words.cend(),
[](const string& lhs, const string& rhs)
{ return lhs.size() < rhs.size(); });
const int length = max_word.size();
cout << "The number of words read is " << count << endl;
cout << "The longest word has a length of " << length << endl;
cout << "The longest word is " << max_word << endl;
return 0;
}
- 在Linux for Windows上编译C++代码时出错
- 尝试将另一个子句添加到代码中时出错
- 将代码从 Python 转换为 C++ 时出错
- <limits> 使用 VSC2019 编译旧代码时出错
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 在OpenGL中使用程序集代码渲染视频时出错
- 编译以下代码时出错
- Boost FFT示例-编译时出错,这段代码在做什么
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- C++:为什么这段代码会出错?多元线性回归
- 混合开放MP/MPI代码中的多线程发送/接收时出错
- 将节点 js 代码集成到 cpp 库时出错
- 构建代码时出错
- 在字符串流中使用来自 github 的代码时出错
- 在主代码中调用 .hpp 函数时出错
- 将 find_if() 与数组一起使用的代码段中出错
- 这段带有副本C++08的代码出错
- 我在做什么这个C++代码出错了
- 我的函数代码出错(停止工作)
- 打开txt文件并创建值返回函数的代码出错