STL lists

This is a discussion on STL lists within the C++ Programming forums, part of the General Programming Boards category; Compiled the code on g++ and it won't work, why? Code: #include <iostream> #include <string> #include <list> #include <algorithm> using ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212

    STL lists

    Compiled the code on g++ and it won't work, why?

    Code:
    #include <iostream>
    #include <string>
    #include <list>
    #include <algorithm>
    using namespace std;
    
    void func(string t) {
        cout << t << endl;
    }
    
    int main() {
        list < string > prod_code;
        string temp;
        do {
     cout << "Enter Product Code: ";
     if (cin >> temp) {
         prod_code.push_back(temp);
     } else {
         break;
     }
        } while (1);
    
        sort( prod_code.begin(), prod_code.end() );
        for_each(prod_code.begin(), prod_code.end() , func);
        return 0;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    The compile won't work..? Least, I couldn't get it to go, but I modfied it a bit and it was all working fine.

    Code:
    
    #include <iostream>
    #include <string>
    #include <list>
    #include <algorithm>
    using namespace std;
    
    void func(string t) {
        cout << t << endl;
    }
    
    int main() {
        list < string > prod_code;
        string temp;
        do {
     cout << "Enter Product Code: ";
     if (cin >> temp) {
         // couldn't figure out how to make cin >> temp fail without exitting program :(
         if (temp != "stop")     prod_code.push_back(temp);
         else break;
     } else {
         break;
     }
        } while (1);
    
        // sort( prod_code.begin(), prod_code.end() );  // didn't compile with gcc 2.9.5 or 3.0.4
        // list's have their own special sort (std::uglyAndHackish :( ) because they don't have random access iterators
        // which makes the default (usual quicksort) simply no longer quick :).
        prod_code.sort();
        for_each(prod_code.begin(), prod_code.end() , func);
        return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Thanks heaps for your help, greatly appreciated. I compiled the code and it works. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  2. STL Linked Lists
    By stanlvw in forum C++ Programming
    Replies: 12
    Last Post: 04-11-2008, 01:33 AM
  3. A question about STL Lists.
    By joenching in forum C++ Programming
    Replies: 9
    Last Post: 04-25-2005, 09:23 PM
  4. Stl lists and user defined types
    By figa in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2005, 11:09 AM
  5. help with STL lists
    By WebmasterMattD in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2003, 06:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21