passing a std::list to function

I am trying to learn abouts lists. Can any on help me with my functions. I would like to pass my list to a function that sorts it using std::sort then I can display the sorted list with another function.

Thanks in advance!

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
  
#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>

//void sortList(std::list<int> &list);
//void editList(std::list<int> &list);
void showList(std::list<int> &list, std::list<int>::iterator, std::list<int>::iterator);


int main(int argc, const char * argv[]) {
    
    std::list<int> randomNumbers;
    srand(static_cast<unsigned int>(time(NULL)));
    
    
    //fill list with random integers and display
    for(int i = 0; i < 20; i++){
        randomNumbers.push_back(1 + rand() % 50);
    }
    std::list<int>::iterator itStart = randomNumbers.begin();
    std::list<int>::iterator itEnd = randomNumbers.end();
    
    //display unsorted list
    std::cout << "Unsorted list:" << std::endl;
    for(std::list<int>::iterator it = randomNumbers.begin(); it != randomNumbers.end(); it++){
        std::cout << *it << " ";
    }
    
    
    //sortList(randomNumbers);
    showList(randomNumbers, itStart, itEnd);
    
    
    return 0;
}

//
//void sortList(std::list<int> &list, std::list<int>::iterator start, std::list<int>::iterator end){
//    
//    std::sort(list.begin(), list.end());
//
//}

void showList(std::list<int> &list,std::list<int>::iterator start, std::list<int>::iterator end){
    
    std::cout << "\nThe list:" << std::endl;
    
    for(std::list<int>::iterator it = start; it != end; it++){
        std::cout << *it << " ";
    }
    std::cout << std::endl;
}


closed account (48T7M4Gy)
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
#include <iostream>
#include <list>
#include <algorithm>

void sortList(std::list<int> &list);
void showList(const std::list<int> &);

int main(int argc, const char * argv[]) {
    
    std::list<int> randomNumbers;
    
    
    //fill list with random integers and display
    srand(static_cast<unsigned int>(time(NULL)));
    for(int i = 0; i < 20; i++){
        randomNumbers.push_back(1 + rand() % 50);
    }
    
    //display unsorted list
    std::cout << "Unsorted list:" << std::endl;
    showList(randomNumbers);
    
    // SORT LIST
    sortList(randomNumbers);
    
    // DISPLAY SORTED LIST
    std::cout << "Sorted list:" << std::endl;
    showList(randomNumbers);
    
    return 0;
}

void showList(const std::list<int> &aList){
    for(auto it: aList){
        std::cout << it << ' ';
    }
    std::cout << '\n';
}


void sortList(std::list<int> &aList){
    aList.sort();
}
Unsorted list:
20 16 42 40 41 38 26 40 27 12 36 15 44 42 28 13 32 16 49 15 
Sorted list:
12 13 15 15 16 16 20 26 27 28 32 36 38 40 40 41 42 42 44 49 
Program ended with exit code: 0


http://en.cppreference.com/w/cpp/container/list/sort
Last edited on
and to add to kemort's, as ever, excellent reply OP you can't do this:
 
 std::sort(list.begin(), list.end());

because std::sort requires random-access iterators whereas the list.begin(), list.end() iterators are bi-directional types - all random access iterators are also bidirectional but the reverse is not true
std::sort(): http://en.cppreference.com/w/cpp/algorithm/sort
list.begin(): http://www.cplusplus.com/reference/list/list/begin/
bi-directional iterator: http://www.cplusplus.com/reference/iterator/BidirectionalIterator/
To sort a std::list using std::sort, we would have to resort to an indirect sort.

1
2
3
4
5
6
7
8
9
const std::list<int> seq { 5, 9, 3, 2, 7, 1, 4, 8, 0, 6 } ;

// indirect sort with std::sort
// http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
std::vector< std::reference_wrapper<const int> > refs( std::begin(seq), std::end(seq) ) ;
std::sort( std::begin(refs), std::end(refs) ) ;

for( int v : refs ) std::cout << v << ' ' ;
std::cout << '\n' ;

http://coliru.stacked-crooked.com/a/1a8103553cb44c62

The recommended way to sort std::list (direct sort) is to use the member function sort()
http://en.cppreference.com/w/cpp/container/list/sort
Though the cppreference page doesn't mention it, the complexity is:
"Approximately N log(N) comparisons, where N == size()"

1
2
3
4
5
6
7
std::list<int> seq { 5, 9, 3, 2, 7, 1, 4, 8, 0, 6 } ;

// (direct) stable sort with list::sort
seq.sort() ; // http://en.cppreference.com/w/cpp/container/list/sort

for( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;

http://coliru.stacked-crooked.com/a/1a8103553cb44c62
Excellent! Thanks very much everyone for your help, not sure why I couldn't get i to work but it does now - thanks again!
Though the cppreference page doesn't mention it,

does now
just a few more things please - I'm trying to pehaps be a bit ambitious and over reach... Ive added some insert and erase functions, but they don't seem to have the desired effect!! It sjust something I want to try and get working

Thanks in advance

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <list>
#include <cmath>
#include <algorithm>

void sortList(std::list<int> &list);
void editList(std::list<int> &list);
void showList(std::list<int> &alist);


int main(int argc, const char * argv[]) {
    
    std::list<int> randomNumbers;
    srand(static_cast<unsigned int>(time(NULL)));
    int sentinel = 0;
    
    
    //fill list with random integers and display
    for(int i = 0; i < 5; i++){
        randomNumbers.push_back(1 + rand() % 50);
    }
    
    //display unsorted list
    std::cout << "Unsorted list:" << std::endl;
    for(std::list<int>::iterator it = randomNumbers.begin(); it != randomNumbers.end(); it++){
        std::cout << *it << " ";
    }
    
    do{
        
        std::cout << "\n1 to sort list" << std::endl;
        std::cout << "2 to display list" << std::endl;
        std::cout << "3 to edit list" << std::endl;
        std::cout << "-1 to end. Selection: ";
        std::cin >> sentinel;
        while( (!std::cin) || (sentinel < -1 || sentinel > 3 )){
            std::cin.clear();
            std::cin.ignore();
            std::cout << "-1 to end. Please select: ";
            std::cin >> sentinel;
        }
        
        switch (sentinel) {
            case 1:
                std::cout << "case 1" << std::endl;
                sortList(randomNumbers);
                break;
            case 2:
                std::cout << "case 2" << std::endl;
                showList(randomNumbers);
                break;
            case 3:
                std::cout << "case 3" << std::endl;
                editList(randomNumbers);
                break;
            default:
                std::cout << "Not a choice!" << std::endl;
                break;
        }
    }while(sentinel != -1);
    
    
    return 0;
}


void sortList(std::list<int> &alist){
    
    alist.sort();

}

void showList(std::list<int> &list){
    
    std::cout << "\nThe list:" << std::endl;
    
    for( auto it : list){
        std::cout << it << " ";
    }
    std::cout << std::endl;
}

void editList(std::list<int> &aList){
    
    int sentinel = 0;
        
        std::cout << "\n1 to push_front" << std::endl;
        std::cout << "2 to push_back" << std::endl;
        std::cout << "3 to insert" << std::endl;
        std::cout << "4 to erase" << std::endl;
        std::cout << "-1 to end. Selection: ";
        std::cin >> sentinel;
        while( (!std::cin) || (sentinel < -1 || sentinel > 4 )){
            std::cin.clear();
            std::cin.ignore();
            std::cout << "-1 to end. Please select: ";
            std::cin >> sentinel;
        }
        
        switch (sentinel) {
            case 1:
                std::cout << "Input number to push_front: " << std::endl;
                int number;
                std::cin >> number;
                aList.push_front(number);
                break;
            case 2:
                std::cout << "Input number to push_back: " << std::endl;
                std::cin >> number;
                aList.push_back(number);
                break;
            case 3:
                std::cout << "Input position and number to insert: " << std::endl;
                std::cout << "Postion: ";
                int position;
                std::cin >> position;
                std::cout << "Number: ";
                std::cin >> number;
                for( std::list<int>::iterator it = aList.begin(); it != aList.end(); it++){
                    if(*it == position){
                        aList.insert(it, number);
                    }
                }
                break;
            case 4:
                std::cout << "Input position to erase: " << std::endl;
                std::cout << "Postion: ";
                std::cin >> position;
                for( std::list<int>::iterator it = aList.begin(); it != aList.end();){
                    if(*it == position){
                        it = aList.erase(it);
                    }
                    else{
                        it++;
                    }
                }
                break;
            default:
                std::cout << "Not a choice!" << std::endl;
                break;
        }
    
    
    
}

Last edited on
Sorry - I've been having a bad day. After a long hard look at what I was doing I have realised that my code is ok - just me that is wrong.

As a rule of thumb - I'm not usually that stupid 😂


Thanks again for all of your help.
Last edited on
Topic archived. No new replies allowed.