473,769 Members | 5,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::map<int,st d::set<std::str ing> > Wrong? (Segmentation fault.)

Hello,

I have the following code:

std::map<int,st d::set<std::str ing> > k;
k[0]="1234567890 ";
k[1]="2345678901 ";
//...
std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)
throw(std::runt ime_error)
{
std::map<int,st d::set<std::str ing> >::const_iterat or i;
i=k.find(0);
if(i==k.end())
throw std::runtime_er ror("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: "Segmentati on 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::s tring> 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/
Jul 23 '05 #1
5 8748
"Peter Jansson" <we*******@jans son.net> wrote in message
news:Pi******** *************** ********@ukato. freeshell.org.. .
Hello,

I have the following code:

std::map<int,st d::set<std::str ing> > k;
k[0]="1234567890 ";
k[1]="2345678901 "; The above two lines are incorrect.
Maybe you meant:
k[0].insert("123456 7890");
k[1].insert("234567 8901");
//...
std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)
NB: you probably want to pass the k parameter by const reference:
std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > const&
k)
BTW: a typedef of two would probably make sense instead of writing
map<int,set<str ing> > repeatedly...
throw(std::runt ime_error)
{
std::map<int,st d::set<std::str ing> >::const_iterat or i;
i=k.find(0);
if(i==k.end())
throw std::runtime_er ror("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
Jul 23 '05 #2
On 3/14/2005 4:33 AM, Ivan Vecerina wrote:
"Peter Jansson" <we*******@jans son.net> wrote in message
news:Pi******** *************** ********@ukato. freeshell.org.. .
Hello,

I have the following code:

std::map<int, std::set<std::s tring> > k;
k[0]="1234567890 ";
k[1]="2345678901 ";


The above two lines are incorrect.
Maybe you meant:
k[0].insert("123456 7890");
k[1].insert("234567 8901");


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
Jul 23 '05 #3
Kristo wrote:
On 3/14/2005 4:33 AM, Ivan Vecerina wrote:
"Peter Jansson" <we*******@jans son.net> wrote in message
news:Pi******** *************** ********@ukato. freeshell.org.. .
Hello,

I have the following code:

std::map<int,st d::set<std::str ing> > k;
k[0]="1234567890 ";
k[1]="2345678901 ";

The above two lines are incorrect.
Maybe you meant:
k[0].insert("123456 7890");
k[1].insert("234567 8901");

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::s tring>. Look carefully at the
declaration of k.
to which
a value (e.g., "1234567890 ") can be safely assigned.

Kristo

Jul 23 '05 #4

On Mon, 14 Mar 2005, Ivan Vecerina wrote:
The above two lines are incorrect.
Maybe you meant:
k[0].insert("123456 7890");
k[1].insert("234567 8901");


Yes, that is what I meant.
std::set<std::s tring> myMethod(std::m ap<int,std::set <std::string> > k)
throw(std::runt ime_error)
{
std::map<int,st d::set<std::str ing> >::const_iterat or i;
i=k.find(0);
if(i==k.end())
throw std::runtime_er ror("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::s tring> myMethod_which_ works(std::map< int,std::set<st d::string> > k)
throw(std::runt ime_error)
{
std::map<int,st d::set<std::str ing> >::const_iterat or i;
std::set<std::s tring> strings;
bool haveNotFoundZer o=true;
for(i=k.begin() ;i!=k.end() && haveNotFoundZer o;k++)
{
if(i->first==0)
{
haveNotFoundZer o=false;
strings=i->second;
}
}
if(haveNotFound Zero)
throw std::runtime_er ror("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/
Jul 23 '05 #5
"Peter Jansson" <we*******@jans son.net> wrote in message
news:Pi******** *************** *******@norge.f reeshell.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::s tring>
myMethod(std::m ap<int,std::set <std::string> > const& k)
throw(std::runt ime_error)
{
std::map<int,st d::set<std::str ing> >::const_iterat or i = k.find(0);
if(i==k.end())
throw std::runtime_er ror("No zero in k.");
return i->second;
}

int main()
{
std::map<int,st d::set<std::str ing> > k;
k[0].insert("123456 7890");
k[1].insert("234567 8901");

std::set<std::s tring> 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
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2376
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, /tmp/cceNEN7W.s:286 String too big (5071 bytes) line: #.stabs "lower_bound::1317:_ZNSt8_Rb_treeISsSt4
3
10833
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 tables, so far I have been using maps like: std::map<std::string, int> frequency; A string for the letter combinations and an int for the frequency, but now I want to add three more frequencies:
19
6164
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 found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
1
3756
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) class library and type in the following code below: #include <map> #include<string>
8
14333
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 ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
11
8795
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
6
4749
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 predicate for the <set>. Here is an example: #include <string> #include <set> using namespace std;
5
3779
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 in total. how do I go about it?, do I set a vector<ofstream*cities; something like
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10039
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5297
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.