Thread: Sets and Lists

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Sets and Lists

    I've been searching these forums and Google for good articles on how to use C++ sets and lists but I've only found a lot of stuff talking about their built in functions, not actually using them. Do any of you know of any good tutorials? I started building a test program but I ran into some compiler errors:

    Code:
    #include <set>
    #include <iostream>
    #include <list>
    #include <cstdlib>
    
    int main(int argc, char *argv[])
    {
    
        list<int> intList; // line 8
    
        for(int i = 0; i < 10; i++)
        {
            intList.push_front(i + 10); // line 12
        }
    
        list<int>::iterator theIterator; // line 15
        for(theIterator = intList.begin(); theIterator != intList.end(); theIterator++)
        {
            cout << *theIterator;
        }
    
        system("Pause");
        return 0;
    }
    Code:
    8 setTest.cpp `list' undeclared (first use this function) 
    8 setTest.cpp expected primary-expression before "int" 
    8 setTest.cpp expected `;' before "int" 
    12 setTest.cpp `intList' undeclared (first use this function) 
    15 setTest.cpp expected primary-expression before "int" 
    16 setTest.cpp expected `;' before "int" 
    17 setTest.cpp `theIterator' undeclared (first use this function) 
    19 setTest.cpp `cout' undeclared (first use this function)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Use std::list (if you're not using "using namespace std;")
    Code:
      std::list<int> intList;

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The C++ Standard Library by Josuttis is widely considered to be an excellent tutorial and reference on standard library containers, algorithms and a few other things. If you have access to it and plan to do a lot of C++ programming, it is worth the cost.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    What is the difference between using sets and lists? A set is ordered and cannot contain duplicates? When I look up the syntax on cppreference.com, they basically have the same code written down for set and list examples (for examples under set they use list<chat> and no mention of set<char> - here)

    Here is some simple code I have that I wanted to loop through all the items in a set and print them out. When I use the above code it works (using list) but this code is giving me compiler errors:

    Code:
    #include <iostream>
    #include <set>
    #include <list>
    
    int main()
    {
        int myInts[] = {1, 2, 3, 4};
        set<int> iSet(myInts, myInts+5); // line 8
    
        set<int>::iterator theIterator; // line 10
        for(theIterator = iSet.begin(); theIterator != iSet.end(); theIterator++)
        {
            cout << *theIterator << "\n";
        }
    
        system("Pause");
        return 0;
    
    }
    Code:
    8 setTest.cpp `set' undeclared (first use this function) 
    8 setTest.cpp expected primary-expression before "int" 
    8 setTest.cpp expected `;' before "int" 
    10setTest.cpp expected primary-expression before "int" 
    10 setTest.cpp expected `;' before "int" 
    11 setTest.cpp `theIterator' undeclared (first use this function) 
    11 setTest.cpp `iSet' undeclared (first use this function)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have exactly the same problem as before.

    >> set<int> iSet(myInts, myInts+5); // line 8
    Also, this line has a mistake. Your array has four elements, you shouldn't be using 5 here.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Same problem, use std::set instead of set. Most books tend to leave out std:: in their examples for conciseness, so you need to remember it.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> for examples under set they use list<chat> and no mention of set<char> - here
    The reference page just has an error (probably just got copied and pasted), it should refer to set instead of list.

    As far as differences, a set is ordered based on some sorting criteria, it does not contain duplicates (although a multiset can), and it provides fast lookup of a value. A list remembers the order you place a value in the list, and has different efficiency promises for the different operations. Pushing a value on to the front or back of a set doesn't make sense because the set figures out the order, so there is no push_front or push_back function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists : removeduplicate(); problem.
    By InsideTheAsylum in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2005, 08:50 PM
  2. Linked Lists
    By Philandrew in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2004, 07:53 PM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM
  5. circular linked lists??
    By atif in forum C Programming
    Replies: 3
    Last Post: 04-30-2002, 03:58 AM