M
ma740988
The position returned via the STL std::set container never made much
sense to me. When you insert elements within the container, the
position returned - via find - does not reflect the actual position of
the value within the container. std::string - for instance - does.
How then do I get the actual position of the value when using
std::set?
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
# include <fstream>
using std::ifstream;
using std:
fstream;
# include <vector>
using std::vector;
# include <iomanip>
using std::setw;
# include <string>
using std::string;
# include <limits>
using std::numeric_limits;
# include <algorithm>
using std::copy;
# include <iterator>
using std::istream_iterator;
using std:
stream_iterator;
using std::back_inserter;
# include <set>
using std::set;
int main()
{
cout << "Testing std::set " << '\n';
set<int> cc;
cc.insert(10);
cc.insert(30);
cc.insert(20);
if (cc.insert(20).second)
cout << "success" << '\n';
else
cout << "failure -> set doesn't allow duplicates " << '\n';
cc.insert(40);
set<int>::const_iterator pos = cc.begin();
pos = cc.find(40);
if (pos != cc.end())
cout << "Found " << *pos << " within the set " << '\n' << '\n';
else
cout << " not found " << '\n';
// for (; pos != cc.end(); ++pos)
// cout << *pos << '\n';
cout << "Testing std::string " << '\n';
string my_string ("abcdefg");
string::size_type idx = my_string.find('c');
if (idx == string::npos)
cout << "not found " << '\n';
else
cout << "String found at position: " << static_cast<int>(idx) <<
'\n';
}
my_string.find ('c') returns 2 for the position which makes sense.
cc.find(40) on the other hand 'returns' the contents at the position
not the actual position.
Consider:
std::set<int, greater<int> > mySet;
mySet.insert (10);
mySet.insert (40);
mySet.insert (50);
mySet.insert (35);
mySet.insert (30);
Based on the sort criteria, could I get a 'write up' - of sorts on
how/when the actual 'sorting' happens?
I envision per my reading of the C++ Std Library.
10 gets inserted.
40 gets inserted. Since 40 is greater than 10, 40 gets moved to the
'top'.
50 gets inserted. Since 50 is greater than 10 && 50 greater than 40,
50 gets moved to the top. This describes the 'transitive' behavior of
strick weak ordering.
and so on ...?
At some point I suspect I'll peruse the source (if avaibable) of
std::set since I'm also not sure (but curious) of how the 'motor
operates' - if you will.
Take the case again where 50 gets inserted. This much is true,
there's an operator() for the criteria, however, is it safe to state
that operator() is called twice? Once to compare 50 against 10, and a
second time to compare 50 against 40, except I now feel like I'm
headed towards logarithimic complexity versus linear versus .. can't
recall the other one.
Thanks for your time.
sense to me. When you insert elements within the container, the
position returned - via find - does not reflect the actual position of
the value within the container. std::string - for instance - does.
How then do I get the actual position of the value when using
std::set?
# include <iostream>
using std::cout;
using std::cin;
using std::endl;
# include <fstream>
using std::ifstream;
using std:
# include <vector>
using std::vector;
# include <iomanip>
using std::setw;
# include <string>
using std::string;
# include <limits>
using std::numeric_limits;
# include <algorithm>
using std::copy;
# include <iterator>
using std::istream_iterator;
using std:
using std::back_inserter;
# include <set>
using std::set;
int main()
{
cout << "Testing std::set " << '\n';
set<int> cc;
cc.insert(10);
cc.insert(30);
cc.insert(20);
if (cc.insert(20).second)
cout << "success" << '\n';
else
cout << "failure -> set doesn't allow duplicates " << '\n';
cc.insert(40);
set<int>::const_iterator pos = cc.begin();
pos = cc.find(40);
if (pos != cc.end())
cout << "Found " << *pos << " within the set " << '\n' << '\n';
else
cout << " not found " << '\n';
// for (; pos != cc.end(); ++pos)
// cout << *pos << '\n';
cout << "Testing std::string " << '\n';
string my_string ("abcdefg");
string::size_type idx = my_string.find('c');
if (idx == string::npos)
cout << "not found " << '\n';
else
cout << "String found at position: " << static_cast<int>(idx) <<
'\n';
}
my_string.find ('c') returns 2 for the position which makes sense.
cc.find(40) on the other hand 'returns' the contents at the position
not the actual position.
Consider:
std::set<int, greater<int> > mySet;
mySet.insert (10);
mySet.insert (40);
mySet.insert (50);
mySet.insert (35);
mySet.insert (30);
Based on the sort criteria, could I get a 'write up' - of sorts on
how/when the actual 'sorting' happens?
I envision per my reading of the C++ Std Library.
10 gets inserted.
40 gets inserted. Since 40 is greater than 10, 40 gets moved to the
'top'.
50 gets inserted. Since 50 is greater than 10 && 50 greater than 40,
50 gets moved to the top. This describes the 'transitive' behavior of
strick weak ordering.
and so on ...?
At some point I suspect I'll peruse the source (if avaibable) of
std::set since I'm also not sure (but curious) of how the 'motor
operates' - if you will.
Take the case again where 50 gets inserted. This much is true,
there's an operator() for the criteria, however, is it safe to state
that operator() is called twice? Once to compare 50 against 10, and a
second time to compare 50 against 40, except I now feel like I'm
headed towards logarithimic complexity versus linear versus .. can't
recall the other one.
Thanks for your time.