【问题标题】:How to iterate through a List function?如何遍历 List 函数?
【发布时间】:2018-08-16 07:33:26
【问题描述】:

我正在运行一个测试程序,我在其中创建一个字符串列表并尝试找出哪些字符串具有特定的后缀或前缀。

#include <iostream>
#include <list>
#include <string>

using namespace std;


list<string> beginWith(const string& pre, list <string> test);
list<string> endWith(const string& suf,list <string> test);


int main(){
    list <string> testList(5);
    string suffix = "able";
    string prefix = "M";
    testList.push_back("Agreeable");
    testList.push_back("Doable");
    testList.push_back("Machine");
    testList.push_back("Makeable");
    testList.push_back("Available");

    for(list <string>::const_iterator it = testList.begin(); it != testList.end(); it++){
        cout << *it << endl;        
    }

    for(list <string>::const_iterator it = beginWith(prefix, testList).begin(); it != beginWith(prefix, testList).end(); it++){
        cout << *it << endl;        
    }

    for(list <string>::const_iterator it = endWith(suffix, testList).begin(); it != endWith(suffix, testList).end(); it++){
        cout << *it << endl;
    }

return 0;
}


list<string> beginWith(const string& pre, list<string> test){
    list <string> answer;
    for(list <string>::const_iterator it = test.begin(); it != test.end(); it++){
        if(pre == it->substr(0, pre.length())){
            answer.push_back(*it);
        }
    }

    return answer;

}

list<string> endWith(const string& suf, list <string> test){
    list <string> answer;
    for(list <string>::const_iterator it = test.begin(); it != test.end(); it++){
        if(suf == it->substr(it->length() - suf.length() , it->back())){
            answer.push_back(*it);

        }
    }

    return answer;

}

我声明了一个字符串列表,使用第一个 for 循环将它们打印出来。我还有 2 个函数将遍历该列表,然后返回具有特定前缀或后缀的字符串列表。我将使用第二个和第三个 for 循环打印出来。第一个 for 循环打印正确但是,我得到一个分段错误:11 当我打印出第二个和第三个 for 循环时。我对如何让这些 for 循环遍历列表函数并打印出内容感到困惑。

【问题讨论】:

  • list&lt;string&gt; tempList = beginsWith(...); for(... it = tempList.begin(); it != tempList.end(); ...

标签: c++ list function for-loop iteration


【解决方案1】:

beginWithendWith 按值返回一个列表。这使得 for 循环在列表的不同副本上调用 begin()end()

【讨论】:

    【解决方案2】:
    list<string> beginWith(const string& pre, list<string> test) {  
        list <string> answer;
        for (auto word : test)  // Use C++ auto to iterate collection
        {
            cout << "Testing " << word << " against " << pre << " ... ";
            if (word.find(pre) == 0) // From http://thispointer.com/c-check-if-a-string-starts-with-an-another-given-string/
            {
                cout << "match!";
                answer.push_back(word);
            }
            cout << '\n';
        }
        return answer;
    }
    
    list<string> endWith(const string& suf, list <string> test) {
        list <string> answer;
        for (auto word : test)
        {
            cout << "Testing " << word << " against " << suf << " ... ";
            if (word.size() >= suf.size() &&
                word.compare(word.size() - suf.size(), suf.size(), suf) == 0)  // From http://thispointer.com/c-how-to-check-if-a-string-ends-with-an-another-given-string/  
            {
                cout << "match!";
                answer.push_back(word);
            }
            cout << '\n';
        }
        return answer;
    }
    
    int main(int argc, wchar_t *argv[])
    {
        list <string> testList {}; // Create empty list, not list with five elements
        string suffix = "able";
        string prefix = "M";
        testList.push_back("Agreeable");
        testList.push_back("Doable");
        testList.push_back("Machine");
        testList.push_back("Makeable");
        testList.push_back("Available");
    
        for (auto word : testList) {
            cout << word << '\n';
        }
    
        auto prefixedWords = beginWith(prefix, testList);
        cout << "Prefixed words: \n";
        for (auto prefixed : prefixedWords) {
            cout << "  " << prefixed << '\n';
        }
    
        auto suffixedWords = endWith(suffix, testList);
        cout << "Suffixed words: \n";
        for (auto suffixed : suffixedWords) {
            cout << "  " << suffixed << '\n';
        }
    
        return 0;
    }
    

    程序输出:

    Agreeable
    Doable
    Machine
    Makeable
    Available
    Testing Agreeable against M ...
    Testing Doable against M ...
    Testing Machine against M ... match!
    Testing Makeable against M ... match!
    Testing Available against M ...
    Prefixed words:
      Machine
      Makeable
    Testing Agreeable against able ... match!
    Testing Doable against able ... match!
    Testing Machine against able ...
    Testing Makeable against able ... match!
    Testing Available against able ... match!
    Suffixed words:
      Agreeable
      Doable
      Makeable
      Available
    

    【讨论】:

    • 建议概述您做了什么以及为什么这样做,以便提问者学习的不仅仅是复制和粘贴技能。
    • 是的。之前匆忙发布 - 现在编辑一些解释。
    • 更好,但我建议解释auto prefixedWords = beginWith(prefix, testList); 如何解决提问者遇到的可见问题。小调整:用for (const auto &amp; word : test) 交换for (auto word : test) 消除了不必要的string 副本,并为编译器提供了一些优化机会。这同样适用于其他 for 循环。
    猜你喜欢
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多