C++ 如何遍历列表函数?

C++ 如何遍历列表函数?,c++,list,function,for-loop,iteration,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(c

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

#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;

}
#包括
#包括
#包括
使用名称空间std;
列表开始(常量字符串和前置,列表测试);
列表结束(常量字符串和suf,列表测试);
int main(){
列表测试列表(5);
字符串后缀=“可”;
字符串前缀=“M”;
测试列表。推回(“同意”);
测试列表。推回(“可行”);
测试列表。推回(“机器”);
testList.push_back(“Makeable”);
测试列表。推回(“可用”);
对于(list::const_迭代器it=testList.begin();it!=testList.end();it++){

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

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

列表templast=beginsWith(…);for(…it=templast.begin();it!=templast.end();…
列表templast=beginsWith(…);for(…it=templast.begin();it!=templast.end());…建议概述您所做的事情以及您为什么这样做,以便提问者学到的不仅仅是复制和粘贴技能。是的。早些时候发布-现在在一些解释中编辑。更好,但我建议解释如何
自动前缀词=beginWith(前缀,测试列表);
解决了询问者遇到的可见问题。小调整:将
替换为(自动单词:测试)
替换为(const auto&word:test)
消除了不必要的
字符串
复制,并为编译器提供了一些优化的机会。这同样适用于其他
for
循环。建议概述您所做的事情以及您为什么这样做,以便询问者学到的不仅仅是复制和粘贴技能。是的。在早些时候发布过-在一些解释中编辑现在。更好了,但我建议解释一下
auto-prefixedWords=beginWith(prefix,testList);
如何解决询问者遇到的可见问题。小调整:将
交换为(auto-word:test)
交换为(const-auto&word:test)
消除了不必要的
字符串
副本,并为编译器提供了一些优化的机会。这同样适用于其他
for
循环。
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