Solved

# Sample code using sets

Posted on 2004-11-03
192 Views
Could someone provide me with sample code just using sets and iterators.  Just searching (strings) and accessing.

Thanks!
0
Question by:jewee

LVL 55

Accepted Solution

A basic set iteration at http://www.linuxselfhelp.com/HOWTO/C++Programming-HOWTO-17.html

#include <iostream>
#include <set>

using namespace std;

int main()
{
set<int, less<int> > s;
set<int, less<int> >::iterator i;

s.insert(4);
s.insert(0);
s.insert(-9);
s.insert(7);
s.insert(-2);
s.insert(4);
s.insert(2);

cout << "The set contains the elements: ";
for (i=s.begin(); i!=s.end(); i++) cout << *i << ' ';
cout << endl;
}

Here are some operations with sets:
http://www.yrl.co.uk/phil/stl/stl.htmlx#Set_and_Multiset
0

LVL 8

Assisted Solution

To find a value (string in your case), it could look something like:

set<string> s;
s.insert("why");
s.insert("not");
set<string>::iterator current = s.find("why");
if(current == s.end())
{
// Didn't find it
}
else
{
cout << *current << endl;
}
0

LVL 86

Assisted Solution

http://www.sgi.com/tech/stl/set.html ("set") not also documents that well, but comes with a little more complex example:

Example

struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};

int main()
{
const int N = 6;
const char* a[N] = {"isomer", "ephemeral", "prosaic",
"nugatory", "artichoke", "serif"};
const char* b[N] = {"flat", "this", "artichoke",
"frigate", "prosaic", "isomer"};

set<const char*, ltstr> A(a, a + N);
set<const char*, ltstr> B(b, b + N);
set<const char*, ltstr> C;

cout << "Set A: ";
copy(A.begin(), A.end(), ostream_iterator<const char*>(cout, " "));
cout << endl;
cout << "Set B: ";
copy(B.begin(), B.end(), ostream_iterator<const char*>(cout, " "));
cout << endl;

cout << "Union: ";
set_union(A.begin(), A.end(), B.begin(), B.end(),
ostream_iterator<const char*>(cout, " "),
ltstr());
cout << endl;

cout << "Intersection: ";
set_intersection(A.begin(), A.end(), B.begin(), B.end(),
ostream_iterator<const char*>(cout, " "),
ltstr());
cout << endl;

set_difference(A.begin(), A.end(), B.begin(), B.end(),
inserter(C, C.begin()),
ltstr());
cout << "Set C (difference of A and B): ";
copy(C.begin(), C.end(), ostream_iterator<const char*>(cout, " "));
cout << endl;
}
0

LVL 8

Expert Comment

Sorry, the find should be
set<string>::iterator current = find(s.begin(), s.end(), "why"); // #include <algorithm>
0

## Featured Post

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.