Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k[0]="1234567890";
k[1]="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)
throw(std::runtime_error)
{
std::map<int,std::set<std::string> >::const_iterator i;
i=k.find(0);
if(i==k.end())
throw std::runtime_error("No zero in k.");
return i->second;
}
Compilation of this code goes well, but I have the following problem while
executing this in my implementation: "Segmentation fault". I have
pin-pointed the problem to be the last return statement. When I ignore
using find(int) and instead loops over the map with the following code,
everyting goes fine.
std::set<std::string> strings;
for(i=k.begin();i!=k.end();i++)
{
if(k->first==0)
strings=k->second;
}
return strings;
Anybody know what is going on here?
Regards,
Peter Jansson http://www.jansson.net/ 5 8705
"Peter Jansson" <we*******@jansson.net> wrote in message
news:Pi*******************************@ukato.frees hell.org... Hello,
I have the following code:
std::map<int,std::set<std::string> > k; k[0]="1234567890"; k[1]="2345678901";
The above two lines are incorrect.
Maybe you meant:
k[0].insert("1234567890");
k[1].insert("2345678901");
//... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)
NB: you probably want to pass the k parameter by const reference:
std::set<std::string> myMethod(std::map<int,std::set<std::string> > const&
k)
BTW: a typedef of two would probably make sense instead of writing
map<int,set<string> > repeatedly...
throw(std::runtime_error) { std::map<int,std::set<std::string> >::const_iterator i; i=k.find(0); if(i==k.end()) throw std::runtime_error("No zero in k."); return i->second; }
The above code seems ok, and I don't see a problem in it.
Maybe you could post a complete working sample?
Regards,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
On 3/14/2005 4:33 AM, Ivan Vecerina wrote: "Peter Jansson" <we*******@jansson.net> wrote in message news:Pi*******************************@ukato.frees hell.org...
Hello,
I have the following code:
std::map<int,std::set<std::string> > k; k[0]="1234567890"; k[1]="2345678901";
The above two lines are incorrect. Maybe you meant: k[0].insert("1234567890"); k[1].insert("2345678901");
What's wrong with them? If an object with key x doesn't exist,
operator[] will create a default object (in this case a string) to which
a value (e.g., "1234567890") can be safely assigned.
Kristo
Kristo wrote: On 3/14/2005 4:33 AM, Ivan Vecerina wrote:
"Peter Jansson" <we*******@jansson.net> wrote in message news:Pi*******************************@ukato.frees hell.org...
Hello,
I have the following code:
std::map<int,std::set<std::string> > k; k[0]="1234567890"; k[1]="2345678901";
The above two lines are incorrect. Maybe you meant: k[0].insert("1234567890"); k[1].insert("2345678901");
What's wrong with them? If an object with key x doesn't exist, operator[] will create a default object (in this case a string)
No, in this case a std::set<std::string>. Look carefully at the
declaration of k.
to which a value (e.g., "1234567890") can be safely assigned.
Kristo
On Mon, 14 Mar 2005, Ivan Vecerina wrote: The above two lines are incorrect. Maybe you meant: k[0].insert("1234567890"); k[1].insert("2345678901");
Yes, that is what I meant. std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error) { std::map<int,std::set<std::string> >::const_iterator i; i=k.find(0); if(i==k.end()) throw std::runtime_error("No zero in k."); return i->second; }
The above code seems ok, and I don't see a problem in it. Maybe you could post a complete working sample?
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.
std::set<std::string> myMethod_which_works(std::map<int,std::set<std::st ring> > k)
throw(std::runtime_error)
{
std::map<int,std::set<std::string> >::const_iterator i;
std::set<std::string> strings;
bool haveNotFoundZero=true;
for(i=k.begin();i!=k.end() && haveNotFoundZero;k++)
{
if(i->first==0)
{
haveNotFoundZero=false;
strings=i->second;
}
}
if(haveNotFoundZero)
throw std::runtime_error("No zero in k.");
return strings;
}
I thought the method find should be more transparent and perhaps even
optimized for sorted maps but it did not work as already mentioned. I
would be grateful for any suggestion/hint on what is wrong.
Regards,
Peter Jansson http://www.jansson.net/
"Peter Jansson" <we*******@jansson.net> wrote in message
news:Pi******************************@norge.freesh ell.org... On Mon, 14 Mar 2005, Ivan Vecerina wrote: The above code seems ok, and I don't see a problem in it. Maybe you could post a complete working sample?
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.
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"
}
I thought the method find should be more transparent and perhaps even optimized for sorted maps but it did not work as already mentioned.
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.
I would be grateful for any suggestion/hint on what is wrong.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Marcelo Pinto |
last post by:
Hi all,
In a class of mine I tried to use the following member:
std::map<std::string, std::set<std::string> > m_files;
but I recieved the following error message:
mips-tfile,...
|
by: Juicer_X |
last post by:
Hello everyone,
I've been working with the STL Containers for a little while now, but in
the middle of working on a small "Markov Chain" class I realized that I
wanted to modify my frequency...
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not...
|
by: Maxwell |
last post by:
Hello,
I having having oodles of trouble using the std lib in my MC++ (VS.NET
2003) Class library. I figured out a simple sample to reproduce the
errors I am having. Create a MC++ (VS.NET 2003)...
|
by: davihigh |
last post by:
My Friends:
I am using std::ofstream (as well as ifstream), I hope that when i
wrote in some std::string(...) with locale, ofstream can convert to
UTF-8 encoding and save file to disk. So does...
|
by: Martin Jørgensen |
last post by:
Hi,
- - - - - - - - - - - - - - -
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
|
by: Martin |
last post by:
Hi
I need to maintain a <setof pointers to objects, and it must be
sorted based on the values of pointed objects, not pointer values. I
can achieve this easily by defining my own comparing...
|
by: Gary Wessle |
last post by:
Hi
I have a map<string, doublem_temperatures which gets updated
often.
I need to save the data to files corresponding to each string each
time the map is updated, I am expecting about 80 files...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| | |