如何遍历List函数?

时间:2018-03-08 03:16:15

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

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

#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个函数将迭代该列表,然后返回具有特定前缀或后缀的字符串列表。我会用第2和第3个for循环打印出来。第一个for-loop打印正确但是,我得到一个分段错误:11当我打印出第二个和第三个for循环时。我很困惑如何让这些for循环迭代列表函数并打印出内容。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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