these strings won't stack!

I'm learning to use the stl containers, iterators and algorithms. There is one line of this exercise which isn't working and I cannot understand why.

In the problem code, I am reading a list of strings and attempting to populate a stack. The error message to follow the code.

Here's my code

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

//  main.cpp
//  FunWithSTL
//
//  Created by Aaron on 10/26/15.
//  Copyright © 2015 Aaron. All rights reserved.
//


#include <iostream>
#include <string>
#include <utility>
#include <iterator>
#include <unordered_map>
#include <list>
#include <vector>
#include <stack>

using namespace std;

typedef unordered_map<string, int > scorecard;
typedef list<string> roster;
typedef stack<string> plates;

int main(int argc, const char * argv[]) {
    const int SIZE = 10;

    //1)
    pair<string, int> Values[] =
    {
        { "Stewie", 100 }, { "Brian", 90 }, { "Meg", 66 },
        { "Lois", 89 }, { "Chris", 17 }, { "Peter", 21 }, { "Marge", 77 }, { "Bart", 62 }, { "Lisa", 99 },{ "Maggie", 3 }
    };
    


    //2))
    vector<int> scores;
    vector<string> names;
    for (int i =0;i < SIZE; i++){
        names.push_back(Values[i].first);
        scores.push_back((Values[i].second));
    
    }
    

    //3)
  
    for(int i = 0 ;i < scores.size();i++){
        cout << names[i] <<" got "<< scores[i]<<endl;
        //pairs.insert(names[i], scores[i]);

        
    }
    scorecard scard;

    int index = 0;
    for (scorecard::const_iterator iter = scard.begin();
        iter != scard.end();++iter){
        scard.insert(std::make_pair(Values[index].first,Values[index].second));
        ++index;
        }
        

   
    
   
    
    //4)  scard is the map and roster is the list
    roster r;
    for (scorecard::const_iterator iter = scard.begin();iter !=scard.end();++iter){
        r.push_back(iter->first);
    }
   
    
    
    //5
    sort(r.begin(),r.end());
    
    //6
    plates n;
    for (roster::const_iterator iter = r.begin(); iter !=r.end();++iter){
        n.push(*iter);
    }
  
    //7
    while (!n.empty()) {
        cout << n.top()<< endl;
        n.pop();
    }
    
    

    return 0;
}


The line underneath //6 where the for loop is is broken. The error is: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm:4004:65: Invalid operands to binary expression ('std::__1::__list_iterator<std::__1::basic_string<char>, void *>' and 'int')

Any ideas what I'm doing wrong?

Thanks!
Last edited on
Line 78: std::sort(r.begin(),r.end());

std::sort() requires RandomAccessIterators. std::list<>::iterator is not a RandomAccessIterator
http://en.cppreference.com/w/cpp/algorithm/sort

Use std::list<>::sort instead. http://en.cppreference.com/w/cpp/container/list

1
2
3
//5
// std::sort(r.begin(),r.end());
r.sort() ;


http://coliru.stacked-crooked.com/a/738b12b57bdd0178
Thanks! That worked like a charm. There were a bunch of other problems with this code, but I was able to work through them once I got over this.
Topic archived. No new replies allowed.