使用Ubuntu Linux上使用G 编译引物示例代码时出错

Error when compile the primer example code using g++ on Ubuntu Linux

本文关键字:代码 出错 编译 Linux Ubuntu 使用      更新时间:2023-10-16

代码来自 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;
}