-
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)
-
Use std::list (if you're not using "using namespace std;")
Code:
std::list<int> intList;
-
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.
-
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)
-
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.
-
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.
-
>> 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.