"Peter Jansson" <webmaster@jansson.net> wrote in message
news:Pine.NEB.4.61.0503151827170.3337@norge.freesh ell.org...[color=blue]
> On Mon, 14 Mar 2005, Ivan Vecerina wrote:[color=green]
>> The above code seems ok, and I don't see a problem in it.
>> Maybe you could post a complete working sample?
>>[/color]
>
> Well, the non-working code is the myMethod method above (non-working since
> it fails while executing, it compiles fine). Below, is the code that
> works.[/color]
Again, your usage of the map::find member function seemed correct,
but a problem somewhere else in your code could not be excluded.
And as requested above, *you* should post a minimum code sample
that compiles and reproduces the problem you see.
Something like:
#include <iostream>
#include <map>
#include <set>
#include <string>
std::set<std::string>
myMethod(std::map<int,std::set<std::string> > const& k)
throw(std::runtime_error)
{
std::map<int,std::set<std::string> >::const_iterator i = k.find(0);
if(i==k.end())
throw std::runtime_error("No zero in k.");
return i->second;
}
int main()
{
std::map<int,std::set<std::string> > k;
k[0].insert("1234567890");
k[1].insert("2345678901");
std::set<std::string> set = myMethod(k); // does not throw
std::cout << *set.begin(); // prints "1234567890"
}
[color=blue]
> I thought the method find should be more transparent and perhaps even
> optimized for sorted maps but it did not work as already mentioned.[/color]
map::find is transparent and optimized, and does work.
You shouldn't persuade yourself otherwise before writing a complete
minimum sample that compiles and illustrates the issue you observe.
[color=blue]
> I would be grateful for any suggestion/hint on what is wrong.[/color]
The above code shall work, and does on the platform I use.
If it fails on your system, this would be a bug in the implementation
you use, and you should seek support from your vendor or a platform-
specific forum (and use the code sample as a bug report).
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form