Troudeloup
asked on
[noob][c++] std::set how do I use it?
how do I use set?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is each item in the set called a node?
ASKER
this doesn't work :(
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
set<int> ::interator i;
s.insert(4);
s.insert(0);
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
}
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
set<int> ::interator i;
s.insert(4);
s.insert(0);
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, insert is to put a element/node into the ...set,
how do I access it?
can I arrange them?
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
set<int> ::interator i;
s.insert(4);
s.insert(0);
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
}
how do I access it?
can I arrange them?
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> s;
set<int> ::interator i;
s.insert(4);
s.insert(0);
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
big to small
ASKER
how do I display number of elements/node in the set?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
set<int, greater<int> > s;
set<int, greater<int> >::iterator i;
set < //skip > // so this calls the set template?
what is
int, greater<int> ?
and i am guessing that
in
set<int, greater<int> > s;
set<int, greater<int> >::iterator i;
s; and
::iterator i;
are put in two lines following the big chunk because there can be more settings to the set<> right?
set<int, greater<int> >::iterator i;
set < //skip > // so this calls the set template?
what is
int, greater<int> ?
and i am guessing that
in
set<int, greater<int> > s;
set<int, greater<int> >::iterator i;
s; and
::iterator i;
are put in two lines following the big chunk because there can be more settings to the set<> right?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
I don't get this part, how do I do this?
i = 5;
cout << s.i << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
I don't get this part, how do I do this?
i = 5;
cout << s.i << endl;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
show an item from the set given i = n;
like,
in that case
i expect
s.(0) = 4
or something
like,
in that case
i expect
s.(0) = 4
or something
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
well, i ll admit (yet again)
that I am doing homework,
but i am trying to figure out the bits myself.
I think these two terms will help:
I am trying to learn the syntax of
its accessors and mutators.
that I am doing homework,
but i am trying to figure out the bits myself.
I think these two terms will help:
I am trying to learn the syntax of
its accessors and mutators.
ASKER
i have to use set.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>>> show an item from the set given i = n;
set<int, greater<int> > s;
set<int, greater<int> >::iterator iter;
int n = 4;
int i = 0;
for (iter=s.begin(), i = 0; iter !=s.end(); ++iter, ++i)
{
if (i == n)
{
cout << *iter << endl;
break;
}
}
You see a set is not very suitable for 'indexed' access. The purpose of a set is to get 'unique' items into an automatically sorted container:
set<int> s;
s.insert(5);
s.insert(4);
s.insert(1);
set<int>::iterator iter = s.begin();
cout << *iter; // shows 1
*iter = 6; // update first element
iter = s.begin();
cout << *iter; // shows 4
Note, the set::iterator::operator*() is an overloaded derefencere operator. It isn't a dereference of a 'pointer' as set::iterator is a class type and not a pointer. But of course it is 'willingly' made that you can use an iterator like a pointer.
set<int, greater<int> > s;
set<int, greater<int> >::iterator iter;
int n = 4;
int i = 0;
for (iter=s.begin(), i = 0; iter !=s.end(); ++iter, ++i)
{
if (i == n)
{
cout << *iter << endl;
break;
}
}
You see a set is not very suitable for 'indexed' access. The purpose of a set is to get 'unique' items into an automatically sorted container:
set<int> s;
s.insert(5);
s.insert(4);
s.insert(1);
set<int>::iterator iter = s.begin();
cout << *iter; // shows 1
*iter = 6; // update first element
iter = s.begin();
cout << *iter; // shows 4
Note, the set::iterator::operator*()
ASKER
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int, less<int> > s;
set<int, less<int> >::iterator i;
// what's the two line above?
// this reads like an object instance "s" has two more numbers added to it.
s.insert(4);
s.insert(0);
// this merely prints all, i think "i" is the index.
cout << "The set contains the elements: " << endl;
for (i=s.begin(); i !=s.end(); i++)
{
cout << *i << endl;
}
}