簡體   English   中英

STL std :: remove_if編譯失敗

[英]STL std::remove_if compiler failure

我無法得到std :: remove_if進行編譯,因為你可以看到我選擇了一種工作正常的替代手搖曲柄方法,編譯器錯誤位於代碼之后的列表底部。

任何幫助將非常感激。

謝謝,湯姆

#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <string>

//
// Find the largest compound word composed
// of sub-words from a list.
//
// - read list from file. 
//
// Psuedo Code:
//
// 1. Read Next Word from File.
// 2. Search in list for word formed from word.
// 3. if Found in List
// 4.   if Found Compound is longer then Current Compound
// 5.     Replace
// 6.     Remove all strings less then Current Compound Length 
// 7. 
//

typedef std::set<std::string> StrSet;
typedef StrSet::iterator StrSetIter;


struct if_substr
{
    std::string m_word;
public:
    if_substr(const std::string& w) : m_word(w) { }

    bool operator() (const std::string& str) const
    {
    std::size_t f = m_word.find(str);
    return (std::string::npos!=f && m_word.length()>str.length());
    }

};

struct if_remove
{
    std::string m_word;
public:
    if_remove(const std::string& w) : m_word(w) { }

    bool operator() (std::string str) const
    {
        return m_word.length()>str.length();
    }

};


class FindLongestCompound
{

    std::ifstream m_file;

    StrSet m_words;
    std::string m_current;

public:
    FindLongestCompound(std::string filename)
    {
    m_file.open(filename, std::ifstream::in); 

    if (!m_file)
        throw std::runtime_error("Failed to open file"+filename);
    }


    void Start(void)
    {
    std::string nextWord;
    while(m_file >> nextWord)
    {

        std::cout << "read word: " << nextWord << std::endl;
        if_substr ifSubstr(nextWord);
        StrSetIter srchItem = std::find_if(std::begin(m_words), std::end(m_words),ifSubstr);
        if (srchItem != m_words.end())
        {
        m_current = nextWord; 
        std::cout << "new current: " << m_current << std::endl;


        if_remove ifRemove(m_current);
        std::remove_if(m_words.begin(), m_words.end(),ifRemove);

        #if 0
        StrSetIter j = m_words.begin();
        do 
        {
            if (j->length() < m_current.length()) 
            j = m_words.erase(j);
            else 
            j++;

        } while (j != m_words.end());
        #endif
        } 
        std::cout << "insert next word: " << nextWord << std::endl;
        m_words.insert(nextWord);
    } 
    }

    std::string result() { return m_current; } 

};


int main(int argc, char *argv[])
{
    if (argc<2)
    {
    std::cout << "Please provide filename" << std::endl;
    return -1;
    }

    FindLongestCompound flc(argv[1]);

    flc.Start();

    std::cout << "result: " << flc.result() << std::endl;
} 
 --- errors --- In file included from stackoverflow.C:2: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15: In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:2148:26: error: no viable overloaded '=' *__first = _VSTD::move(*__i); ~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~ stackoverflow.C:91:8: note: in instantiation of function template specialization 'std::__1::remove_if<std::__1::__tree_const_iterator<std::__1::basic_string<char>, std::__1::__tree_node<std::__1::basic_string<char>, void *> *, long>, if_remove>' requested here std::remove_if(m_words.begin(), m_words.end(),ifRemove); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1415:19: note: candidate function not viable: 'this' argument has type 'const value_type' (aka 'const std::__1::basic_string<char>'), but method is not marked const basic_string& operator=(const basic_string& __str); ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1422:45: note: candidate function not viable: 'this' argument has type 'const value_type' (aka 'const std::__1::basic_string<char>'), but method is not marked const _LIBCPP_INLINE_VISIBILITY basic_string& operator=(const value_type* __s) {return assign(__s);} ^ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1423:19: note: candidate function not viable: 'this' argument has type 'const value_type' (aka 'const std::__1::basic_string<char>'), but method is not marked const basic_string& operator=(value_type __c); ^ 1 error generated. 

問題不在於您的仿函數,而在於您正在使用的容器類型。 你不能將std::remove_ifstd::set (在這種情況下你的StrSet )。

簡而言之, std::remove_if不會刪除任何內容,只是更改元素的順序 ,使“已刪除”元素僅出現在某個點之后。 只有erase才真正消除它們。

但是, std::set具有已定義的元素順序 ,任何操作都無法繞過該順序 假設您有以下一組按字典順序排列的字符串:

{ "aaa", "bbb", "ccc", "ddd", "eee" }

現在,您嘗試將std::remove_if應用於一個移除所有僅元音字符串的std::remove_if函數。 你會最終得到這個:

{ "bbb", "ccc", "ddd", "aaa", "eee" }
                     ^
                     |
        "removed" elements start here

這適用於例如std::vector但不適用於std::set因為它會導致一個元素不再正確排序的集合(“刪除”元素仍然是集合的一部分!)。 所以你得到一個編譯錯誤。

為了達到預期目標,請在循環中使用std::set::erase 它將正常工作,因為只有擦除元素的迭代器無效,因此end()仍然有效。

if_remove ifRemove(m_current);
for (StrSet::iterator set_iter = m_words.begin(); set_iter != m_words.end(); )
{
    if (ifRemove(*set_iter))
    {
        set_iter = m_words.erase(set_iter);
    }
    else
    {
        ++set_iter;
    }
}

在C ++ 11中,您可以使用auto而不是StrSet::iterator

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM