Confused on how to iterate through a list function

I'm running a test program where I make a list of strings and try to find which strings have a certain suffix or prefix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
      #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;
    
    }


I declared a list of strings printed them out with the first for-loop. I also have 2 functions which will iterate through that list and then return a list of strings that have a certain prefix or suffix. I'll print those out with the 2nd and 3rd for-loop. The 1st for-loop prints out correctly however, I get a segmentation fault: 11 when I print out the 2nd and 3rd for loops. I'm confused as to how I would get those for-loops to iterate through the list functions and print out the contents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <list>
#include <string>
#include <algorithm>

std::list<std::string> beginWith( const std::string& prefix, const std::list<std::string>& seq );
std::list<std::string> endWith( const std::string& suffix, const std::list<std::string>& seq );

int main()
{
    // http://www.stroustrup.com/C++11FAQ.html#init-list
    const std::list<std::string> test_list { "Agreeable", "Doable", "Machine", "Makeable", "Available" };
    const std::string suffix = "able";
    const std::string prefix = "M";

    {
        // print test_list (range-based loop)
        // http://www.stroustrup.com/C++11FAQ.html#for
        // http://www.stroustrup.com/C++11FAQ.html#auto
        for( const auto& str : test_list ) std::cout << str << ' ' ;
        std::cout << '\n' ;

        // print test_list (classical loop)
        for( auto iter = test_list.begin() ; iter != test_list.end() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }

    {
        std::cout << "\n\nprefixed with '" << prefix << "':\n........................\n" ;

        // print list returned by beginWith (range-based loop)
        for( const auto& str : beginWith( prefix, test_list ) ) std::cout << str << ' ' ;
        std::cout << '\n' ;

        // print list returned by beginWith (classical loop)
        const auto bw_list = beginWith( prefix, test_list ) ; // we need a list object to iterate through 
        // note: begin() and end() are iterators to the same list
        for( auto iter = bw_list.begin() ; iter != bw_list.end() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }

    {
        std::cout << "\n\nsuffixed with '" << suffix << "':\n........................\n" ;

        // print list returned by endWith (range-based loop)
        for( const auto& str : endWith( suffix, test_list ) ) std::cout << str << ' ' ;
        std::cout << '\n' ;

        // print list returned by endWith (classical loop)
        const auto ew_list = endWith( suffix, test_list ) ;
        for( auto iter = ew_list.begin() ; iter != ew_list.end() ; ++iter ) std::cout << *iter << ' ' ;
        std::cout << '\n' ;
    }
}

std::list<std::string> beginWith( const std::string& prefix, const std::list<std::string>& seq )
{
    std::list<std::string> result ;
    // http://en.cppreference.com/w/cpp/string/basic_string/find (1)
    for( const auto& str : seq ) if( str.find(prefix) == 0 ) result.push_back(str) ;
    return result ;
}

bool has_suffix( const std::string& str, const std::string& suffix )
{ return str.size() >= suffix.size() && std::equal( suffix.rbegin(), suffix.rend(), str.rbegin() ) ; }

std::list<std::string> endWith( const std::string& suffix, const std::list<std::string>& seq )
{
    std::list<std::string> result ;
    // http://en.cppreference.com/w/cpp/algorithm/equal (1)
    for( const auto& str : seq ) if( has_suffix( str, suffix ) ) result.push_back(str) ;
    return result ;
}

http://coliru.stacked-crooked.com/a/a9ef5cdce35cc898
http://rextester.com/IUJSD28910
Topic archived. No new replies allowed.