stack / broken code

i have problems with this code i was following an example and i does run on xcode but it does not run in other editors as jgrass


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
#include <iostream>
#include <stack>
#include <string>
#include <list>

using namespace std;

int main (int argc, char ** argv){
    cout <<"iitialize stack from list: " <<endl;
    list<int> li = { 1, 2, 3, 4, 5 };
    stack<int, list<int>> si(li);//constructor copies to new list
    
    cout << "li has  "<<li.size()<<"entries; si has "<<si.size()<<" entries. " << endl;
    
    cout <<"pop all from si: "<<endl;
    while(!si.empty()){
        cout << si.top() << " ";
        si.pop();
        
    }
    cout << endl;
    
    cout << "li has " << li.size() << "entries: si has" << si.size()<< " entries "<<endl;
    
    cout <<"contents of li after ls is empty: "<<endl;
    for (int i : li) cout << i <<' ';
    cout<<endl;
    
    stack<string>ss;
    cout << "push string string onto ss: "<<endl;
    ss.push("one");
    ss.push("two");
    ss.push("three");
    ss.push("four");
    ss.push("five");
    cout <<"size ss: "<<ss.size()<<endl;
    cout << "pop all from ss: "<< endl;
    while(!ss.empty()){
        cout<< ss.top()<< " ";
        ss.pop();
        
    }
    
    cout <<endl;
    cout <<"sixe of ss: "<< ss.size() <<endl;
    return 0;
    
    
}
It seems to run on the C++ Shell compiler (the gear icon next to your code).


Does it crash (not run) or not compile?

The program will require C++11 support -- on older compilers this needs to be enabled explicitly.
it runs here but, i use another text editor and did not run and gave me an error like this ( error: non-aggregate type 'list<int>' cannot be initialized with an initializer list
list<int> li = { 1, 2, 3, 4, 5 }; )
What compiler are you using when you received the error? How exactly did you compile the code?

Using an initialization list requires a C++11 or higher compatible compiler.
Topic archived. No new replies allowed.